Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Animations not playing correctly for FPS enemy AI

$
0
0
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![alt text][1] [1]: /storage/temp/109481-screenshot-13.png 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(); 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(); } } If you could help with the animations or just improve my script that would be amazing. Thanks in advance!

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>