- Home /
Getting speed without Rigidbody.velocity.magnitude
Hi there! I'm working on a game where the longer the player stays moving the more they accelerate and I made this code to to check if player is moving above a certain speed.
void FixedUpdate ()
{
if (player.velocity.magnitude >= 2)
{
print("true" + player.velocity.magnitude);
player.AddRelativeForce(Vector3.forward * 1.5f * Time.deltaTime);
}
else
{
print("false" + player.velocity.magnitude);
}
}
However player.velocity.magnitude always returns zero (I've also checked if player is equal to the correct Rigidbody and it is)
I searched a bit online and apparently it's something about the FPCharacter altering the speed directly so the Rigidbody doesn't know the speed, yet none of them gave an alternative of checking the speed. Is there even a way to do this?
All help appreciated :D
You can just do this numerically. Speed is given in units of distance over time, so you'll want to make a similar calculation. There are more accurate ways to do this, but the simplest (which will be expanded a bit) would just be:
private Vector3 previousPosition;
// Use this for initialization
void Start ()
{
previousPosition = this.transform.position;
}
// Physics routine
void FixedUpdate()
{
// Test it
Debug.Log("Est.Speed: " + this.EstimatedSpeed( Time.fixedDeltaTime).ToString("F1"));
}
private float EstimatedSpeed(float timePassed)
{
// For completeness, we'll get the velocity vector itself, which is the distance travelled
// in each direction divided by the time passed.
Vector3 velocityVector = (this.transform.position - this.previousPosition) / timePassed;
// Get the magnitude of thie vector
float velocity$$anonymous$$agnitude = velocityVector.magnitude;
// $$anonymous$$ark this position for next time
this.previousPosition = this.transform.position;
// Then just return what we calculated
return velocity$$anonymous$$agnitude;
}
Answer by AurimasBlazulionis · Nov 07, 2016 at 04:25 PM
You need to store position of the object one frame before and use some something similar like this (transform.position - oldPosition).magnitude / Time.deltaTime
or fixedDeltaTime
if you wish to do everything in FixedUpdate.
Few notes:
The speed you get is in world space. You will probably need to do
Mathf.Abs(theSpeedCalculation)
to get it without negative numbers. And also, you might need not to take Y axis in to account. To do that, just set the Y position of the old position to the current Y position.normalized speed calculation is quite expensive. It computes a cubic root everytime. If you just need to check if the speed is higher than specific value and do not need to use speed in other calculations, you can use
sqrMagnitude
instead ofmagnitude
to get a non cubic rooted value. Then compare speed to the value cubed or multiplied 3 times together. This will achieve what you need.
Norms give positive numbers, there is no need to take the absolute value of a magnitude, as it takes the square root of squared real numbers.
Normalization is a square root, as distance in R3 (the world Unity defaults to) is an L2 norm.
Thanks for pointing out a mistake, I meant magnitude ins$$anonymous$$d of normalized.
Your answer
Follow this Question
Related Questions
Increase speed of an object's fall 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
How to limit an object's position to a spherical radius? 0 Answers