public void Update() { var playerPos = Player.instance.transform.position; var playerDist = (playerPos - transform.position).magnitude; var localMask = LayerMask.GetMask("Props"); var LoS = !Physics2D.Raycast(transform.position, playerPos - transform.position, playerDist, localMask); if (playerDist <= stalkRange && LoS) { behaviour = EnemyBehaviour.Agressive; path.speed = 3000; path.SetTarget(Player.instance.transform.position); } else if ((!LoS && playerDist <= stalkRange * 1.25f) || (LoS && playerDist <= stalkRange * 1.5f)) { behaviour = EnemyBehaviour.Near; path.speed = 2000; path.SetTarget(path.GetPointNearTarget(Player.instance.transform, stalkRange)); } else { if (behaviour != EnemyBehaviour.Idle || (behaviour == EnemyBehaviour.Idle && path.reachedEnd)) { path.SetTarget(path.GetPointNearTarget(transform, stalkRange)); } behaviour = EnemyBehaviour.Idle; path.speed = 1000; } sprite.flipX = (path.currentVelocity.x < 0); }
public void HandleBehaviour() { if (idling) { return; } if (behaviour == EnemyBehaviour.Idle) { if (path.reachedEnd) { var chance = Random.value; Debug.Log(chance); if (chance <= 0.1f) { idling = true; path.reachedEnd = false; path.NullTarget(); InstancedAction.DelayAction(() => { idling = false; path.reachedEnd = true; }, 1f); } else { path.SetTarget(path.GetPointNearTarget(transform, idleRange)); } } } else if (behaviour == EnemyBehaviour.Near) { if (path.reachedEnd) { path.SetTarget(path.GetPointOnDirection(Player.instance.transform, 20f, shotRange, (transform.position - Player.instance.transform.position))); } } else { if (lastBehaviour != EnemyBehaviour.Agressive) { path.SetTarget(path.GetPointOnDirection(Player.instance.transform, 20f, shotRange, (transform.position - Player.instance.transform.position))); } else if (path.reachedEnd) { var offset = Random.Range(15, 80); path.SetTarget(path.GetPointOnDirection(Player.instance.transform, offset, shotRange, (transform.position - Player.instance.transform.position), true)); } if (path.targetDistance <= shotRange * 1.2f && timer <= 0) { var direction = Player.instance.transform.position - bulletPoint.position; sprite.flipX = (direction.x < 0); anim.SetTrigger("Shot"); timer = shotCd; idling = true; shoting = true; shotDirection = direction.normalized; path.reachedEnd = false; path.NullTarget(); InstancedAction.DelayAction(() => { idling = false; path.reachedEnd = true; }, 1f); } } }
public void SetBehaviour() { var playerPos = Player.instance.transform.position; var playerDist = (playerPos - transform.position).magnitude; var mask = LayerMask.GetMask("Props"); var playerSight = !Physics2D.Raycast(transform.position, playerPos - transform.position, playerDist, mask); if (playerDist < circlingRange && playerSight && !striked) { behaviour = EnemyBehaviour.Agressive; if (!checking) { checking = true; if (timer < checkTime) { timer = checkTime; } } else { timer -= Time.deltaTime; if (timer <= 0 && checking) { if (playerDist <= circlingRange * 1.2f) { Debug.Log("STRIKE"); striked = true; path.stopped = true; path.rb.velocity = Vector2.zero; anim.SetTrigger("Strike"); strikeDirection = (playerPos - transform.position).normalized; } else { checking = false; } } } } if ((playerDist < NearRange * 0.75 && !playerSight) || (playerDist < NearRange && playerSight)) { if (behaviour != EnemyBehaviour.Near) { path.GetRandomOffset(); } behaviour = EnemyBehaviour.Near; path.SetTarget(path.GetPointOnRandomDirection(Player.instance.transform, 10f, circlingRange)); } else { if ((behaviour == EnemyBehaviour.Idle && path.reachedEnd) || behaviour != EnemyBehaviour.Idle) { var chance = Random.value; if (chance > 0.3f) { path.SetTarget(path.GetPointNearTarget(transform, IdleRange)); } else { path.stopped = true; InstancedAction.DelayAction(() => { path.stopped = false; }, 1f); } } behaviour = EnemyBehaviour.Idle; } }