Question by 
               m9playsgames · Mar 07, 2017 at 08:16 PM · 
                scripting problemquaternionenemyaiquaternion.lookrotationquaternion.slerp  
              
 
              Enemy rotation while looking. Help.
Hello every one!
I have a script that makes the enemy AI look around for me. If I have my flashlight on, he can spot me from greater distance. However, when he spots me and I get closer, the enemy rotates upward while looking at me.
I thought I could fix this by locking the rotation but it has not helped. Is the script overwriting the lock? Please help!
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class EnemyAI : MonoBehaviour {
 
     public float fpsTargetDistance;
     public float enemyLookDistance;
     public float enemyLightLookDistance;
     public float attackDistance;
     public float attackLightDistance;
     public float enemyMovementSpeed;
     public float damping;
     public Transform fpsTarget;
     Rigidbody theRigidBody;
     Renderer myRender;
     public Light enemyLight;
     public Light seeEnemy;
     public Light PlayerLight;
     public GameObject MuzzleFlash;
     public Animator animator;
     public AudioSource gunshot;
 
     // Use this for initialization
     void Start() {
         myRender = GetComponent<Renderer>();
         theRigidBody = GetComponent<Rigidbody>();
     }
 
     // Update is called once per frame
     void FixedUpdate() {
         fpsTargetDistance = Vector3.Distance(fpsTarget.position, transform.position);
 
         if (PlayerLight.isActiveAndEnabled)
         {
             if (fpsTargetDistance < enemyLightLookDistance)
             {
                 enemyLight.enabled = true;
                 seeEnemy.enabled = true;
                 lookAtPlayer();
                 getCloser();
                 print("Noticed Flashlight");
                 if (fpsTargetDistance < attackLightDistance)
                 {
                     print("ATTACK!");
                     attack();  
                 }
             }
             if (fpsTargetDistance > enemyLightLookDistance)
             {
                 enemyLight.enabled = false;
                 seeEnemy.enabled = false;
 
 
             }
         }
         else
         {
             enemyLight.enabled = false;
             seeEnemy.enabled = false;
             animator.SetBool("IsShootingGun", false);
         }
         if (fpsTargetDistance < enemyLookDistance)
         {
             enemyLight.enabled = true;
             seeEnemy.enabled = true;
             lookAtPlayer();
             print("Noticed Player");
         }
         if (fpsTargetDistance < attackDistance)
         {
             attack();
             print("ATTACK!");
         }
     }
 
     void lookAtPlayer()
     {
         Quaternion rotation = Quaternion.LookRotation(fpsTarget.position - transform.position);
         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
     }
     void attack() {
         animator.SetBool("IsShootingGun", true);
     }
     void getCloser()
     {
         theRigidBody.AddForce(transform.forward * enemyMovementSpeed);
     }   
 }
 
 
              
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
Getting a smooth rotation with Quaterminions slerp in IEnumerator 1 Answer
Can't turn Y axis if terrain Hugging 0 Answers
Getting a 2D object to face the direction of its velocity relative to other objects in the level 0 Answers
Uniform quaternion from a list of points? 0 Answers
Rotation script isn't working 1 Answer