Hey, peeps.
I’ve recently started to add animations into my game, something I have zero experience with. However, the animations seem to have caused a bug that I find quite bizarre and baffling.
This is in regards to the in-game enemies. When I walk up to them, they start aiming and fire. And as long as I move in front of them, they adjust their aim like they should. However, when I circle-strafe around them, they seem to completely freak out. They stand in place, refuse to turn to face me and shoot into the wall. However, the bullets still head towards me, as if they actually had turned in my direction.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnemyAI : MonoBehaviour {
float patrolpoint;
public enum EnemyType {Guard = 1, Attack = 2, Defend = 3};
public EnemyType enemytype;
string mode;
UnityEngine.AI.NavMeshAgent agent;
float stoppingDistance;
Transform Target;
public Transform PatrolPoint1;
public Transform PatrolPoint2;
public Transform PatrolPoint3;
public Transform PatrolPoint4;
public float maxRange;
RaycastHit hit;
public float PatrolSpeed;
public float EngageSpeed;
public float TurnSpeed;
float DistanceToPlayer;
public static bool firing;
public float FireTime;
float FireRemain;
public float Cooldown;
float CooldownLeft;
string stage;
Vector3 TargetDir;
public Text DebugText;
Vector3 newDir;
Quaternion EnemyRotation;
public Rigidbody EnemyRigidbody;
float MovementSpeed;
public Animator YakuzaAnimator;
float MoveSpeed = 0;
float timeout = 0;
Vector3 LastPosition = Vector3.zero;
void Start () {
Target = GameObject.FindWithTag ("Player").transform;
agent = GetComponent<UnityEngine.AI.NavMeshAgent> ();
patrolpoint = 1;
FireRemain = FireTime;
CooldownLeft = Cooldown;
if (enemytype == EnemyType.Guard) {
mode = "patrol";
stage = "maneuvering";
}
if (enemytype== EnemyType.Attack) {
mode = "engaged";
stage = "maneuvering";
}
if (enemytype == EnemyType.Defend) {
mode = "wait";
stage = "maneuvering";
} else {
}
}
void Update () {
DistanceToPlayer=Vector3.Distance(Target.position, transform.position);
EnemyRotation = transform.rotation;
DebugText.text = "Mode: " + mode.ToString ();
if (mode == "patrol") {
YakuzaAnimator.SetBool ("Firing", false);
agent.stoppingDistance = 0;
agent.speed = PatrolSpeed;
if (patrolpoint == 1) {
agent.SetDestination (PatrolPoint1.position);
}
if (patrolpoint == 2) {
agent.SetDestination (PatrolPoint2.position);
}
if (patrolpoint == 3) {
agent.SetDestination (PatrolPoint3.position);
}
if (patrolpoint == 4) {
agent.SetDestination (PatrolPoint4.position);
}
else {
}
}
if (mode == "engaged") {
if (stage == "maneuvering") {
agent.stoppingDistance = maxRange;
agent.speed = EngageSpeed;
agent.SetDestination (Target.position);
firing = false;
if (DistanceToPlayer <= maxRange) {
if (Physics.Raycast (transform.position, (Target.position - transform.position), out hit, maxRange)) {
if (hit.transform == Target) {
stage = "aiming";
FireRemain = FireTime;
TargetDir = Target.position - transform.position;
TargetDir = Vector3.Normalize(TargetDir);
} else {
agent.stoppingDistance = 1;
agent.speed = EngageSpeed;
agent.SetDestination (Target.position);
}
}
}
}
if (stage == "aiming") {
float step = TurnSpeed * Time.deltaTime;
YakuzaAnimator.SetBool ("Firing", true);
newDir = Vector3.RotateTowards (transform.forward, TargetDir, step, 0.0f);
timeout = timeout + Time.deltaTime;
if (timeout >= 2) {
stage = "firing";
}
else {
if (newDir == TargetDir) {
stage = "firing";
} else {
transform.rotation = Quaternion.LookRotation (newDir);
}
}
}
if (stage == "firing") {
firing = true;
FireRemain = FireRemain - Time.deltaTime;
YakuzaAnimator.SetBool ("Firing", true);
if (FireRemain <= 0) {
stage = "cooldown";
CooldownLeft = Cooldown;
}
}
if (stage == "cooldown") {
firing = false;
agent.stoppingDistance = maxRange;
agent.speed = EngageSpeed;
agent.SetDestination (Target.position);
CooldownLeft = CooldownLeft - Time.deltaTime;
YakuzaAnimator.SetBool ("Firing", false);
if (CooldownLeft <= 0) {
stage = "maneuvering";
}
else {
}
}
if (stage == "Hide") {
agent.stoppingDistance = 0;
agent.speed = PatrolSpeed;
agent.SetDestination (PatrolPoint1.position);
}
if (stage == "Wait") {
if(Vector3.Distance(transform.position, Target.position) < maxRange )
{
if(Physics.Raycast(transform.position, (Target.position - transform.position), out hit, maxRange))
{
if(hit.transform == Target)
{
mode = "engaged";
}
}
}
}
}
}
void FixedUpdate (){
MovementSpeed =Mathf.Lerp(MovementSpeed, (transform.position - LastPosition).magnitude / Time.deltaTime, 0.10f);
if (MovementSpeed <= 1) {
YakuzaAnimator.SetFloat ("MovementSpeed", 1);
} else {
YakuzaAnimator.SetFloat ("MovementSpeed", MovementSpeed);
}
LastPosition = transform.position;
}
public void SeenPlayer(bool SeenPlayer){
if (SeenPlayer == true) {
if (mode == "patrol") {
mode = "engaged";
stage = "maneuvering";
}
}
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject.name == PatrolPoint1.name) {
if (mode == "patrol") {
patrolpoint = 2;
}
if (mode == "Hide") {
mode = "Wait";
}
}
if (col.gameObject.name == PatrolPoint2.name) {
patrolpoint = 3;
}
if (col.gameObject.name == PatrolPoint3.name) {
patrolpoint = 4;
}
if (col.gameObject.name == PatrolPoint4.name) {
patrolpoint = 1;
}
else {
}
}
}
As mentioned before, this has me severely puzzled, and I hope that someone else knows what the cause of my troubles might be.