private bool PlayerTracking() { Vector2 playerDir; float playerDist; { GameObject player = GameObject.FindWithTag("Player"); if (player == null) { return(false); } Vector2 playerDirst = Vector2ex.By3(player.transform.position) - Vector2ex.By3(transform.position); playerDist = playerDirst.magnitude; playerDir = playerDirst / playerDist; } RaycastHit2D rcHit = Physics2D.Raycast(transform.position, playerDir, 10000.0f, 1 << LayerMask.NameToLayer("Map")); if (rcHit.collider != null) { if (rcHit.distance < playerDist) { return(false); } } return(true); }
protected override void StateUpdate() { if (BehaviorState.Attack == state) { if (PlayerTracking()) { GameObject player = GameObject.FindWithTag("Player"); Vector2 destDir = (Vector2ex.By3(player.transform.position) - Vector2ex.By3(transform.position)).normalized; float angle = Vector2.Angle(destDir, direction); if (angle <= weaponRotateSpeed * Time.deltaTime) { destDir = direction; weapon.WeaponFire(); } else { bool isRevClockwise = direction.x * destDir.y - direction.y * destDir.x >= 0; direction = Vector2ex.Rotate(direction, weaponRotateSpeed * Mathf.Deg2Rad * Time.deltaTime * (isRevClockwise ? 1 : -1)); } } else { SetStateIdle(); } } else if (BehaviorState.Moving == state) { transform.position += Vector2ex.To3(direction); } }
private void SetStateAttack() { state = BehaviorState.Attack; animator.SetBool("isMoving", true); GameObject player = GameObject.FindWithTag("Player"); dashDir = (Vector2ex.By3(player.transform.position) - Vector2ex.By3(transform.position)).normalized; playerLost = false; ResetStateTime(); }
protected override void StateChange() { if (state == BehaviorState.Bike_in || state == BehaviorState.Bike_riding || state == BehaviorState.Bike_out) { return; } float playerDist = Vector2.Distance(Vector2ex.By3(GameObject.FindWithTag("Player").transform.position), Vector2ex.By3(transform.position)); if (!PlayerTracking() || Random.Range(0.0f, 1.0f) < playerDist / 200.0f) { SetState_BikeIn(); } else { if (state == BehaviorState.Attack_machinegun || state == BehaviorState.Attack_machinegun_prefire || state == BehaviorState.Attack_grenadelauncher) { float rnd = Random.Range(0, 100); if (rnd < 65) { SetState_Idle(); } else { SetState_Move(); } } else { float rnd = Random.Range(0, 100); if (rnd < 65) { SetState_AttackMachinegun(); } else { SetState_AttackGrenadelauncher(); } } } }
protected override void StateUpdate() { if (BehaviorState.Bike_riding == state) { if (PlayerTracking()) { if (Vector2.Distance(Vector2ex.By3(GameObject.FindWithTag("Player").transform.position), Vector2ex.By3(transform.position)) < 100) { SetState_BikeOut(); animator.SetTrigger("isBikeFinish"); } } //으아 길찾기 if (TimeEx.Cooldown(ref astar_cooldown) == 0) { astar_cooldown = 0.5f; if ((astar_lastSucceeded = astarPathFinder.경로검색(GameObject.FindWithTag("Player").transform.position)) == true) { List <Astar.최단경로노드> path = astarPathFinder.GetPath(); astar_index = 0; astar_dir = Vector2ex.By3(path[0].월드위치) - Vector2ex.By3(transform.position); astar_remainDist = astar_dir.magnitude; astar_dir /= astar_remainDist; } } if (astar_lastSucceeded) { float movement = bikeSpeed * Time.deltaTime; while (true) { if (astar_remainDist < movement) { transform.position += Vector2ex.To3(astar_dir) * astar_remainDist; movement -= astar_remainDist; List <Astar.최단경로노드> path = astarPathFinder.GetPath(); astar_index++; if (astar_index >= path.Count) { astar_cooldown = 0; break; } astar_dir = Vector2ex.By3(path[astar_index].월드위치) - Vector2ex.By3(transform.position); astar_remainDist = astar_dir.magnitude; astar_dir /= astar_remainDist; } else { transform.position += Vector2ex.To3(astar_dir) * movement; astar_remainDist -= movement; break; } } } lrFliper.In(astar_dir); } else if (BehaviorState.Bike_out == state) { float movement = bikeSpeed * Time.deltaTime; transform.position += Vector2ex.To3(astar_dir) * movement; } else if (BehaviorState.Attack_machinegun == state || BehaviorState.Attack_machinegun_prefire == state || BehaviorState.Attack_grenadelauncher == state) { if (PlayerTracking()) { GameObject player = GameObject.FindWithTag("Player"); Vector2 destDir = (Vector2ex.By3(player.transform.position) - Vector2ex.By3(transform.position)).normalized; float angle = Vector2.Angle(destDir, direction); if (angle <= weaponRotateSpeed * Time.deltaTime) { if (state == BehaviorState.Attack_grenadelauncher) { weapon_GrenadeLauncher.WeaponFire(); } else { weapon_MG.WeaponFire(); } direction = destDir; } else { bool isRevClockwise = direction.x * destDir.y - direction.y * destDir.x >= 0; direction = Vector2ex.Rotate(direction, weaponRotateSpeed * Mathf.Deg2Rad * Time.deltaTime * (isRevClockwise ? 1 : -1)); } } else { if (BehaviorState.Attack_machinegun == state) { stateTime -= 1; if (stateTime <= 0) { SetState_Idle(); } else { SetState_AttackMachinegun_Prefire(); } } } } else if (BehaviorState.Moving == state) { transform.position += Vector2ex.To3(direction); } }