- Home /
 
Applying gravity to simple zombie AI
I made a simple zombie ai that follows the player, however, when the player is above the zombie the zombie will fly straight at the player instead of staying on the ground. Could someone please give me feedback (in C#) on how to keep the zombie on the ground? Here is my code:
  Debug.DrawLine(myTransform.position, target.transform.position); //draw line (in pause mode) from enemy to player
                 myTransform.rotation = Quaternion.Slerp(myTransform.rotation, //causes enemy to look at player
                     Quaternion.LookRotation(target.position - myTransform.position),//causes enemy to look at player
                         rotationSpeed * Time.deltaTime);//causes enemy to look at player
                
                  myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; //tells enemy to move toward player
 
              Answer by dre38w · Nov 23, 2012 at 12:56 AM
What you want to do is just apply that rotation on the enemy's Y axis. Change your myTransform.rotation to this:
 dirToPlayer = target.position - myTransform.position;
 
 myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.Euler(0, Quaternion.LookRotation(dirToPlayer).eulerAngles.y, 0), rotationSpeed * Time.deltaTime);
 
               That will only apply that rotation on the Y axis of the zombie.
I'm not sure this answers his question though:( Although this will keep him from incorrectly rotating, it won't, however, keep the zombie from floating if the player's position is higher or lower than the zombie.
It should because what happens is the zombie is looking towards wherever the player is located in the world. Whether it be above or below and then moving towards that position. Locking out all the other rotations and just rotating on the Y the enemy will no longer look up and move towards the player's above position.
Your answer
 
             Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Send an enemy back to its spawn point using waypoints 2 Answers
How can I make a gravity field similar to a planetary body? 2 Answers
How do I programm a Planet Gravitation? 0 Answers