Lerp problem when target is behind turret
Hi!
I'm trying to make a 2-part laser turret that follows the given target.
The turret consists of:
(BLUE) A fork that rotates around horizontally
(RED) An axle that sits on the fork that rotates vertically
Emitter which emits the laser, sitting at the barrel of the gun
The problem only occurs when a target passes over the turret or the target is located exactly behind the turret (180 degrees from the original facing direction).
At that point the fork spins quickly around to face the target (working as intended) but the axle rotation speed drops and the turret takes ages to zero in on the target until the target returns to in front of the turret, at which point the turret starts tracking the target correctly again.
What is the specific reason the axle behaves like this?
Below is the method for following the target.
void FollowTarget()
{
//Rotation from Emitter to Target
var targetRotation = Quaternion.LookRotation(target.position - Emitter.position);
//Cleaning up the rotation for fork-component (moving on y-axis)
f_targetRotation = targetRotation;
f_targetRotation.x = 0.0f;
f_targetRotation.z = 0.0f;
//Cleaning up the rotation for axle-component (moving on x-axis)
a_targetRotation = targetRotation;
a_targetRotation.y = 0.0f;
a_targetRotation.z = 0.0f;
//lerping the Fork and Axle
Fork.localRotation = Quaternion.Lerp(Fork.localRotation, f_targetRotation, Time.deltaTime * 32);
Axle.localRotation = Quaternion.Lerp(Axle.localRotation, a_targetRotation, Time.deltaTime * 32);
}
I'm quite new to Unity so any suggestions and corrections to the code are appreciated :) Thanks in advance!
Further testing shows that when the target is exactly above the turret, the fork spins rapidly around a few times until it locks on the target. I think this behaviour is related to the main issue.
I'm thinking my follow-method is missing some handling for these specific situations where the fork cannot decide on what direction to face.
Your answer
Follow this Question
Related Questions
Interpolated movement in a circle 1 Answer
Setting slider value over time using C# script? 0 Answers
Using lerp properly 2 Answers
Mathf.Lerp crashing build? 1 Answer
Vector3.lerp for moving transform 0 Answers