Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Jeye · Feb 12, 2019 at 12:45 AM · charactercontrollergravitymovement script

What have I done wrong with gravity and movement?

Hi, so I've been having a couple of problems with this script. I got player movement working and have been trying to add a jump.


  • The first problem is the jump isn't smooth. The object shoots up in what seems like 1 frame and then falls down.

  • Second is that the jump doesn't work when standing still.


A couple of things I've managed to figure out is that when standing still the controller doesn't read as grounded. Also that when i take out the Transform.translate line at the bottom it makes the controller be grounded when still, but then it can only move in x and z when off the ground. Thanks.

     public float speeed = 8f;
     private float verticalVelocity;
     private float jumpForce = 40f;
     private float gravity = 50f;
     private Vector3 movement;
     private Vector3 moveRotation;
     
     void Update()
     {
 
         CharacterController controller = gameObject.GetComponent<CharacterController>();
         
 
         float moveH = Input.GetAxis("Horizontal");
         float moveV = Input.GetAxis("Vertical");
         movement = new Vector3(moveH, verticalVelocity, moveV);
         moveRotation = new Vector3(moveH, 0, moveV);
 
 
 
         verticalVelocity = -gravity * Time.deltaTime;

         //JUMP
         if (controller.isGrounded)
         {
             
             print("CharacterController is grounded");
             verticalVelocity = -gravity * Time.deltaTime;
             if (Input.GetButtonDown("Jump"))
             {
 
                 verticalVelocity = jumpForce;
             }
 
         }
 
         //character rotation
         if (moveRotation != Vector3.zero)
         {
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(moveRotation), 0.15f);
         }
 
 
         //move
         // this line is what makes the controller not be grounded when standing still
         transform.Translate(movement * speeed * Time.deltaTime, Space.World); 
 
         controller.Move(movement * speeed * Time.deltaTime);
         
     }





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 TreyH · Feb 12, 2019 at 02:43 PM 0
Share

I don't know what CharacterController.$$anonymous$$ove does, but is there a reason you're using it and Transform.Translate back to back?

avatar image Jeye TreyH · Feb 23, 2019 at 06:59 PM 0
Share

Controller.move moves the controller, and stops when there's collisions.

I'm not sure how I ended up using translate.transform as well, but I think using translate.transform allows the player to move, ins$$anonymous$$d of constantly colliding with the floor.

For the I've now used raycast to detect the floor which works fine and now allows me to use jump when standing still. But my jump still teleports the player up and then gravity pulls it back down.

3 Replies

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

Answer by Jeye · Feb 23, 2019 at 07:03 PM

Rather than using isGrounded, I've used a physics.raycast to detect the floor which allows you to jump when standing still. But I'm still having problems with a smooth jump.

When the jump button is pressed the player teleport's upwards and the gravity gradually pulls them back down. Its probably something to do with how I've coded gravity.

Thanks for the help so far. Any more would be appreciated :)

Comment
Add comment · 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
0

Answer by James_BadAlchemy · Feb 12, 2019 at 01:28 AM

Have you tried just using unity's physics engine. By default unity applies realistic gravity.

Comment
Add comment · Show 1 · 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 Jeye · Feb 12, 2019 at 01:33 PM 0
Share

You mean with rigidbody? If yes then I have tried that.

avatar image
0

Answer by umair94 · Feb 12, 2019 at 05:50 AM

i think if you just add rigidbody to the object . then jump on button down and change y-axis like if(Input.GetKeyDown(KeyCode.Space)){ transform.GetComponent().velocity = Vector3.up * speed; }

Comment
Add comment · Show 4 · 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 Jeye · Feb 12, 2019 at 01:32 PM 0
Share

I've tried using a rigid body a couple of times but got worse results so reverted back to this. I'd like to try any figure out how to fix without rigid body. Not sure how possible that is though.

avatar image blueshark- Jeye · Feb 12, 2019 at 02:05 PM 0
Share

What doesn't work with the rigidbodies?

avatar image Jeye blueshark- · Feb 12, 2019 at 03:08 PM 0
Share

I may have used it incorrectly but I would do similar to this:
if (Input.GetButtonDown("Jump")) { rigidbody.velocity = Vector3.up * jumpForce; } Though just adding the rigid body to the game object makes the player very slow when on the ground and eventually clip through the ground. With no other force being applied.

(edit. shaking is from my camera script ignore that)

avatar image triis4924 Jeye · Feb 12, 2019 at 02:49 PM 0
Share

if you do something with physic I would highly recommend rigid body's

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

124 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 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 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 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 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 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

Charactercontroller won't go down, only goes up 1 Answer

controller.Move doesn't stay grounded when walking down slope 3 Answers

Strange Character Controller Behavior Caused by Simulated Gravity and Ground Check 0 Answers

why does character controller accelerate off ledges? 1 Answer

Change gravity point on trigger 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