How to fix turret rotation offest by 90 degrees
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);
}
}
}
Answer by Dibbie · Jun 22, 2016 at 04:50 AM
2 ways.
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.
Thanks for your response. Unfortunately even un-parented and given a new rotation it didn't work. I tried using an empty game object, offsetting the rotation in that and then parenting the turret to that but it also just ignored that and rotated the same.
The code also didn't work as turretRot.y is a Quaternion and it didn't seem to like taking 90 from it. However it gave me an idea of how to fix it. I changed the euler angles of the y value ins$$anonymous$$d which worked!.
I thought I'd share my code to show how i got it working and hopefully it might help somebody else.
public class TurretAimScript : $$anonymous$$onoBehaviour
{
public Transform target;
public float smoothing = 10.0f;
Quaternion lookAtRotation;
public float offset = 5.0f;
Vector3 tmp;
void Update()
{
lookAtRotation = Quaternion.LookRotation(target.transform.position - transform.position);
if (transform.rotation != lookAtRotation)
{
tmp = lookAtRotation.eulerAngles;
tmp.y -= 90; // fix the rotation
tmp.z = (tmp.x * -1) - offset; // uses the x value to aim vertically and iff offset so it aims at the center of the target
tmp.x = 0; // stops the turret from rotating in the wrong axis
Quaternion newRot = Quaternion.Euler(tmp);
transform.rotation = Quaternion.RotateTowards(transform.rotation, newRot, Time.deltaTime * smoothing * 2);
}
}
}
Glad I could help - I completely forgot about EulerAngles as a solution, and also forgot to say - 90f, as a Quaternion only accepts float values and not integers, haha.
Answer by dahiyabunty1 · Aug 08, 2021 at 04:50 PM
if someone face this problem again .
solution add second parameter in function lookrotation vector 3.up or anything try change fit ur need
Your answer
Follow this Question
Related Questions
Quaternion.LookRotation on only one axis? 1 Answer
Rotate LOCAL y axis to match Joystick input 0 Answers
transform rotation inaccuracy 0 Answers
Rotate around a point with a specific angle 0 Answers
How to make camera moving as the player on a planet 0 Answers