- Home /
LookAt only on Z Axis
Hey guys, I'm trying to get an object to look at a player. The object moves with the player, however the problem is that it moves on the X and Y axis when I want it to just look at the Z axis.
public class FollowAim : MonoBehaviour {
private GameObject targetOBJ;
private Transform targetTRA;
// Update is called once per frame
void Update () {
targetOBJ = GameObject.FindGameObjectWithTag("Player");
targetTRA = targetOBJ.transform;
this.transform.LookAt(targetTRA);
}
}
Is there any way to get the object to JUST rotate on the Z axis?
I'm not sure I follow the question, they way you're talking about "moving on" and "looking at" axes seems a bit confused. Note that an axis is a line, with infinite extent, so saying that you want something to look at an axis doesn't actual define what you're trying to do.
I take it that you're saying that you don't actually want the object to look at the player? So what point do you want it to look at?
Sorry, What I meant was that it is rotating on the X and Y when I want it to JUST rotate on Z. I want the object to look at the player, and in order for it to do this, it must only rotate on Z
Sorry it appears that I wasn't clear on what it was I wanted to get done so I'll try to refine it a bit.
I'm doing a top down game. The objects move along X(L and R) and Y(U and D) and rotate on Z. So if I wanted an enemy to face the player, I would want the enemy to rotate on Z. Rotating on X or Y results in the enemy shooting bullets off into space not on screen or shooting the sprite on its side, neither of which I want.
$$anonymous$$y problem was that the original script was causing the enemy to Rotate on both X and Y at the same time, and not on Z. I wanted to know if/how to prevent the enemy from rotating on X and Y, and to only rotate on Z.
Hopefully that is more clear, sorry for the confusion.
Answer by jimbobulus2 · Aug 28, 2015 at 06:14 PM
Hi @bishop87, If you want to just rotate on the Z axis, don't use the LookAt() method, it uses all axes by default.
in your Update(), replace the line with LookAt to this:
Vector3 difference = targetTRA.position - transform.position;
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ);
If you need more info, I made a tutorial with an example project here: http://blog.iss.io/?p=26
This is the right approach I$$anonymous$$O. If you only want to rotate about one axis then only rotate about one axis... LookAt is not the best way to do that.
@jimbobulus2 How would I do the same thing for the x axis? It works great for the z axis.
@Cypras You would swap the axes, same code though: float rotX = $$anonymous$$athf.Atan2(difference.z, difference.y) * $$anonymous$$athf.Rad2Deg; Quaternion newRotation = Quaternion.Euler(new Vector3(rotX + adjustmentAngle, 0.0f, 0.0f));
I love how you approach this problem and I need such a solution but X and Y angles always get stuck at 0 and if I write down original X and Y angle ins$$anonymous$$d of 0, it doesn't look at target object anymore. Rotation goes wierd. Can you help me with my question? It is posted here.
Answer by captainmalk · Mar 07, 2020 at 06:49 PM
If you want to use "transform.lookat" you can always make a new vector and keep the object's transform in one or more of the axises, this is what i often do:
public Transform target;
void Update() {
transform.LookAt (new Vector3 (target.position.x, transform.position.y, target.position.z));
}
You should be able to change the "transform.position" on any axis you want to keep on the original position.
Thank you! This helped a lot in my racing game. I'm making a Car AI without using actual AI, and this helped so much!
Your answer
Follow this Question
Related Questions
Enemy circling around the player when using LookAt 1 Answer
Look At with SmoothDamp 1 Answer
Respawn looking 1 Answer