# Question how to limit velocity of ball

Discussion in 'Physics' started by jermainetai0307, Apr 9, 2023.

1. ### jermainetai0307

Joined:
Nov 10, 2022
Posts:
25
i have a ball moving along the x and z axis only, i tried to clamp the velocity of it but doesn't do anything.
Code (CSharp):
1. public class ballmove : MonoBehaviour
2.     {
3.     // Drag and drop Rigidbody in Inspector
4.     public Rigidbody rb;
5.     public Vector3 velocity;
6.     public float bounce;
7.
8.
9.     void Start()
10.     {
11.         rb = GetComponent<Rigidbody>();
12.         // Add force once at start
13.         rb.AddForce((Vector3.forward + Vector3.left) * 10.0f, ForceMode.VelocityChange);
14.
15.     }
16.
17.     void Update()
18.     {
19.         // Track velocity, it holds magnitude and direction (for collision math)
20.         velocity = rb.velocity;
21.         Debug.Log("velocity:" + rb.velocity);
22.         //Debug.Log("direction" + direction.magnitude);
23.
24.     }
25.
26.     private void FixedUpdate()
27.     {
28.     }
29.
30.     void OnCollisionEnter(Collision collision)
31.     {
32.         // Reflect params must be normalized so we get new direction
33.         Vector3 direction = Vector3.Reflect(velocity.normalized, collision.contacts[0].normal);
34.
35.         // Magnitude of the velocity vector is speed of the object (we will use it for constant speed so object never stop)
36.         float speed = (velocity.magnitude * 1.05f);
37.
38.         // Like earlier wrote: velocity vector is magnitude (speed) and direction (a new one)
39.         rb.velocity = direction.normalized * speed;
40.
41.         if (collision.gameObject.tag == "deathwall")
42.         {
43.             Application.Quit();
44.         }
45.     }
46.
47. }
what I have tried is
Mathf.Clamp(rb.velocity.z,80,-80)
something like this with the x velocity too, under Update(), but it doesn't seem to work at all.

2. ### DevDunk

Joined:
Feb 13, 2020
Posts:
4,214
Define does not work?
Did you use rb.velocity = mathf.clamp.....

3. ### jermainetai0307

Joined:
Nov 10, 2022
Posts:
25
hmm im not sure how im suppose to do that, Im getting errors.
rb.velocity = Mathf.Clamp(rb.velocity, 80, 80);
this will show the error under the second rb.velocity by saying cannot implicitly change vector to float while
rb.velocity.z = Mathf.Clamp(rb.velocity, 80, 80);
this adds on to the problem saying the first one cannot be modified because it is not a variable
rb.velocity = Mathf.Clamp(rb.velocity.z, 80, 80);
if i do it like this it will underline the whole Mathf.Clamp and that it cannot convert float to vector

### Unity Technologies

Joined:
May 24, 2013
Posts:
10,209
That method clamps a float but are asking it to somehow magically work with a Vector3 which is three floats. Clearly that won't work.

The next issue is that you're assigning the Z which cannot work; it's a value-type and for reasons explained in lots of C# docs/tutorials, you cannot do that. Look up the difference between C# reference and values types and how they work.

In the end, you have a Vector3 so look at the docs. A quick look at that type shows you this Vector3.ClampMagnitude which will do what you want; take a Vector3 and clamp its magnitude.

Hope that helps.

jermainetai0307 likes this.