Doubt regarding Enemy rotation Unity 3D

Hey there! Hope that you are doing well. :slightly_smiling_face:
My question is that, in video number; 169. Rotate to face Target, we have used this function for rotation of enemy towards player :

void FaceTarget()
{
    Vector3 direction = (target.position - transform.position).normalized;
    Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
    transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * turnSpeed);
}

So my question is, can we use LookAt method in place of this whole big function as compared to LookAt method?
I’m asking this because LookAt method does the same work as the FaceTarget method and I’m concerned about it, whether it will give me any errors in future or not.

Hi Pratyush,

In many cases, there are multiple ways to make something work in Unity, and Rick cannot show all of them. If your code works, it’s a solution by definition.

Don’t worry about future errors. You are not a clairvoyant, neither are we. When Rick writes his code, he cannot know if an error will occur in the future because of this code. If an error occurs, he will fix it. Do the same. You obviously did research, and you appear to be knowing what you are doing. And if that’s true, I don’t see any reason why you should not use your alternative solution.

Did this help? :slight_smile:


See also:

Thanks a lot ma’am!
Your advice definitely helped me alot, in terms of making choices and not being tensed about what will happen in future.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.