Velocity X and Y speed limits
I've looked around and tried many of the answers but still can't quite get what I want. I have many gameObject prefabs with rigidBody2d.
I apply a force to get them moving in their physical world but things get out of hand if they go too fast from various collisions. I'm looking to have min and max speed limits for both the x and y velocities (which can be negative).
Ideally the objects should never be able to go horizontally across or vertically up/down because the speed limits will kick in and can't get stuck stationary.
I first tried:
if(rb.velocity.magnitude > maxSpeed) {
float reduction = maxSpeed/rb.velocity.magnitude;
rb.velocity *= reduction;
}
if(rb.velocity.magnitude < minSpeed) {
float increase = minSpeed/rb.velocity.magnitude;
rb.velocity *= increase;
}
But didn't work. Think vectors are beating my brain up. Thanks for any help.
Answer by Salmjak · Feb 15, 2016 at 08:50 PM
I suppose you already tried: rb.velocity.x/y = minSpeed/maxSpeed? Ex.
if(rb.velocity.y < minSpeedY){
Vector2 newVelocity = new Vector3(rigidbody.velocity.x, minSpeedY);
rb.velocity = newVelocity;
}
EDIT: Ok, let's do the math. What does Vector2.magnitude do? It gets the magnitude which is the same as the Hypotenuse of the corresponding triangle. What does multiplying a vector2 do? It multiplies each component by the factor.
So you try to solve the equation sqrt(x*x + y*y)*q = minSpeed.
Let's say minSpeed = 20 and our Vector is (2,1). This will give us the equation: sqrt(4)*q = 20. So q= 20/2 = 10.
When you then multiply your vector you get a vector which is (20,10) the magnitude of this is sqrt(20*20 + 10*10) = sqrt(400 + 100) = sqrt(500) ~= 22.
So you didn't get 20 as you wanted.
What you really want to solve is: 20*20 = x*q*x*q + y*q*y*q.
So if the vector2 was (2,1) this would get us 400 = 4q^2 + q^2 = 5*(q^2). So we would get q = sqrt(400/5) ~= 8.94.
If we now multiply our vector we would get (17.88,8.94) and the magnitude of this would be sqrt(17.88^2 + 8.94^2) ~= sqrt(320 + 79.9) = sqrt(399.9) = 19.99 ~= 20!
Hurray!
So in your code the equation would be:
float increase = Mathf.sqrt((minSpeed*minSpeed)/((rb.velocity.x*rb.velocity.x) +(rb.velocity.y*rb.velocity.y))); //We just break out q from our equation "20*20 = x*q*x*q + y*q*y*q" => "20^2 = x^2*q^2 + y^2*q^2" => "20^2 = (x^2 + y^2)*q^2" => sqrt(20^2/(x^2 + y^2)) = q
Yeah, it doesn't work because y can be negative too - so looking to limit ranges of, for example y can be -0.5 to -1.5 OR 0.5 to 1.5
I'm sure there is some function that I don't know about that does this for you, but there you go.
Thanks for the breakdown, I think I'll need to sleep on it as struggling to follow.
Just copy the last code-snippet and see if it works. What it should do: when your velocity is greater/less than maxSpeed/$$anonymous$$Speed it should scale up/down the vector without changing the direction.
Like this?:
if(rb.velocity.magnitude < $$anonymous$$Speed) {
float increase = $$anonymous$$athf.Sqrt(($$anonymous$$Speed*$$anonymous$$Speed)/((rb.velocity.x*rb.velocity.x) +(rb.velocity.y*rb.velocity.y)));
rb.velocity *= increase;
}
I get this:
Rigidbody2D.velocity assign attempt for 'blueBubble(Clone)' is not valid. Input velocity is { NaN, NaN }.
UnityEngine.Rigidbody2D:set_velocity(Vector2)
BlueBubble:FixedUpdate() (at Assets/Scripts/InGame/BlueBubble.cs:59)
I guess because 0's can enter the equation
Yeah. I would also guess that. If both x and y vectors are 0 it would cause a division by zero. Try checking for zero (if(rb.velocity.x != 0 || rb.velocity.y != 0), so if one of them isn't zero you can continue).