Locking the x and z axis while using transform.LookAt()?

Hi

I have an Object which I would like to look at the camera and all times I wanted to use transform.LookAt() but it uses all axis and I couldn’t find a way to lock the x and z axis so it stays flat on the ground. I feel like the other posts in this forum regarding this topic aren’t working anymore (outdated?)

Hope there is a way to do this :slight_smile:

Thanks

Vector3 rotation = Quaternion.LookRotation(target).eulerAngles;
rotation.y = 0f;

transform.rotation = Quaternion.Euler(rotation);
2 Likes

Isn’t this opposite of what he wants?

Shouldn’t it be this:

Vector3 rotation = Quaternion.LookRotation(target).eulerAngles;
rotation.x = 0f;
rotation.z = 0f;

transform.rotation = Quaternion.Euler(rotation);
2 Likes

I honestly don’t think an answer to this would be outdated. :slight_smile:

3 that are working…

Vector3 rot = Quaternion.LookRotation(target.position - transform.position).eulerAngles;
rot.x = rot.z = 0;
transform.rotation = Quaternion.Euler(rot);
 // or
Vector3 newtarget = target.position;
newtarget.y = transform.position.y;
transform.LookAt(newtarget);
// or
Vector3 dir = target.position - transform.position;
dir.y = 0;
transform.rotation = Quaternion.LookRotation(dir);
11 Likes

Thanks guys! :slight_smile:

Actually found a video tutorial on youtube which works. Thanks again!

3 Likes

Why would anyone be daft enough to post a video in a forum? Those expire and get banned depending on your region. If you have working code please post it as either that video has expired or it is not available in my country.