- Home /
Question by
inaudiblefuzz · Feb 24, 2019 at 01:34 AM ·
c#aienemy aichase
Enemy chase script // No longer attacking
My enemy chase script was up and running when I get within 10 units he chases me down. I added in a fov so he'll engage when within 30 degrees. He's no longer attacking at all. I messed something up... I don't know what. Any ideas? - Thanks!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chase : MonoBehaviour
{
public Transform player; //Access Player character
static Animator anim; //Access animator
void Start() //Initalization
{
anim = GetComponent<Animator>();
}
void Update() //Update is called once per frame
{
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)
{
this.transform.Translate(0, 0, 0.05f);
anim.SetBool("isWalking", true);
anim.SetBool("isAttacking", false);
}
else
{
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
Raycasting AI 0 Answers
Enemy keeps moving in one direction. Help pls! 2 Answers
Enemy AI Not Working? 0 Answers
Looping Between Waypoints 1 Answer