Show which direction the Rigidbody is accelerating in?

Hello. I have a rigidbody that I can AddForce to and change its speed, etc. I have not been able to figure out how you can make a little line or arrow that shows what direction you are travelling in. For example I can be going 5m/s straight up, and still rotate the player. It’s all done in zero gravity so the only way to slow down is to accelerate in the opposite direction you are moving. However I want a little arrow or something that shows the direction you are moving, that way people can turn 180 degrees and use that arrow as a reference to slowing down. The arrow will need to update in real time so if you suddenly change directions, you know which direction you need to be facing so you can slow down.

I hope this makes sense. Let me know if you need me to clarify.
I appreciate your help!

Thanks

2 Answers

2

If you want it for testing purposes there is debug drawline that is working only in editor Unity - Scripting API: Debug.DrawLine

For that start point would be transform.position and direction vector3.forward (I have not tested this so try it yourself)

Sadly it's not for debugging.

The value of rigidbody.velocity will tell you exactly which way the object is moving. velocity is ‘speed in direction’ (vector). Speed is ‘velocity without direction’ (scalar).

So how could I for example take a line renderer and make it point towards the velocity? I can't think of what I would do next.

With your Line Renderer, draw your line from the object.position to the point rigidbody.velocity. It is relative to your object and the length of the line will indicate the speed. Normalise it for a unit vector. Side note. I can't write the word 'unit' these days without deleting a y from the end of it.

I used this: VelocityPointer.SetPosition(0, transform.position); VelocityPointer.SetPosition(1, rigidbody.velocity); And it worked fine, but I'm not wanting to change the length of the line, I'm wanting it to point in the direction the player is accelerating. So if the player is moving up. I want the line to point up aswell. I want the length of the line to stay the same, not change. If the character is moving left, I want the line to point left, etc. Here is a picture I made quickly to explain it a little better: [18222-unity+arrow+line.jpg|18222] I appreciate your help so far! Thank you!

The velocity vector is already pointing in the right direction. Feed it into your line renderer and Normalise it to make it always = 1 unit long :)

Im typing this: VelocityPointer.SetPosition(1, rigidbody.velocity.Normalize); and getting this error: Assets/Scripts/Movement.js(52,36): BCE0017: The best overload for the method 'UnityEngine.LineRenderer.SetPosition(int, UnityEngine.Vector3)' is not compatible with the argument list '(int, function(UnityEngine.Vector3): UnityEngine.Vector3)'.