# Floating Problem With Rigidbody.Velocity in Unity

Discussion in 'Scripting' started by muzamilsajid, Nov 4, 2019.

1. ### muzamilsajid

Joined:
Oct 28, 2019
Posts:
8
Below is the code that I am using to move an object in Unity. The rb.Velocity line makes my object float in in Gameplay mode. If I comment out the line then the object falls just fine.

Could someone explain whats happening here?

Code (CSharp):
1. public class PlayerController : MonoBehaviour
2. {
3.
4.     public float forwardVelocity = 0F;
5.     public float maxSpeed = 180;
6.     public float acceleratePerSecond = 8.0F;
7.     public float rotateSpeed = 3.0F;
8.     private float yaw = 0.0f;
9.     private float pitch = 0.0f;
10.     protected Rigidbody rb;
11.     float timeZeroToMax = 2.5F;
12.
13.     // Start is called before the first frame update
14.     void Start()
15.     {
16.         rb = GetComponent<Rigidbody>();
17.         acceleratePerSecond = maxSpeed / timeZeroToMax;
18.         forwardVelocity = 0F;
19.
20.     }
21.
22.     // Update is called once per frame
23.     void Update()
24.     {
25.
26.             if (Input.GetKey(KeyCode.UpArrow)) //Accelerate The Vehicle
27.             {
28.                 if (forwardVelocity< maxSpeed)
29.                 {
30.                 forwardVelocity += acceleratePerSecond * Time.deltaTime;
31.
32.                 }
33.
34.             }
35.
36.
37.         forwardVelocity = Mathf.Min(forwardVelocity, maxSpeed);
38.
39.
40.         rb.velocity = transform.forward * forwardVelocity;
41.
42.
43.         transform.Rotate(0, Input.GetAxis("Mouse X") * rotateSpeed, 0);
44.
45.
46.         yaw += rotateSpeed * Input.GetAxis("Mouse X");
47.
48.
49.         transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
50.     }
51.
52. }
53.

2. ### LilFire

Joined:
Jul 11, 2017
Posts:
63
since forwardVelocity = 0, rb.velocity = 0

SparrowsNest likes this.
3. ### SparrowsNest

Joined:
Apr 6, 2017
Posts:
2,028
Gravity acts (if you don't change in) in the Y axis of the velocity vector, you're constantly overriding the gravity by setting it to 0 (assuming your object is not looking up or down) or where ever this vector ends up pointing to.

You need to incorparate the previous fall speed into the new velocity, or stop overriding the velocity and add to it instead of setting it.

4. ### muzamilsajid

Joined:
Oct 28, 2019
Posts:
8
Mor elaboration over here would be highly appreciated.

5. ### StarManta

Joined:
Oct 23, 2006
Posts:
8,010
More elaboration of what? @SparrowsNest 's comment is pretty much the whole story. What do you need help understanding?

SparrowsNest likes this.
6. ### SparrowsNest

Joined:
Apr 6, 2017
Posts:
2,028
Saying what isn't clear to you will probably help us explain it better. (;

Joe-Censored likes this.
7. ### Joe-Censored

Joined:
Mar 26, 2013
Posts:
9,614
Gravity affects velocity, but on line 40 you are setting velocity directly, thereby erasing the effects of gravity.

SparrowsNest likes this.
8. ### muzamilsajid

Joined:
Oct 28, 2019
Posts:
8
Ok, so what is the approach that i would use over here?

I tried setting a bool var which checks whether the Object is grounded in the OnCollissionEnter method.

If the object is grounded then only will the code run. but even that doesn't work.

Or should i be doing something else apart from "rb.velocity = transform.forward"??

9. ### muzamilsajid

Joined:
Oct 28, 2019
Posts:
8
Ok guys, one last question though no one bothered to reply to my last post.

Maybe i do not understand vectors fully. From my knowledge a vector multiplied by another vector gives u a result of the by product eg:

v1 (1,2,3) x v2(2,2,2) = v3(2,4,6). I hope i am correct.

but what is the result of this:

v1(0,0,1) * 2; ????