Question by
Adrielcz · Oct 05, 2021 at 08:06 PM ·
c#rotationlookatlookattarget
Restrict my tank gun rotation.
So I got a RTS tank turret working, the gun less so, it should rotate up and down to align with the target, but when the target (cube) moves, it can't be reliably be constrained. I tried a lot, but I got no idea of how to fix it, had to turn the backups and I know that Euler angles will not accept "-" degrees .but since nothing is working, this is what I got now: By the way, the gun is following a cube by the tag in the picks: Plz help :( stop at this angle
sadness dot png
[/CODE]
if(turret1)
{
Vector3 targetDirection = Quaternion.LookRotation(target.position - turret1.transform.position).eulerAngles;
turret1.transform.localRotation = Quaternion.Euler(Vector3.Scale(targetDirection, rotationMaskTurret1));
}
if (gun1)
{
Vector3 targetDirection = Quaternion.LookRotation(target.position - gun1.transform.position).eulerAngles;
targetDirection.x = Mathf.Clamp(targetDirection.x, xMin1, xMax1);
gun1.transform.rotation = Quaternion.Euler(targetDirection);
}
[/CODE]
what-i-got.png
(262.5 kB)
what-i-want.png
(394.8 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613043852im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity 2D lookat and shoot issues 0 Answers
transform.lookat is not working correctly 0 Answers
Unity lookat at finger position 3D C# 0 Answers
LookAt() rotating the wrong way 0 Answers
How can i rotate an object using LookAt with only 1 axis? 0 Answers