- Home /
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.
Answer by TheFrankman123 · Mar 27, 2012 at 11:11 PM
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.
Answer by fafase · Mar 21, 2012 at 07:34 PM
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.
Answer by blacktear · Mar 21, 2012 at 07:59 PM
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 :)
Answer by TheFrankman123 · Mar 21, 2012 at 07:59 PM
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!");
}
}
try without the transform, actually transform.LookAtIgnoreHeight(target); does not make sense since it is not a member of transform. The guy reported it this way on the thread but as it is simply a function you should call it as a function simply LookAtIgnoreHeight(target);
Unless i'm missing something calling the function like this
LookAtIgnoreHeight(target);
in the $$anonymous$$over functions throws up this error...
BCE0017: The best overload for the method 'waypoints2.LookAtIgnoreHeight(UnityEngine.Transform)' is not compatible with the argument list '(UnityEngine.Vector3)'.
Try taking off the .position in the parameter you pass in the update, try $$anonymous$$over(waypoints[currentPoint]); and $$anonymous$$over(player); and then change also function $$anonymous$$over (target:Transform){}
all hell breaks loose when you do that, because then the var diffVector doesn't work and more... surely there is something within the LookAt, maybe LookAtRotation?
Your answer
Follow this Question
Related Questions
AI script problems, interaction with grenade not working, need help. 2 Answers
Ai Zombie Melee Attack script. 5 Answers
Enemy following Player on uneven surface 1 Answer
About a enemy AI in my game 1 Answer
WayPoints mixed with Raycast 1 Answer