AI being scared of player.
So I am trying to create an C# script where the enemy turns and follows the player the moment they enter a trigger sphere. However no matter what I try, the test box that I am using as my enemy always wants to face away from my player showing them its back. Here is the code that I am using and I could really use some help on it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAI : MonoBehaviour
{
public Rigidbody bullet;
public GameObject player;
public int velocity;
public Collider fieldofview;
private bool isPlayerinRange;
public float rotateSpeed;
public void Start()
{
isPlayerinRange = false;
}
public void Update()
{
if (isPlayerinRange)
{
transform.rotation = Quaternion.Slerp(gameObject.transform.rotation, player.transform.rotation, rotateSpeed);
}
}
public void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
Debug.Log("Player in Range");
isPlayerinRange = true;
}
}
public void OnTriggerExit (Collider other)
{
if (other.tag == "Player")
{
Debug.Log("Player not in Range");
isPlayerinRange = false;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How do i randomize certain waypoints at the end of a path? 0 Answers
Top Down 2D Character Facing Animation Problems 0 Answers
Find closest object and return this 1 Answer
Beat 'Em Up AI 0 Answers