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

How can I make a 4 direction animation for an enemy?

$
0
0
I'm trying to animate a 2d enemy in 4 directions, I tried making a blend tree but I couldn't find or figure out any compatible code with my AI code. Can anyone help? using System.Collections; using System.Collections.Generic; using UnityEngine; public class Enemy2AI : MonoBehaviour { public float speed; public float lineOfSite; public float shootingRange; public GameObject bulletPrefab; public GameObject bulletParent; public float fireRate = 1f; private float nextFireTime; private Transform player; // Start is called before the first frame update void Start() { player = GameObject.FindGameObjectWithTag("Player").transform; } // Update is called once per frame void Update() { float distanceFromPlayer = Vector2.Distance(player.position, transform.position); if (distanceFromPlayer < lineOfSite && distanceFromPlayer > shootingRange){ transform.position = Vector2.MoveTowards(this.transform.position,player.position,speed * Time.deltaTime); } else if (distanceFromPlayer <= shootingRange && nextFireTime < Time.time){ Instantiate(bulletPrefab, bulletParent.transform.position, Quaternion.identity); nextFireTime = Time.time + fireRate; } } private void OnDrawGizmosSelected() { Gizmos.color = Color.green; Gizmos.DrawWireSphere(transform.position, lineOfSite); Gizmos.DrawWireSphere(transform.position, shootingRange); } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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