Hello, I am new to Unity. I was looking for a code that would let me point one object at another. I found the Transform.LookAt which seemed like it should help with what I wanted, but it made the object move towards the other as well as point. If anyone can tell me what I am doing wrong, or show me another code that will help, it would be greatly appreciated.
Thank you
1 Answer
1This is a function I use, you may have to modify it for your purposes :
public void TurnTowards(Vector3 turnTo)
{
Vector3 direction = turnTo - trans.position;
direction.y = 0;
int fraction = 1; // might be needed in future
Quaternion targetRotation = Quaternion.LookRotation(direction, Vector3.up);
desiredRotation = Mathf.RoundToInt(targetRotation.eulerAngles.y);
currentRotation = Mathf.RoundToInt(trans.rotation.eulerAngles.y);
if (desiredRotation < currentRotation - turnSpeed * fraction ||
desiredRotation > currentRotation + turnSpeed * fraction)
{
trans.rotation = Quaternion.RotateTowards(trans.rotation, targetRotation, turnSpeed);
}
}