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 pappalardodd · May 20, 2020 at 03:13 PM · scripting problemphysicsrigidbody.addforcerigidbody.velocity

Correctly move rigidbody at constant speed

this is currently my code. The object moves at the desired speed but using [rb.velocity = movement * speed] if it jump it remains glued to the ground. I can't understand how I can fix things.

 private Vector3 movement;
 private float speed = 3;
 public bool isGrounded;
 
 void Start()
 
 rb = GetComponent<Rigidbody>();
 
 void Update()
 {
 float Horizontal = Input.GetAxis("Horizontal");
 float Vertical = Input.GetAxis("Vertical");
 movement = transform.right * Horizontal + transform.forward * Vertical;
 float origMagnitude = movement.magnitude;
 movement.y = 0.0f;
 movement = movement.normalized * origMagnitude;
 
 if(isGrounded && Input.GetKeyDown(KeyCode.Space))
 rb.AddForce((Vector3.up + movement) * JumpForce, ForceMode.Impulse);
 
 RaycastHit hit;
 if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, rayDistance, layer)) isGrounded = true;
 else isGrounded = false;
 }
 
 private void FixedUpdate ()
 {      
      rb.velocity = movement * speed;
 }
Comment
Add comment · Show 4
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 andrew-lukasik · May 20, 2020 at 03:34 PM 0
Share

You need to choose either AddForce or velocity and just stick to it. Using both doesn't mix well.

avatar image pappalardodd andrew-lukasik · May 20, 2020 at 03:43 PM 0
Share

I'd like to add force, but I don't know how to move the object at the same speed with AddForce. Can you show me

avatar image andrew-lukasik pappalardodd · May 20, 2020 at 04:00 PM 0
Share

What about just rb.AddForce( movement * forcePerSecond * Time.fixedDeltaTime );?

Show more comments

1 Reply

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

Answer by tadadosi · May 20, 2020 at 04:48 PM

By doing movement.y = 0.0f and passing the entire movement Vector3 to your rb.velocity, you are basically denying your rigidbody physics on Y axis, this is what you should do:


 using UnityEngine;
 
 public class pappaController : MonoBehaviour
 {
     public float speed = 10f;
     public float jumpForce = 5f;
     public float rayDistance = 5f;
     public LayerMask WhatIsGround;
 
     private Rigidbody rb;
     private Vector3 movement;
     private bool isGrounded;
 
     void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     void Update()
     {
         float Horizontal = Input.GetAxis("Horizontal");
         float Vertical = Input.GetAxis("Vertical");            
         movement = (transform.right * Horizontal + transform.forward * Vertical) * speed; // multiply by speed and you got your movement ready
         // removed this part, don't know what was the point of it
 
         if (isGrounded && Input.GetKeyDown(KeyCode.Space))
             rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
 
         RaycastHit hit;
         isGrounded = Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, rayDistance, WhatIsGround);
     }
 
     private void FixedUpdate()
     {
         // y axis -> rb.velocity.y
         rb.velocity = new Vector3(movement.x , rb.velocity.y, movement.z);
     }
 }
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 pappalardodd · May 20, 2020 at 07:29 PM 0
Share

Thanks so much! (the only thing I think is, in this way along the X and Z axis the object will not be subject to external forces .. if for example it is hit by a car the object however will not move undergoing the impact) . That's why I wanted to use addForce but at this point I don't think it's possible to move the object at a fixed speed.

avatar image tadadosi pappalardodd · May 20, 2020 at 07:54 PM 0
Share

If you want to just use AddForce, you could leave the velocity untouched and clamp it to a max velocity by doing something like:


 if (velocity.magnitude > maxVelocity)
     velocity *= 0.99f; // $$anonymous$$ake this a variable and play with its value to get different clamping results.


Or you could set a condition to make your object react to the impact, it could be a bool and a timer with a fixed duration, if the object is hit you make the player lose control for a short time and allow the velocity to be handled by Unity.

avatar image pappalardodd tadadosi · May 20, 2020 at 08:39 PM 0
Share

I was unable to apply your clamp idea with a script, I tried this script, and it works but the speed fluctuates too much:

 if(rb.velocity.sqr$$anonymous$$agnitude < speed)
       rb.AddForce(movement * speed, Force$$anonymous$$ode.VelocityChange);

By bool you mean something like::

 if (hit) rb.velocity = new Vector3 (rb.velocity.x, rb.velocity.y, rb.velocity.z);

could it work with a timer?

Show more comments

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

286 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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.velocity seems to be breaking jumping physics 0 Answers

Moving rigidbody (Player) with addForce or Velocity ? 1 Answer

VR Sword Physics, how to add force or velocity to the sword for impact when it hits an object 1 Answer

How to allow the player to change the mass/force of an object in the UI? Noob question 2 Answers

How to make Jump() framerate independent? 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