I have this script that points a missile's z+ axis at its target:
public class Missile : MonoBehaviour
{
Vector3 target;
public Transform car;
void Update()
{
car = GameObject.FindGameObjectWithTag("Cube").transform;
target = Vector3.Normalize(car.position - transform.position);
Quaternion direction = Quaternion.LookRotation(target);
transform.rotation = Quaternion.Slerp(transform.rotation, direction, 4*Time.deltaTime);
transform.position = transform.position + 10* transform.up* Time.deltaTime;
}
}
But my missile model has its "front" pointing in the direction of the y+ axis, not z.
How can I convert the LookRotation
code above to rotate the y axis to my target, instead of the z? If possible, I'd love to see a diagram that explains how the conversion works.
I have referred to some previous answers about this topic, but still couldn't understand how to do it in this case.
target
as a member variable. You can just use it as a temporary insideUpdate
and throw it out at the end of the function. 2) don't search for your target withGameObject.Find()
every frame - this is slow. Do it only on start-up, or when the target changes (say the previous target got destroyed mid-flight and you need to pick a new one). 3) You're using a non-linear blend for your rotation, so the extra cost ofSlerp
overLerp
isn't doing anything useful for you here. 4) Give type names an initial uppercase letter. \$\endgroup\$