Help with rotating turret

Hello, I am completely stuck on this seemingly simple task and need some help:

I got a rotating cylinder which has a cube (turret) as a child. The turret is only allowed to aim up down but not sideways, trying as good as he can to look at the target. The turret should rotate with the base cylinder. I have used this code to limit the rotation on the x-axis, however the turret doesn’t rotate anymore with the cylinder base :frowning: How can I get this right?

public class Targetcursor : MonoBehaviour {
	
	void Update () {
		
		Vector3 target = GameObject.Find("target").transform.position;
		Vector3 dir = target-transform.localPosition;
		dir.x = transform.position.x;
		transform.LookAt(dir);
	}
}

I included a small scene setup package (Unity 3.5) in case someone wants to play with it…

826450–30766–$turret.unitypackage (38.1 KB)

When i get home, i’ll try to grab your project and get it to work. But right off the bat i’m seeing that your using the local position. Unless both the target and the target cursor have the same parent, then you probably don’t want to use localPosition, probably want to use transform.position.

ON the plus side, you had target - position set up right, which is awesome, I like you already. (target - tower!!)

That would be awesome if you had time to fix this. I am afraid the whole thing needs some quaternion magic too :confused: where I’m not very good at yet… I’ll try myself meanwhile and certainly will update this topic if I find the solution.

Ugh… I somehow through trial and error made something like that which approximates to the solution, but I’m not sure if this is correct, need some further testing… something is still bugged, furthermore the collider of the turret has a different rotation compared tothe actual mesh O_o.

	void Update () {
		
		Vector3 target = GameObject.Find("target").transform.position;
		Vector3 dir = target-transform.position;
		dir.z = 0;
		
		Quaternion q1 = transform.parent.rotation;
		Quaternion q2 = Quaternion.LookRotation(dir,Vector3.up);
		
		transform.rotation = q1*q2;

Ok, I think I got it to work correctly after watching a Quaternion tutorial on youtube…
Corrected package included as well in case anyone else needs to get something like that done :roll_eyes:
I still wonder if this is the best solution. When watching through the Unity-Answers section most suggestions are with “LookAt” and just setting one Axis zero… which doesn’t seem to work in my case.

void Update () {
		
		Vector3 target = GameObject.Find("target").transform.position;
		Vector3 dir = target-transform.position;
		Quaternion newQuaternion = Quaternion.LookRotation(dir,Vector3.up);
		newQuaternion.z = 0.0f;
		newQuaternion.y = 0.0f;
		
		transform.rotation = newQuaternion;
		
		Quaternion baseRotation = transform.parent.rotation;
		transform.rotation = baseRotation*newQuaternion;
	}

826568–30774–$turret.unitypackage (38.3 KB)

Hi
After 6 years… but maybe someone will find this usefull :slight_smile:
Just calculating future angle between the parent (tank, base etc) and the turret.
If angle will be bigger than our Max then turret is not roatatin.

        Vector3 targetDir = ShootingTarget.position - transform.position;
        targetDir.y = 0;
        Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, RotationSpeed * Time.deltaTime, 0.0f);
        float newAngle = Vector3.Angle(this.transform.parent.transform.forward, newDir);
        if (newAngle > MaxRotationAngle) return;
        transform.rotation = Quaternion.LookRotation(newDir);