Untitled
unknown
plain_text
2 years ago
6.5 kB
6
Indexable
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.AI; using static UnityEngine.GraphicsBuffer; public class BowController : MonoBehaviour { [Header("Shoot")] public GameObject bullet; public Transform shootPoint; public float shootSpeed = 10f; public float shootUpSpeed = 2f; float originalTime; [Header("Navigation")] public GameObject player; NavMeshAgent navMeshAgent; public TargetScanner targetScanner; public GameObject currentTarget; public float delayLostTarget = 10f; private float timeLostTarget = 0; [Header("Wander")] public float wanderRadius; private Transform target; public GameObject NavSphere; [Header("Action")] Animator animator; public string currentAction; const string STAND_STATE = "Stand"; const string TAKE_DAMAGE_STATE = "Damage"; public const string DEFEATED_STATE = "Defeated"; public const string WALK_STATE = "Walk"; public const string ATTACK_STATE = "Attack"; public bool isChasing = false; private void Awake() { currentAction = STAND_STATE; animator = GetComponent<Animator>(); navMeshAgent = GetComponent<NavMeshAgent>(); player = FindObjectOfType<PlayerFPS>().gameObject; currentTarget = null; } private void Update() { Vector3 targetPosition = player.transform.position; shootPoint.LookAt(targetPosition); if (currentAction == DEFEATED_STATE) { navMeshAgent.ResetPath(); return; } if (currentAction == TAKE_DAMAGE_STATE) { navMeshAgent.ResetPath(); TakingDamage(); return; } FindingTarget(); if (currentTarget == null) { Wander(); //navMeshAgent.ResetPath(); return; } if (MovingToTarget()) { Walk(); return; } if (currentAction != ATTACK_STATE && currentAction != TAKE_DAMAGE_STATE) { Attack(); return; } if (currentAction == ATTACK_STATE) { } } public void Wander() { Vector3 WanderPos = RandomNavSphere(transform.position, wanderRadius, 7); Debug.Log(transform.position.ToString()); navMeshAgent.SetDestination(WanderPos); Debug.Log("gotonewdestination"); if (navMeshAgent.remainingDistance > navMeshAgent.stoppingDistance) { if (currentAction != WALK_STATE) Walk(); return; } else { if (currentAction != STAND_STATE) Stand(); return; } } public Vector3 RandomNavSphere(Vector3 NavSphere, float dist, int layermask) { Vector3 randDirection = NavSphere + Random.insideUnitSphere * dist; randDirection += NavSphere; NavMeshHit navHit; NavMesh.SamplePosition(randDirection, out navHit, dist, layermask); Debug.Log(navHit.position.ToString()); return navHit.position; } public void DamageAgro() { currentTarget = player; } public void Stand() { ResetAnimation(); currentAction = STAND_STATE; animator.SetBool("Stand", true); } public void TakeDamage() { ResetAnimation(); currentAction = TAKE_DAMAGE_STATE; animator.SetBool("Damage", true); } private void TakingDamage() { if (this.animator.GetCurrentAnimatorStateInfo(0).IsName(TAKE_DAMAGE_STATE)) { float normalizedTime = this.animator.GetCurrentAnimatorStateInfo(0).normalizedTime; if (normalizedTime > 1) { return; } } } private bool MovingToTarget() { navMeshAgent.SetDestination(player.transform.position); if (navMeshAgent.remainingDistance == 0) return true; if (navMeshAgent.remainingDistance > navMeshAgent.stoppingDistance) { if (currentAction != WALK_STATE) Walk(); } else { RotateToTarget(currentTarget.transform); return false; } return true; } private void FindingTarget() { if (targetScanner.Detect(transform, player)) { currentTarget = player; isChasing = true; timeLostTarget = 0; return; } if (currentTarget != null) { timeLostTarget += Time.deltaTime; if (timeLostTarget > delayLostTarget) { timeLostTarget = 0; currentTarget = null; isChasing = false; } return; } currentTarget = null; } private void ShootPlayer() { GameObject currentBullet = Instantiate(bullet, shootPoint.position, shootPoint.rotation); Rigidbody rig = currentBullet.GetComponent<Rigidbody>(); rig.AddRelativeForce(Vector3.forward * shootSpeed, ForceMode.VelocityChange); rig.AddRelativeForce(Vector3.up * shootUpSpeed, ForceMode.VelocityChange); } private void Attack() { ResetAnimation(); currentAction = ATTACK_STATE; animator.SetBool(ATTACK_STATE, true); } public void Walk() { ResetAnimation(); currentAction = WALK_STATE; animator.SetBool(WALK_STATE, true); } private void RotateToTarget(Transform target) { Vector3 direction = (target.position - transform.position).normalized; Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z)); transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 3f); } private void ResetAnimation() { animator.SetBool(STAND_STATE, false); animator.SetBool(TAKE_DAMAGE_STATE, false); animator.SetBool(WALK_STATE, false); animator.SetBool(ATTACK_STATE, false); } }
Editor is loading...