- Home /
Chase script. I added a FOV now he won't engage
My chase script was working okay until I added a fov of <30. Now he doesn't engage me anymore. Another thing I was wondering about - Before I added in the FOV and he was chasing me around his walking animation was really clunky. Very hit/ miss. For the most part he was sliding. Is this because I chose to slow of a walking animation? Or maybe I should slow down his movement speed?
Thank you!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chase : MonoBehaviour
{
public Transform player; //Access Player character
static Animator anim; //Access animator
//Initalization
void Start()
{ //Animator
anim = GetComponent<Animator>();
}
//Update is called once per frame
void Update()
{ //Enemy attack range and field of view
Vector3 direction = player.position - this.transform.position;
float angle = Vector3.Angle(direction, this.transform.forward);
if (Vector3.Distance(player.position, this.transform.position) < 10 && angle <30)
{
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), 0.1f);
anim.SetBool("isIdle", false);
if (direction.magnitude > 2)
{ //Enemy walks within range to attack
this.transform.Translate(0, 0, 0.05f);
anim.SetBool("isWalking", true);
anim.SetBool("isAttacking", false);
}
else
{ //Enemy is within range //Stops walking and attacks
anim.SetBool("isAttacking", true);
anim.SetBool("isWalking", false);
}
}
else
{
anim.SetBool("isIdle", true);
anim.SetBool("isWalking", false);
anim.SetBool("isAttacking", false);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
AI Field of vision 1 Answer
Distribute terrain in zones 3 Answers
Enemy chase script // No longer attacking 0 Answers
How to make "enemy" have a frustum(cone) view and not a circle radius 2 Answers
Multiple Cars not working 1 Answer