Turret following target
I am currently trying to have a turret follow a target. However, it is not rotating properly. As you can see, the barrel does not only rotate around the axis it should, but in the wrong way. It does not align with the rest of the turret.
The turret is supposed to follow the target, rotating the turret and the barrel. Both transforms used seem to rotate properly on their own. However, the pitch transform is a child of the yaw transform, but rotates only around its own x-axis, not aligning with the turrets rotation.
The rotation code:
public class FollowTarget : MonoBehaviour {
GameObject target;
public Transform rotTr;
public Transform targetTr;
public float upperLimit;
public float lowerLimit;
public float speed;
public bool x;
public bool y;
// Use this for initialization
void Start () {
target = GameObject.FindGameObjectWithTag("Player");
}
// Update is called once per frame
void Update () {
if(target != null)
{
if (x)
{
X();
}
if (y)
{
Y();
}
}
}
void X()
{
Vector3 targetDir = target.transform.position - transform.position;
// Rotating in 2D Plane...
targetDir.x = 0.0f;
targetDir = targetDir.normalized;
Vector3 currentDir = targetTr.forward;
currentDir = Vector3.RotateTowards(currentDir, targetDir, speed * Time.deltaTime / 90, 1.0f);
Quaternion qDir = new Quaternion();
qDir.SetLookRotation(currentDir, Vector3.up);
if (qDir.x > upperLimit) qDir.Set(upperLimit, qDir.y, qDir.z, qDir.w);
if (qDir.x < lowerLimit) qDir.Set(lowerLimit, qDir.y, qDir.z, qDir.w);
rotTr.rotation = qDir;
if (rotTr.rotation.x > upperLimit) rotTr.rotation.Set(upperLimit, rotTr.rotation.y, rotTr.rotation.z, rotTr.rotation.w);
if (rotTr.rotation.x < lowerLimit) rotTr.rotation.Set(lowerLimit, rotTr.rotation.y, rotTr.rotation.z, rotTr.rotation.w);
}
void Y()
{
Vector3 targetDir = target.transform.position - transform.position;
// Rotating in 2D Plane...
targetDir.y = 0.0f;
targetDir = targetDir.normalized;
Vector3 currentDir = rotTr.forward;
currentDir = Vector3.RotateTowards(currentDir, targetDir, speed * Time.deltaTime / 90, 1.0f);
Quaternion qDir = new Quaternion();
qDir.SetLookRotation(currentDir, Vector3.up);
rotTr.rotation = qDir;
if (rotTr.rotation.y > upperLimit) rotTr.rotation.Set(rotTr.rotation.x, upperLimit, rotTr.rotation.z, rotTr.rotation.w);
if (rotTr.rotation.y < lowerLimit) rotTr.rotation.Set(rotTr.rotation.x, lowerLimit, rotTr.rotation.z, rotTr.rotation.w);
}
}
Hi, I would suggest to check the free "Tank!" demo/tutorial from Unity $$anonymous$$m in the Asset Store. here: https://www.assetstore.unity3d.com/en/#!/content/46209. $$anonymous$$aybe they solved this problem there and it can be a source of inspiration.
Unfortunately, no. The turrets do not rotate and both tanks are controlled by players. Thank you for your suggestion anyways.
Arg, the title image of this tutorial is misleading...
Did you check this post: http://answers.unity3d.com/questions/636145/how-rotate-turret-and-follow-body-object.html
Also, quickly checking your code: you are comparing quaternion's xyzw components. Unless you know exactly what you are doing, those values are not like Euler angles and are usually not "human understandable" (the Unity doc page about quaternion explains that too). Try with quaternion.eulerAngles.x etc... It also has it's limits but as far as rotations are not combined it works well.
I also use a lot of Debug.DrawLine to figure out what's happening while debugging this kind of code, helps a lot :)
Your answer
Follow this Question
Related Questions
How to make a camera follow a ball from behind? c# 3 Answers
2D Object following the cursor without glitchyness at the center 0 Answers
Rotate player with mouse. 0 Answers
How to Rotate on Y Axis by 90 degrees left or right? 1 Answer
Rotate an object around one of his childs in the self space (not the world space) 0 Answers