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 RoyaleNite · May 08, 2019 at 08:08 PM · movementrigidbodyjumping

Smoother movement and jumping with rigidbodies

I keep trying to make smoother movement and jumping with a rigidbody but wither the jumping stops working or the movement isn't smooth. And the charactercontroller is not working like I want it to.

     private Vector3 move;
     public float velY, gravity, jumpSpeed;
     public BoxCollider col;
     public float speed;
 
     public Rigidbody playerR;
     public static bool jumped;
     public Joystick joystick;
 
     public void Start()
     {
         jumpSpeed = 7;
         gravity = 7;
         speed = 4;
         velY = 5;
     }
 
     void Update()
     {
 
         if (IsGrounded())
         {
             velY -= gravity * Time.deltaTime;
         }
 
         Vector3 move = Vector3.zero;
         move.x = Input.GetAxisRaw("Horizontal") * speed;
         move.y = velY;
         move.z = Input.GetAxisRaw("Vertical") * speed;
 
         if (joystick.InputDirection != Vector3.zero)
         {
             move.x = joystick.InputDirection.x * speed;
             move.z = joystick.InputDirection.z * speed;
             move.y = 0;
         }
 
         playerR.velocity = move;
 
     }
 
     public bool IsGrounded()
     {
         int layerMask = LayerMask.GetMask("Platform");
         return Physics.CheckBox(col.bounds.center, new Vector3(col.bounds.center.x, col.bounds.min.y, col.bounds.center.z), col.transform.rotation, layerMask);
     }

Comment
Add comment · Show 1
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 Temseii · May 08, 2019 at 08:21 PM 0
Share

Few things:

Duplication is bad. Ins$$anonymous$$d of multiplying every variable by 'speed', just do it once where you're setting your velocity.

          Vector3 move = Vector3.zero;
          move.x = Input.GetAxisRaw("Horizontal");
          move.y = velY;
          move.z = Input.GetAxisRaw("Vertical");
  
          if (joystick.InputDirection != Vector3.zero)
          {
              move.x = joystick.InputDirection.x;
              move.z = joystick.InputDirection.z;
              move.y = 0;
          }
  
          playerR.velocity = move * speed;
 

You should also handle physics in FixedUpdate ins$$anonymous$$d of Update. Just move playerR.velocity = move; out of Update into FixedUpdate.

To fix the issues you brought up, some more details would be helpful. How isn't it smooth and how isn't it working like you want it to?

1 Reply

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

Answer by Cornelis-de-Jager · May 08, 2019 at 09:33 PM

If you want smoother movement then its best to let Unity handle velocity. Rather than setting the velocity just add force.

 Rigidbody rb;
 
 public float jumpStrength;
 
 void Start () {
     rb = GetComponent<Rigidbody>();
 }
 
 void LateUpdate () {
     // Handle the movement
     HandleMovement();
     
     // Handle Jumping
     HandleJump();
 }
 
 void HandleMovement () {
 
     // Get the movement vector
     Vector3 move = new Vector3 (Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
     move *= speed * Time.DeltaTime;
     
     // Use ForceMode to make it smoother
     // Force is the default but you can also try
     // Acceleration & VelocityChange a bit more sharper movement
     // We must also ensure that we don't add force to exceed max velocity/speed    
     var movementPlane = new Vector3 (rb.velocity.x, 0, rb.velocity.z);
     if (movementPlane.magnitude < speed)
         rb.AddForce(move, ForceMode.Force);
     
 }
 
 void HandleJump () {
     
     // For jumping we do something similar with force
     // However we want it instant so we can use the Impulse force mode
     if (Grounded()){
         rb.AddForce(new Vector3 (0, jumpStrength, 0), ForceMode.Impulse);
     }
 }
Comment
Add comment · Show 2 · 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 RoyaleNite · May 09, 2019 at 07:25 PM 0
Share

This works but my jumping still looks laggy, i think its the way my camera moves that's causing it

avatar image Cornelis-de-Jager RoyaleNite · May 09, 2019 at 09:19 PM 0
Share

@RoyaleNite below is a script I use for smooth camera movement.

 public class CameraScript : $$anonymous$$onoBehaviour {
 
     public Transform player;
     public Vector3 offset;
     public float delay = 1f;
     
     void Start () {
         offset = transform.position - player.position;
     }
     
     void LateUpdate () {
         transform.position = Vector3.Lerp(transform.position, transform.position + offset, delay);
     }
 }

Its a very simple script but works very well. If you want it smoother then simply increase the delay. If you want to make it more advance to add zoo$$anonymous$$g and rotation around the player, simply change the offset variable to whatever you like.

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

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

Rigidbody--Addforce on a Spherical Platform(A Globe) 2 Answers

Help with player movement and jumping 0 Answers

I need help with a movement script 1 Answer

Rigidbody.Addforce makes object skip 5 Answers

Slope Limit on Rigidbody First Person Controller 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