Get horizontal velocity of an object
how can i set float x for example for only the horizontal velocity of an object? in C# btw
Comment
Answer by Chikari · Feb 13, 2017 at 02:28 PM
Vector3 tempVector = GetComponent<Rigidbody>().velocity;
tempVector.x = 10f;
GetComponent<Rigidbody>().velocity = tempVector;
Answer by UnityCoach · Feb 14, 2017 at 12:32 PM
If what you want is to "read" the horizontal velocity of an object, you may simply cast it to a Vector2.
((Vector2) GetComponent<Rigidbody>().velocity).x
Your answer
![](https://koobas.hobune.stream/wayback/20220612111001im_/https://answers.unity.com/themes/thub/images/avi.jpg)