I’ve been tinkering with the Tanks! tutorial file and have been trying to make the tanks use the mouse to aim, firing and facing in the direction of the cursor, I’ve managed to do this using the code below but the result isn’t exactly what I had hoped for (the cursor controls the tank and it faces but its not overly accurate). pic to illustrate.
I think you should use Camera.ScreenPointToRay and Physics.Raycast to project the position of the mouse onto the actual 3D scene. Use layers so the raycast hits the ground only (no tanks nor buildings). Then you may calculate the angle using Hit.point.
Can you do a LookAt using the rigidbody though? if i mess with the transform the movement functionality using W and S (to go forward and back) is lost.
Then use Quaternion.LookRotation and assign it to the rigidbody.
It takes a vector 3 in the form of direction, eg: rb.rotation = Quaternion.LookRotation(hit.point - rb.position)
btw, i found that rb.position and transform.position returns the exact same value every time, but rb.position is alot more expensive to call so i’d advise to use it like this even if you’re worried about setting the transform on a rigidbody.
*I may be wrong on that one, it may indeed do something funky to the physics system, but i have tested this and found nothing but a performance difference
further more, i beg of you to slep this unless you intentionally want it to snap on the spot, if you don’t know what it means look into it, it’s 4AM so i’ll just leave this here…