Been searching for a few hours on this and completely stuck. I think I am using the wrong words when trying to find an example of this as I am sure it’s a common thing.
Working on learning by making a simple game. I have an object (spaceship) that I am adding velocity to to move it. When it gets affected by gravity or other inputs I affect it’s velocity. Movement is working well, but now I want the nose of my ship to rotate toward the direction of movement. I can’t use player input it is affecting current velocity and not a direction direction input/change.
I have used other game objects to move things around the ship (targeting indicator of next pickup) by using this code;
public Transform target;
public int offset;
public Transform objectA;
void Update()
{
var dir = target.position - transform.position;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle + offset, Vector3.forward);
Vector3 pointA = objectA.transform.position;
Vector3 pointB = target.transform.position;
float distance = Vector3.Distance(objectA.position, target.position);
float a = 0.55f / distance;
Vector3 pointC = Vector3.Lerp(pointA, pointB, a);
transform.position = pointC;
}
Problem is I have no target to use similar code to in order to rotate my ship object/sprite to look in direction of travel. I’ve tried to even find a way to project a empty game object out in front to then use the same code, but have had no luck with that either.
yes, second is the stepped rotation. The angle between velocity and one local vector from the transform (that should be “face”) will be calculated. And then the object will be rotated in the left or right side depended on sign of the angle. Also, you are looking is the face left from the velocity vector or right.
lol, I feel with you. If I am asking something here then the solution is almost every time simple.