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 Reverend-Speed · Jun 17, 2014 at 02:17 PM · charactercontrollergravityjumpisgrounded

Why does my characterController code not apply gravity correctly?

I'm using a characterController and while movement on the ground is just dandy, jumping and gravity are somewhat loopy.

1) Hitting jump while stationary behaves as expected - Starting at 1.08 in y, the character rises to roughly 2.5, then falls back to 1.08. However, jumping like this and then holding a direction causes the character to glide slowwwly back to ground level. Releasing a direction key causes gravity to kick back in and the character drops to ground. Not expected behaviour.

2) Hitting jump while moving results in my character hopping up a tiny increment, reaching 1.20, then quickly falling to 1.08 (ground level).

I've stared at my code until I can no longer understand any of it. I'm hoping for a solution... can anybody see where I'm going wrong?

 void FixedUpdate () {
 
         CharacterController controller = GetComponent<CharacterController> ();
 
         camForward = Vector3.Scale (camFieldSouth.forward, new Vector3 (1, 0, 1)).normalized;
 
         float inputX = Input.GetAxis ("Horizontal");
         float inputY = Input.GetAxis ("Vertical");
 
 
                 
         if(controller.isGrounded){
 
             moveDirection = new Vector3(inputX, -antiBumpFactor, inputY);
             moveDirection = moveDirection.z * camForward + moveDirection.x * camFieldSouth.right;
             if(moveDirection.magnitude > 1)moveDirection.Normalize();
             // moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
 
             if(Input.GetButton("Fire1")){
                 moveDirection.y = jumpSpeed;
                 jumped = true;
             }
         }else {
             // Check movement but don't touch the y component
             Vector3 camMoveRef = new Vector3(inputX, -antiBumpFactor, inputY);
             camMoveRef = camMoveRef.z * camForward + camMoveRef.x * camFieldSouth.right;
             // camMoveRef.y = 0.0f;
             if(camMoveRef.magnitude > 1)moveDirection.Normalize();
             moveDirection.x = camMoveRef.x * speed; // If this is just inputX * speed, it works fine - but not cam relative.
             moveDirection.z = camMoveRef.z * speed; // If this is just inputY * speed, it works fine - but not cam relative.
             Debug.Log (moveDirection.y);
             // moveDirection = transform.TransformDirection(moveDirection);
             jumped = false;
         }
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move (moveDirection * Time.deltaTime);
 
         // NPC movement code
         // Trimming the position List to remain with set size
         if (positionRecord.Count > positionRecordSize) {
             positionRecord.RemoveAt(0);
         }

Further information on request. Thanks for your time, folks.

--Rev

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

0 Replies

· Add your reply
  • Sort: 

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

21 People are following this question.

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

Related Questions

Jumping Stopped Working. 2 Answers

CharacterController isGrounded toggles value 2 Answers

Adding gravity to character and grounded checks are not working? 0 Answers

How do you make a characterController jump automatically when it reaches the edge of a platform? 0 Answers

Getting this character controll script working right 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