Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class TriggerDetectPlayer : MonoBehaviour {
- float rotationSpeed = 270;
- float movementSpeed = 1;
- float meshRadius = 1.8f;
- bool bitting;
- IEnumerator turnTowardsPlayer;
- IEnumerator moveTowardsPlayer;
- public delegate void ZombieWalkDelegate();
- public ZombieWalkDelegate zWalk;
- public delegate void ZombieIdleDelegate();
- public ZombieWalkDelegate zIdle;
- public delegate void ZombieBiteDelegate();
- public ZombieWalkDelegate zBite;
- public delegate void ZombieStopBiteDelegate();
- public ZombieWalkDelegate zStopBite;
- void OnTriggerEnter(Collider other)
- {
- if (other.tag.Equals ("Player")) {
- //float playerDistance = Vector3.Distance(other.transform.position, this.transform.position);
- //Debug.Log ("Encontrou o Player");
- //if(playerDistance >= 2f * meshRadius)
- //{
- turnTowardsPlayer = TurnTowardsPlayer(other.transform);
- moveTowardsPlayer = MoveTowardsPlayer(other.transform);
- StartCoroutine(turnTowardsPlayer);
- StartCoroutine(moveTowardsPlayer);
- //}
- }
- }
- void OnTriggerExit(Collider other)
- {
- if (other.tag.Equals ("Player")) {
- float playerDistance = Vector3.Distance(other.transform.position, this.transform.position);
- if(playerDistance >= 2f * meshRadius)
- {
- StopCoroutine(turnTowardsPlayer);
- StopCoroutine(moveTowardsPlayer);
- zIdle();
- }
- }
- }
- IEnumerator TurnTowardsPlayer(Transform player)
- {
- while (true) {
- Quaternion targetRotation = Quaternion.LookRotation(player.position - transform.position, Vector3.up);
- targetRotation.x = 0;
- targetRotation.z = 0;
- transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, 1);
- yield return 0;
- }
- }
- IEnumerator MoveTowardsPlayer(Transform player)
- {
- while (true) {
- Vector3 playerDirection = transform.position - player.position;
- playerDirection.y = 0;
- playerDirection = playerDirection.normalized;
- Vector3 deltaMovement = playerDirection * movementSpeed * Time.deltaTime;
- transform.position -= deltaMovement;
- float playerDistance = Vector3.Distance(player.position, this.transform.position);
- //Debug.Log ("Player Distance: " + playerDistance + "Mesh radius: " + meshRadius);
- if(playerDistance <= meshRadius && !bitting)
- {
- zBite();
- bitting = true;
- }
- if(playerDistance >= meshRadius && bitting)
- {
- Debug.Log ("Saiu de perto player");
- zStopBite();
- bitting = false;
- }
- zWalk();
- yield return 0;
- }
- }
- }
- using UnityEngine;
- using System.Collections;
- public class ChangeAnimation : MonoBehaviour {
- Animator anim;
- public TriggerDetectPlayer tdp;
- void Start()
- {
- anim = GetComponent<Animator> ();
- tdp.zWalk = ZombieWalk;
- tdp.zIdle = ZombieIdle;
- tdp.zBite = ZombieNeckBite;
- tdp.zStopBite = ZombieStopBiting;
- }
- void ZombieWalk()
- {
- anim.SetBool ("playerDetected", true);
- }
- void ZombieIdle()
- {
- anim.SetBool ("playerDetected", false);
- }
- void ZombieNeckBite()
- {
- anim.SetBool ("bite", true);
- }
- void ZombieStopBiting()
- {
- anim.SetBool ("bite", false);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement