I want an object to follow another using Transform.LookAt. However, I do not want the rotation from LookAt to be instant. I want there to be a maximum angle it can turn in any one frame. Example - A missile following a ship. It should be turning rather slowly towards the target and not instantly.
How can I accomplish that?
I have tried taking the difference between the current transform’s rotation angle, and the angle the transform is at when rotated using LookAt and then applying the difference as the rotation amount, clamped between a min and max. However, I have run into problems when rotations happen around 0 degrees, going from negative to positive, and have been so far unable to solve those (my missile flips wildly when the object that it is following goes from positive to negative).
This code is for a dungeon crawler camera and the way its set up is you can change the damping effect in the game engine itself. This code with also track the object you want it to when you apply it to your camera. Hope it helps
This works well, but I don’t understand how to actually dampen how fast it can rotate. I’ve tried values between 1 and 100, and between 0 and 1.
This one, likewise, when I convert it to use rotation, does not seem to have any effect with the dampen variable.
And this was the code that I wrote. The dampen for this actually works, but if the target rotation goes from 359 to 1, it flips out and starts doing weird things. I just don’t know 3D vectors that well to understand what I’m doing wrong.
public GameObject target;
public float flightTime = 10f;
public float flightSpeed = 10f;
public float maxTurnAngle = 10f;
private Vector3 dir;
void Start()
{
//orient towards target
transform.LookAt(target.transform);
//set velocity
rigidbody.velocity = transform.forward * flightSpeed;
}
void Update()
{
//set direction towards target
dir = target.transform.position - transform.position;
//current rotation
Quaternion currentRotation = transform.rotation;
//current rotation angles
float currentAngleY = transform.rotation.eulerAngles.y;
float currentAngleX = transform.rotation.eulerAngles.x;
//orient towards target
transform.LookAt(target.transform);
//get new angles for when oriented towards target
float newAngleY = transform.rotation.eulerAngles.y;
float newAngleX = transform.rotation.eulerAngles.x;
//get differences between current and new angles
float differenceY = newAngleY - currentAngleY;
float differenceX = newAngleX - currentAngleX;
//set rotation back to original
transform.rotation = currentRotation;
//apply dampen for Y rotation
float rotateByY = Mathf.Clamp(Mathf.Abs(differenceY), 0f, maxTurnAngle);
if (differenceY < 0f)
{
rotateByY = rotateByY * -1f;
}
//apply dampen for X rotation
float rotateByX = Mathf.Clamp(Mathf.Abs(differenceX), 0f, maxTurnAngle);
if (differenceX < 0f)
{
rotateByX = rotateByX * -1f;
}
//rotate
transform.Rotate(rotateByX, rotateByY, 0f);
//set new velocity
rigidbody.velocity = transform.forward * flightSpeed;
}
Ohhh, I must have misrepresented what I’m trying to do. Sorry, me no speaka da English so well, haha.
What I’m trying to do is to simulate a missile fired at the player who controls a helicopter. So I don’t want it to be able to track the player with 100% accuracy, so that the player is able to dodge it.
For that purpose I am trying to rotate the object to face the player, but limit how fast it can rotate. That is what I meant when I said “dampen”.