Question by
djordr · Mar 26, 2018 at 04:25 AM ·
rotationaxislimittransform.lookat
transform.LookAt and move towards an object on only two axises.
Yes I know a similar question has been asked before but it was explained so poorly that I'm forced to ask the question again. Here is the function that is called when the enemy is chasing the player.
void ChasePlayer()
{
one = false; //Ignore this
two = false; //Ignore this
enemySpeed = 3; //Speed is increased from walking to running
transform.LookAt (Player.position); //Enemy faces the player
transform.position = Vector3.MoveTowards(transform.position, Player.position, enemySpeed * Time.deltaTime); // the enemy's position is equal to moving from it's current position to player position at running speed.
}
Problem is that if the player jumps the enemy starts flying, how would I limit him from looking up at the player. Would it need to track something that isn't jumping? An empty that the player is parented to maybe. Or is there some way of doing it in script?
Comment
Best Answer
Answer by djordr · Mar 26, 2018 at 07:12 AM
You had to lock the y axis in a vector 3 like this. So the vector3 tracks the player x position, the player z position but not y since it's set to the object's y position which is always going to be at the same height as the object itself.
Vector3 Targetposition = new Vector3 (Player.position.x, transform.position.y, Player.position.z);
transform.LookAt (Targetposition);