[2262-screen+a.jpg|2262]
[2263-screen+b.jpg|2263]
Hello,
I have two object, a ball (Target) and a gun (Gun) attached to a turret (Turret).
I want the turret to look towards the ball by rotating around its local y axis,
I managed to make it look towards the ball by using Turret.transform.LookAt(Target.position);
then I set to 0 the rotation on x and z axis and it works just fine,
that is when the turret is parallel to the green plane, if the plane is rotating,
(if the turret is attached to a ship for example)
then it doesn’t work anymore. I guess that’s because the angles are in world coordinates?
so if I want to just calculate the alpha angle in the second screenshot, so i could
rotate around local y axis, how should I do that?
with paper and pen I would project the two vectors onto the green plane then
calculate the angle between them, I wonder how to do this in Unity or whether there’s a better way to do that.
EDIT: Thank you bunny for replying, here’s the code I’m using
(yes It is a bit of a mess as results of many experiments sorry)
Quaternion originalRotation = new Quaternion();
originalRotation.x = turretBase2.rotation.x;
originalRotation.y = turretBase2.rotation.y;
originalRotation.z = turretBase2.rotation.z;
originalRotation.w = turretBase2.rotation.w;
turretBase2.LookAt(myTarget.position);
Vector3 eulerAngles = turretBase2.transform.rotation.eulerAngles;
eulerAngles.x = originalRotation.eulerAngles.x;
eulerAngles.z = originalRotation.eulerAngles.z;
Quaternion tempDesiredRotation = Quaternion.Euler(eulerAngles);
turretBase2.rotation = Quaternion.Lerp(originalRotation, tempDesiredRotation, Time.deltaTime * turnSpeed);