transform.LookAt(target);

I am working on enemyAI in which the enemy is on a patrol but engages the player when they are within certain area.

However because the center of my character is lower than the enemy the enemy tries to look down on the floor as i have transform.LookAt(target); telling it to. This makes it act a bit crazy.

Is there a way to use transform.LookAt(target); but specify it to only do so on the Y axis and not the X so it doesn’t look down at the floor.

I found this on the other unity forum.

function LookAtIgnoreHeight (target : Transform) {
    var lookAtPos : Vector3 = target.position;
    //Set y of LookAt target to be my height.
    lookAtPos.y = transform.position.y;
    transform.LookAt (lookAtPos);
}

That should be what you need.

Please confirm if that works for future consultations.

hello, yes there is a way to do this, remember that you are calling the transform of the player character, which contains a Vector3.

For example :

transform.LookAt(new Vector3(other.transform.position.x, other.transform.position.y+10, other.transform.position.z));

Hope this helpes :slight_smile:

This throws up the following error:

MissingMethodException: Method not found: ‘UnityEngine.Transform.LookAtIgnoreHeight’.

Here is my code. The part is in the Mover Function. Rest of the code is include incase it’s relevant.

var waypoints : Transform [];
var currentPoint : int = 0;
var state : String = "patrol";
var speed : float = 10;
var chaseDist : float = 15;
var gravity : float = 15;
private var player : Transform;
private var controller : CharacterController;

function Start(){
	player = gameObject.FindWithTag("Player").transform;
	controller = GetComponent(CharacterController);	
}

function Update () {
	if(player) {	
		var playerDist = Vector3.Distance(player.position, transform.position);
		if (playerDist <= chaseDist ){
			state = "chase";	
		} else {
			state = "patrol";
		}
	
	
		if(state == "patrol"){
			Debug.Log("patrol");
			if(currentPoint < waypoints.length) {
				Mover(waypoints[currentPoint].position);	
			}
		} else if (state == "chase"){
			Debug.Log("chasing");
			Mover(player.position);
		}
	} 
}



function Mover(target : Vector3){

	var diffVector: Vector3 = target - transform.position;

	var movement : Vector3;
	
	if(diffVector.magnitude > 1){
	 movement = (diffVector.normalized * speed);
	 
	}else{
		currentPoint = (currentPoint + 1) % waypoints.length;
	}
	
	movement.y -= gravity * Time.deltaTime;
	controller.Move(movement * Time.deltaTime);
	
	
	 transform.LookAtIgnoreHeight(target);
	//transform.LookAt(target);
	
}

function LookAtIgnoreHeight (target : Transform) {
    var lookAtPos : Vector3 = target.position;
    lookAtPos.y = transform.position.y;
    transform.LookAt (lookAtPos);
}


function OnCollisionEnter(hitPlayer: Collision){
	
	if(hitPlayer.gameObject.tag == "Player"){
		Debug.Log("collision detected");
		Destroy(hitPlayer.gameObject);
		Debug.Log("Object Destroyed!");
		
	}
}

I fixed this in the end, it was complicated but in short ‘blacktear’ was correct.

I had to two different lookats into the two different states of my enemy. When the enemy is moving toward the waypoints then transform.LookAt(target); was fine

then in my chase state i used:

transform.LookAt(Vector3(player.transform.position.x, transform.position.y, player.transform.position.z));

the reason for this is for some reason using (target) wouldn’t work with the both waypoints and the player.