Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by straydogstrut · May 19, 2010 at 12:41 PM · movementcharactercontrollerjumping

Make the Character Controller jump to the side

Hi all, how would I go about adding a sideways jump to the FPSWalker script? For example, dodging to the left or right to avoid a collision. I've attempted it with the following but just get vertical jumping. Thanks.

[EDIT] Thanks to Steve pointing out that the FPSWalker script already does most of what I want! I am very forgetful sometimes.

I've gone back to that script with the addition of a boolean so that the player character can only dodge while fighting. There's still the issue of turning though, since the wolf will need to be able to turn to face the target (we're not using the mouse for turning, only orbiting the PC). It would be nice if the player character could rotate while on the ground, but when pressing the jump button and left/right, he jumps to the side without any rotation. If anyone has any ideas how I can amend the following script to accomodate that I'd be grateful.

if (grounded) { // We are grounded, so recalculate movedirection directly from axes if(isFighting){ moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); } else { moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical")); transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0); }

     moveDirection = transform.TransformDirection(moveDirection);
     moveDirection *= currentSpeed;



     if (Input.GetButton ("Jump")) {
         moveDirection.y = jumpSpeed;
     }
 }

 // Apply gravity
 moveDirection.y -= gravity * Time.deltaTime;

 // Move the controller
 var controller : CharacterController = GetComponent(CharacterController);
 var flags = controller.Move(moveDirection * Time.deltaTime);
 grounded = (flags & CollisionFlags.CollidedBelow) != 0;




 }

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SteveFSP · May 19, 2010 at 01:22 PM 0
Share

Could you clarify a bit what you are looking for that differs from the default FPSWalker script behavior? In the default script the jump will already occur in whatever direction you are moving. I.e. If you are strafing to the left and press the jump button, you will jump to the left.

I see you've added some rotation code. Are you looking for some sort of jump plus turn behavior?

avatar image straydogstrut · May 19, 2010 at 02:15 PM 0
Share

Ah..I didn't realise that!! it's been so long since I modified it. I'll go back to the original script and check that out. The turning is just for turning while on the ground (as opposed to strafing,, since that would be unrealistic for a wolf!) Will I still be able to keep my rotation (as a separate move) with the jumping?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by SteveFSP · May 19, 2010 at 03:24 PM

It should take one simple adjustment to your script. Move the rotation down to where you check the jump. I checked it in my sandbox project and it appears to behave correctly.

[EDIT] Fixed the code to restore the rotation block while fighting. That adds an extra boolean check.

function FixedUpdate() {

if (grounded) {

 var inputH : float = Input.GetAxis("Horizontal");
 var inputV : float = Input.GetAxis("Vertical");

 // We are grounded, so recalculate movedirection directly from axes
 if (isFighting) {
     moveDirection = new Vector3(inputH, 0, inputV);
 } else {
     // Don't rotate here.
     moveDirection = new Vector3(0, 0, inputV);
 }
 moveDirection = transform.TransformDirection(moveDirection);
 moveDirection *= speed;

 if (Input.GetButton ("Jump")) {
     moveDirection.y = jumpSpeed;
 } else {
     if (!isFighting) {
         // Only rotate when not jumping.
         transform.Rotate(0, inputH * rotateSpeed * Time.deltaTime, 0);
     }
 }

}

// Apply gravity moveDirection.y -= gravity * Time.deltaTime;

// Move the controller var controller : CharacterController = GetComponent(CharacterController); var flags = controller.Move(moveDirection * Time.deltaTime); grounded = (flags & CollisionFlags.CollidedBelow) != 0;

}

There is one weakness to the way you are doing it. If the character is rapidly bouncing up and down off the ground, such as can occur on rough ground, the rotation will be a bit jittery.

If that is a problem for you, you may want to mimic the smooth rotation method used by the standard MouseLook Script. I.e. Create a private method that implements the the rotation smoothly, and call the method in place of the direct call to "transform.Rotate(0, inputH rotateSpeed Time.deltaTime, 0);".

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image straydogstrut · May 19, 2010 at 03:42 PM 0
Share

Hi Steve, thanks for taking the time to look at this, I really appreciate it. I had something like that a short while ago. Your suggestion does work, but the only problem is the character strafes and rotates while grounded in the isFighting condition. I would rather it just turned. Thanks for the heads up on the smoothing, i'll look at implementing that now too.

avatar image straydogstrut · May 19, 2010 at 03:56 PM 0
Share

I think i'm making this harder than it needs to be. I haven't animated the dodge yet so i'm thinking rather than jumping to the side, a roll would be easier (I can just use strafing). I'll try that and see how it goes. $$anonymous$$any thanks again Steve.

avatar image SteveFSP · May 19, 2010 at 04:13 PM 0
Share

Oops, missed that. I fixed the answer to restore the rotation block when fighting.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Why can't I properly jump? 2 Answers

Character Controller / Charactor Motor has no inputs? 1 Answer

How to make your character jump? 1 Answer

Can't jump while sprinting 1 Answer

How to keep current velocity while jumping? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges