Quantcast
Viewing all articles
Browse latest Browse all 1488

Hi guys, I am trying to make my enemy to chess the player when he enter in to danger zone. But the enemy is not chasing the player when he enter in to dangerzone. Please help me ..Thank you....

using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyMovementController : MonoBehaviour { public float enemySpeed; Animator enemyAnimator; //facing public GameObject enemyGraphic; bool canFlip = true; bool facingRight = false; float flipTime = 5f; float nextFlipChance = 0f; //attacking public float chargeTime; float startChargeTime; bool charging; Rigidbody2D enemyRB; // Use this for initialization void Start () { enemyAnimator = GetComponentInChildren (); enemyRB = GetComponent (); } // Update is called once per frame void Update () { if (Time.time > nextFlipChance) { if (Random.Range (0, 10) >= 5) flipFacing (); nextFlipChance = Time.time + flipTime; } } void OnTriggerEnter2D(Collider2D other){ if (other.tag == "Player"){ if (facingRight && other.transform.position.x < transform.position.x) { flipFacing (); } else if (!facingRight && other.transform.position.x > transform.position.x) { flipFacing (); } canFlip = false; charging = true; startChargeTime = Time.time + chargeTime; } } void OntriggerStay2D(Collider2D other){ if (other.tag == "Player") { if (startChargeTime < Time.time) { if (!facingRight) enemyRB.AddForce (new Vector2(-1, 0) * enemySpeed); else enemyRB.AddForce (new Vector2(1, 0) * enemySpeed); enemyAnimator.SetBool ("isCharging", charging); } } } void OnTriggerExit2D (Collider2D other ) { if (other.tag == "Player"){ canFlip = true; charging = false; enemyRB.velocity = new Vector2(0f, 0f); enemyAnimator.SetBool ("isCharging", charging); } } void flipFacing() { if (!canFlip) return; float facingX = enemyGraphic.transform.localScale.x; facingX *= -1f; enemyGraphic.transform.localScale = new Vector3( facingX, enemyGraphic.transform.localScale.y, enemyGraphic.transform.localScale.z); facingRight =! facingRight; } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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