Untitled
unknown
plain_text
3 years ago
10 kB
11
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.AI;
using UnityEngine.UIElements;
public class KnifeController : MonoBehaviour
{
[Header("Target&Weapon")]
public GameObject player;
public MeleeWeapon meleeWeapon;
[Header("Navigation")]
NavMeshAgent navMeshAgent;
public TargetScanner targetScanner;
public GameObject currentTarget;
public float delayLostTarget = 10f;
private float timeLostTarget = 0;
[Header("Action")]
Animator animator;
public string currentAction;
const string STAND_STATE = "Stand";
public const string WALK_STATE = "Walk";
public const string ATTACK_STATE = "Attack";
public bool isChasing = false;
private float checkInterval = 2f;
private Vector3 previousPosition;
private float checkTimer;
private void Awake()
{
currentAction = STAND_STATE;
animator = GetComponent<Animator>();
navMeshAgent = GetComponent<NavMeshAgent>();
player = FindObjectOfType<PlayerFPS>().gameObject;
}
private void Update()
{
checkTimer += Time.deltaTime;
if (checkTimer >= checkInterval)
{
float distance = Vector3.Distance(transform.position, previousPosition);
if (distance < 0.01f)
{
if (currentAction == WALK_STATE)
{
Debug.Log("stuck");
navMeshAgent.enabled = false;
navMeshAgent.enabled = true;
navMeshAgent.ResetPath();
}
}
checkTimer = 0f;
previousPosition = transform.position;
}
FindingTarget();
if (currentTarget == null)
{
return;
}
if (MovingToTarget())
{
navMeshAgent.speed = 4;
Walk();
return;
}
if (currentAction != ATTACK_STATE)
{
Attack();
return;
}
}
public void MeleeWeaponStartAttack()
{
meleeWeapon.isAttacking = true;
meleeWeapon.StartAttack();
navMeshAgent.isStopped = true;
}
public void MeleeWeaponStopAttack()
{
meleeWeapon.isAttacking = false;
meleeWeapon.StopAttack();
}
public void DamageAgro()
{
currentTarget = player;
}
public void Stand()
{
ResetAnimation();
currentAction = STAND_STATE;
animator.SetBool("Stand", true);
}
private void Attack()
{
ResetAnimation();
currentAction = ATTACK_STATE;
animator.SetBool(ATTACK_STATE, true);
}
private void OnAttackFinished()
{
if (navMeshAgent.stoppingDistance == 1)
{
Debug.Log("attack again");
Attack();
return;
}
else
{
Debug.Log("move to player");
navMeshAgent.isStopped = false;
return;
}
}
private bool MovingToTarget()
{
if (currentTarget == null)
{
return false;
}
navMeshAgent.SetDestination(currentTarget.transform.position);
if (navMeshAgent.pathPending)
{
if (currentAction != WALK_STATE)
Walk();
}
else if (navMeshAgent.remainingDistance > navMeshAgent.stoppingDistance)
{
if (currentAction != WALK_STATE)
Walk();
}
else
{
RotateToTarget(currentTarget.transform);
Attack();
return false;
}
return true;
}
private void FindingTarget()
{
if (targetScanner.Detect(transform, player))
{
currentTarget = player;
timeLostTarget = 0;
return;
}
if (currentTarget != null)
{
timeLostTarget += Time.deltaTime;
if (timeLostTarget > delayLostTarget)
{
timeLostTarget = 0;
currentTarget = null;
}
return;
}
currentTarget = null;
}
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 * 8f);
return;
}
private void ResetAnimation()
{
animator.SetBool(STAND_STATE, false);
animator.SetBool(WALK_STATE, false);
animator.SetBool(ATTACK_STATE, false);
}
}
using UnityEngine;
using UnityEngine.AI;
public class WanderAIKnife : MonoBehaviour
{
public NavMeshAgent agent;
[Header("Speed&Radius")]
[Range(0, 100)] public float speed;
[Range(1, 500)] public float walkRadius;
[Header("Navigation")]
public GameObject NavSphere;
private Vector3 currentDestination;
[Header("Scripts")]
[SerializeField]
public KnifeController KnifeController;
[SerializeField]
public ReceiveDamageBAD ReceiveDamageBAD;
public void Start()
{
agent = GetComponent<NavMeshAgent>();
if (agent != null && KnifeController.currentTarget == null)
{
agent.speed = speed;
currentDestination = RandomNavMeshLocation();
agent.SetDestination(currentDestination);
KnifeController.Walk();
}
}
public void Update()
{
if (agent != null && !agent.pathPending && agent.remainingDistance <= agent.stoppingDistance && KnifeController.currentTarget == null)
{
if (agent != null && !agent.pathPending && Vector3.Distance(agent.transform.position, currentDestination) <= agent.stoppingDistance && KnifeController.currentTarget == null)
{
currentDestination = RandomNavMeshLocation();
KnifeController.Stand();
}
agent.SetDestination(currentDestination);
KnifeController.Walk();
}
}
private Vector3 RandomNavMeshLocation()
{
Vector3 finalPosition = Vector3.zero;
while (finalPosition == Vector3.zero || !NavSphere.GetComponent<Collider>().bounds.Contains(finalPosition))
{
Vector3 randomPosition = Random.insideUnitSphere * walkRadius;
randomPosition += transform.position;
if (NavMesh.SamplePosition(randomPosition, out NavMeshHit hit, walkRadius, 1))
{
finalPosition = hit.position;
}
}
return finalPosition;
}
}
using System.Collections;
using UnityEngine;
using UnityEngine.AI;
public class ReceiveDamageBAD : MonoBehaviour
{
[Header("Health")]
public int maxHitPoint = 5;
public int hitPoint = 0;
public bool isDead = false;
[Header("Sound")]
public AudioClip hitsound;
public AudioSource Ganados;
[Header("RagdollColliders")]
public CapsuleCollider caps1;
public CapsuleCollider caps2;
public CapsuleCollider caps3;
public CapsuleCollider caps4;
public CapsuleCollider caps5;
public CapsuleCollider caps6;
public CapsuleCollider caps7;
public CapsuleCollider caps8;
public CapsuleCollider caps9;
public BoxCollider boxcol1;
public BoxCollider boxcol2;
[Header("Scripts")]
[SerializeField]
public KnifeController KnifeController;
public GameObject weapon;
public GameObject BloodFX;
private NavMeshAgent navMeshAgent;
void Start()
{
hitPoint = maxHitPoint;
navMeshAgent = GetComponent<NavMeshAgent>();
}
void Update()
{
if (hitPoint <= 0)
{
gameObject.GetComponent<NavMeshAgent>().enabled = false;
isDead = true;
GetComponent<AudioSource>().clip = hitsound;
GetComponent<AudioSource>().Play();
weapon.GetComponent<BoxCollider>().enabled = false;
GetComponent<KnifeController>().enabled = false;
caps1.GetComponent<Rigidbody>().isKinematic = false;
caps2.GetComponent<Rigidbody>().isKinematic = false;
caps3.GetComponent<Rigidbody>().isKinematic = false;
caps4.GetComponent<Rigidbody>().isKinematic = false;
caps5.GetComponent<Rigidbody>().isKinematic = false;
caps6.GetComponent<Rigidbody>().isKinematic = false;
caps7.GetComponent<Rigidbody>().isKinematic = false;
caps8.GetComponent<Rigidbody>().isKinematic = false;
caps9.GetComponent<Rigidbody>().isKinematic = false;
boxcol1.GetComponent<Rigidbody>().isKinematic = false;
boxcol2.GetComponent<Rigidbody>().isKinematic = false;
GetComponent<Animator>().enabled = false;
StartCoroutine(Destroyondeath());
}
}
IEnumerator Destroyondeath()
{
yield return new WaitForSeconds(15);
Destroy(BloodFX);
}
public void GetDamage(int damage)
{
hitPoint -= damage;
if (hitPoint > 0)
{
KnifeController.DamageAgro();
}
}
}Editor is loading...