So I am making a game where an enemy rotates to look at the player when they are nearby. This works perfectly until the player jumps, because now as the enemy is moving towards them they are rotating (and moving) up to match how high they were.
How can I only have it turn horizontally?
Transform.LookAt is effectively just getting the direction to a position and using Quaternion.LookRotation
to then rotate the transform in that direction.
Effectively the code is like this:
using UnityEngine;
public class LookAtExample : MonoBehaviour
{
#region Inspector Fields
[SerializeField]
private Transform _target;
#endregion
#region Unity Callbacks
private void Awake()
{
if (_target == null)
{
enabled = false;
}
}
private void Update()
{
Vector3 targetPosition = _target.position;
Vector3 thisPosition = this.transform.position;
Vector3 direction = targetPosition - thisPosition;
Quaternion lookRotation = Quaternion.LookRotation(direction);
this.transform.rotation = lookRotation;
}
#endregion
}
You can throw this onto a game object in a scene, reference a target, hit play, and the game object will face the target.
Since we control the direction now, you simply only need to add one line to exclude the vertical axis:
private void Update()
{
Vector3 targetPosition = _target.position;
Vector3 thisPosition = this.transform.position;
Vector3 direction = targetPosition - thisPosition;
direction.y = 0f; // flatten y axis
Quaternion lookRotation = Quaternion.LookRotation(direction);
this.transform.rotation = lookRotation;
}
if (distance <= 10 && distance >= 2)
{
enemyAnim.SetBool("Move", true);
transform.LookAt(player.transform.position, Vector3.up);
transform.Translate(Vector3.forward * Time.deltaTime * speed);
}
So this is my code (player is a game object I put the player in) how would I do that here? All I need is just to keep him from rotating back (on the x axis)
I literally wrote all the code you need already.
So I need all that just to keep him from rotating up?
5 lines of vector and rotation operations is not a lot of code. It’s very basic stuff.
If it wasn’t clear, only the code in the Update() function is really relevant. The MonoBehaviour script is an illustrative example.
Read the code, try to understand what it’s doing, and apply that to your use case.
Some useful further reading: Unity - Manual: Moving objects with vectors
zulo3d
December 8, 2024, 6:03am
7
Not that there’s anything wrong with Spiney’s code, but if you want to keep it to a single line then you can do this:
transform.LookAt(new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z));
And you can make the line shorter if you make ‘player’ reference the transform instead of the gameObject like this:
public Transform player;
Then you can do this:
transform.LookAt(new Vector3(player.position.x,transform.position.y,player.position.z));
1 Like
We really shouldn’t be teaching new users to write code like that. It’s not going to do them any favours in the future.
I tried putting a .y after player.transform.position but it kept giving me errors. This works though, thanks!