Hello. Following Project Boost lessons on Udemy has got me to make a game where a missile has to be going at its top speed to blow up a bomb at the end of the level.
I thought using Vector3 and magnitude or velocity would give me a reading, that the faster my rocket goes, the higher the number it returns.
After giving it a shot, firstly I found negative numbers being shown. Then after using Math.Abs I noticed that I was getting readings that varied from 0.1 to 0.3 when acting out the same action. So I was getting different results.
The only way I knew was to average out the X and Y velocities by dividing by two. But still when the rocket goes fast I get a result that can vary from 0.6 to 10.0.
float X = this.rigidBody.velocity.x;
float Y = this.rigidBody.velocity.y;
float absX = Math.Abs(X);
float absY = Math.Abs(Y);
float mag = (absX + absY) / 2f;
Anybody with a solution to finding out if the rocket is going fast, or slower than the bomb at the end needs?
You want the magnitude of the velocity vector. rigidBody.velocity.magnitude This removes the direction of velocity in multiple dimensions and only considers the single number of speed. Your mag calculation is not correct; the magnitude is the sqrt(x*x+y*y+z*z).
Thanks halley… awesome picture 
It doesn’t seem that my results are what I expect still. I fly the rocket around my map and crash into walls. When the rocket crashes Debug.Log shows me its magnitude.
When testing this, if I spun my rocket while crashing it added extra force and upped the magnitude. But after repeating the same crash (up, across, spin around and hit the floor)… the magnitude varied from 0.4 to 3.0.
This doesn’t give me consistency. If I wanted the rocket to crash into a bomb at the end of the level, one crash would not register above (say 3.0 magnitude) what I’ve set, yet one would. It wouldn’t be very fair to the player.
private void KillPlayer()
{
float mag = this.rigidBody.velocity.magnitude;
Debug.Log(mag);
state = State.Dying;
audioSource.Stop();
mainEngineParticles.Stop();
audioSource.PlayOneShot(deathSound);
deathSoundParticles.Play();
Invoke("LoadFirstLevel", 2f);
}
We’re gonna need to see more about how you’re moving this object around, then. Debug.Log will often round numbers to very few digits excessively, but shouldn’t confuse 0.3 with 4.0.
void RespondToThrustInput()
{
if (Input.GetKey(KeyCode.Space))
{
rigidBody.AddRelativeForce(Vector3.up * thrustSpeed * Time.deltaTime);
}
private void RespondToRotateInput()
{
float rotationThisFrame = turningSpeed * Time.deltaTime;
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.forward * rotationThisFrame);
}
else if (Input.GetKey(KeyCode.D))
{
transform.Rotate(-Vector3.forward * rotationThisFrame);
}
}
Don’t sweat it too much. I think the forums can be seen as an answer base, when really you need to spend more time in Unity. It’s like I want the answer now. But I’ll stumble across a solution in a year’s time 