Animations not playing correctly for FPS enemy AI
I am having trouble getting the animations specifically IdleFiring and RunForwards to play when the character is in and out of range of the enemy
Here's my code: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Animations;
public class EnemyAI : MonoBehaviour {
public GameObject Player;
private float ViewRange = 40f;
private float RayRange = 20f;
private int vel = 7;
public AudioClip bangpistol;
public RaycastHit LastPos;
public Vector3 RayDirection = Vector3.zero;
public GameObject target;
public Rigidbody Bullet;
public Transform Muzzle;
public float timer;
public Animator anim;
public float speed1;
public bool firing1;
public Camera cam;
public void Update()
{
timer -= Time.deltaTime;
if (timer <= 0)
{
timer = 0.3f;
}
RayDirection = Player.transform.position - transform.position;
if (Vector3.Angle(RayDirection, transform.forward) < ViewRange)
{
if (Physics.Raycast(transform.position, RayDirection, out LastPos, RayRange))
{
if (LastPos.collider.tag == "Player")
{
Attack();
}
}
}
float speed = speed1;
anim.SetFloat("Speed", speed);
bool firing = firing1;
anim.SetBool("Firing", firing);
}
public void Attack()
{
transform.LookAt(LastPos.transform.position);
if (RayDirection.magnitude > 5)
{
speed1 = 4;
transform.position = Vector3.MoveTowards(transform.position, LastPos.transform.position, Time.deltaTime * vel);
}
else
{
firing1 = true;
if (timer <= 0.1)
{
speed1 = 0;
Rigidbody b = GameObject.Instantiate(Bullet, Muzzle.position, Muzzle.rotation) as Rigidbody;
b.velocity = transform.TransformDirection(new Vector3(0, 0, 100));
AudioSource audio = GetComponent<AudioSource>();
audio.Play();
audio.clip = bangpistol;
audio.Play();
Destroy(Bullet, 2.0f);
}
}
}
private void OnCollisionEnter(Collision Hit)
{
if (Hit.gameObject.tag == "Player")
{
PlayerHealth ph = (PlayerHealth)target.GetComponent("PlayerHealth");
ph.AdjustCurrentHealth -= 10;
}
}
void Start()
{
anim = GetComponent<Animator>();
}
}
If you could help with the animations or just improve my script that would be amazing. Thanks in advance!
screenshot-13.png
(55.6 kB)
Comment
Your answer
Follow this Question
Related Questions
Animations not playing correctly for FPS enemy AI 0 Answers
Animation only plays in one of the objects 1 Answer
ANIMATION SCRIPTS HELP: Can I Skip/Forward the animation that played by touch? using C# script 0 Answers
Idle , Move and Attack animations 0 Answers
Randomly play multiple attack animation 0 Answers