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
1
Question by CaptainIcy281 · Dec 17, 2011 at 11:29 PM · movementjumping

Controlling direction while jumping using CharacterController

Hi, I know this question has been asked (and solved) before, but I have had no luck with using previous solutions.

I'm fairly new to scripting in Unity, but I was doing okay until now… I just don't understand this one thing.

I have a simple CharacterController move script and can move, turn, jump, etc. Only problem is, once I jump in a direction, I'm locked there. I have it set up to allow turning in mid-air, but I still cannot move on the Z axis.

I know about the apparent fix, removing "IsGrounded" but that has not worked for me. Can anyone help me?

Thanks. :)

My script:

 var speed : float = 6.0;
 var jumpSpeed : float = 8.0;
 var turnSpeed : float = 60;
 var gravity : float = 20.0;
 
 private var moveDirection : Vector3 = Vector3.zero;
 
 function Update() {
     var controller : CharacterController = GetComponent(CharacterController);
     var turn: float = Input.GetAxis("Horizontal");
     transform.Rotate(0, turn * turnSpeed * Time.deltaTime, 0);
     if (controller.isGrounded) { // only move or jump if grounded
     moveDirection = transform.forward * Input.GetAxis("Vertical") * speed;
         if (Input.GetButton ("Jump")) {
             moveDirection.y = jumpSpeed;
         }
     }
     // Apply gravity
     moveDirection.y -= gravity * Time.deltaTime;
     // Move the controller
     controller.Move(moveDirection * Time.deltaTime);
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by aldonaletto · Dec 17, 2011 at 11:57 PM

You must move the moveDirection calculation outside the isGrounded if - but this screws up the gravity effect, because moveDirection.y is zeroed each Update. To avoid this problem, store the vertical speed in a separate member variable, and assign it to moveDirection.y prior to move the character. Extra feature: you can store the CharacterController in another member variable (variable declared outside any function) only once, what will speed things a little. The whole thing is:

var speed : float = 6.0; var jumpSpeed : float = 8.0; var turnSpeed : float = 60; var gravity : float = 20.0;

private var moveDirection : Vector3 = Vector3.zero; private var vSpeed : float = 0; // keep vertical speed in a separate variable private var controller: CharacterController; // controller reference

function Update() { if (!controller) controller = GetComponent(CharacterController); var turn: float = Input.GetAxis("Horizontal"); transform.Rotate(0, turn turnSpeed Time.deltaTime, 0); moveDirection = transform.forward Input.GetAxis("Vertical") speed; if (controller.isGrounded) { vSpeed = 0; // a grounded character has zero vert speed unless... if (Input.GetButton ("Jump")) { // unless Jump is pressed! vSpeed = jumpSpeed; } } // Apply gravity vSpeed -= gravity Time.deltaTime; moveDirection.y = vSpeed; // include vertical speed // Move the controller controller.Move(moveDirection Time.deltaTime); }

Comment
Add comment · Show 5 · 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 CaptainIcy281 · Dec 18, 2011 at 12:20 AM 0
Share

Ah, that worked. I understand the problem/solution and didn't just copy your code for no reason. Just so you know your intelligent reply wasn't wasted.

Thank you so much. :)

avatar image aldonaletto · Dec 18, 2011 at 12:56 AM 0
Share

Congratulations, that's the spirit: understand the idea and implement it yourself!
By the way, please click the "check" button below the voting thumbs to mark this answer as accepted - this will help others to find the solution for similar problems.

avatar image Sazem · Jan 22, 2013 at 10:28 PM 0
Share

I was fighting also with this gravity problem for few days. With little tweaking I got it working and now I can make mid air jumps in my 2d jumper. I couldnt do it without this sample! Thank you :)

avatar image _Shockwave · Feb 09, 2014 at 02:28 AM 0
Share

Dude, Thank you so much! You saved me! I was trying to figure this out for weeks!

avatar image Michael_93 · Mar 12, 2014 at 11:32 PM 0
Share

thank you so much for this. It's amazing that not knowing a concept can eat hours out of your work.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Player movement 0 Answers

A better way to do jumping? 2 Answers

Lerp & jumping with sin/cos 1 Answer

No movement when animation plays (2d) 1 Answer


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