So I have already tried code from lots of different tutorials and code from answers from people with similar problems but none of it seems to work. I’ve imported the turret multiple times each with a different starting rotation which is what most solutions seemed to say. But i can’t seem to find how to fix the rotation.
Each time i play the game the turrets rotate to face the target, but instead of the barrel of the gun pointing at the target, it faces it with the side of the turret so its 90 degrees off.
How can i adjust the angle to correct the aim?
public class TurretAimScript : MonoBehaviour
{
public Transform target;
public float smoothing = 10.0f;
Quaternion lookAtRotation;
void Update()
{
lookAtRotation = Quaternion.LookRotation(target.transform.position - transform.position);
if (transform.rotation != lookAtRotation)
{
transform.rotation = Quaternion.RotateTowards(transform.rotation, lookAtRotation, Time.deltaTime * smoothing);
}
}
}
The first is, when your importing it, and you can even do this directly inside of Unity, look at the hierarchy’s transform gizmo, and the blue arrow is Z, which is its forward… Whichever way that is facing, is what the turret considers its “Forward” to be, so if its facing its side… Its side is considered its “forward”
So if you unparent the hierarchy, and manually rotate it in Unity, then parent the hierarchy structure back, itll be facing the correct way - dont rotate the object while it is still in the parent, or itll just kind of mess up and not look right.
Alternatively, you could do it by code as well, after looking at your target, do something like:
Quaternion turretRot = transform.localRotation; //temp variable to store the current rotation
turretRot.y -= 90; //subtract 90 degrees from its looking angle
transform.localRotation = turretRot; //apply the updated rotaton
Which is all untested code, but seems like the idea your looking for.