Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.15 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class TriggerDetectPlayer : MonoBehaviour {
  5.  
  6.  
  7.     float rotationSpeed = 270;
  8.     float movementSpeed = 1;
  9.     float meshRadius = 1.8f;
  10.     bool bitting;
  11.  
  12.     IEnumerator turnTowardsPlayer;
  13.     IEnumerator moveTowardsPlayer;
  14.  
  15.     public delegate void ZombieWalkDelegate();
  16.     public ZombieWalkDelegate zWalk;
  17.  
  18.     public delegate void ZombieIdleDelegate();
  19.     public ZombieWalkDelegate zIdle;
  20.  
  21.     public delegate void ZombieBiteDelegate();
  22.     public ZombieWalkDelegate zBite;
  23.  
  24.     public delegate void ZombieStopBiteDelegate();
  25.     public ZombieWalkDelegate zStopBite;
  26.  
  27.  
  28.     void OnTriggerEnter(Collider other)
  29.     {
  30.         if (other.tag.Equals ("Player")) {
  31.             //float playerDistance = Vector3.Distance(other.transform.position, this.transform.position);
  32.             //Debug.Log ("Encontrou o Player");
  33.  
  34.             //if(playerDistance >= 2f * meshRadius)
  35.             //{
  36.                 turnTowardsPlayer = TurnTowardsPlayer(other.transform);
  37.                 moveTowardsPlayer = MoveTowardsPlayer(other.transform);
  38.                 StartCoroutine(turnTowardsPlayer);
  39.                 StartCoroutine(moveTowardsPlayer);
  40.             //}
  41.         }
  42.     }
  43.  
  44.  
  45.     void OnTriggerExit(Collider other)
  46.     {
  47.         if (other.tag.Equals ("Player")) {
  48.             float playerDistance = Vector3.Distance(other.transform.position, this.transform.position);
  49.  
  50.             if(playerDistance >= 2f * meshRadius)
  51.             {
  52.                 StopCoroutine(turnTowardsPlayer);
  53.                 StopCoroutine(moveTowardsPlayer);
  54.                 zIdle();
  55.             }
  56.         }
  57.     }
  58.  
  59.     IEnumerator TurnTowardsPlayer(Transform player)
  60.     {
  61.         while (true) {
  62.             Quaternion targetRotation = Quaternion.LookRotation(player.position - transform.position, Vector3.up);
  63.             targetRotation.x = 0;
  64.             targetRotation.z = 0;
  65.  
  66.             transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, 1);
  67.             yield return 0;
  68.         }
  69.     }
  70.  
  71.     IEnumerator MoveTowardsPlayer(Transform player)
  72.     {
  73.         while (true) {
  74.             Vector3 playerDirection = transform.position - player.position;
  75.             playerDirection.y = 0;
  76.             playerDirection = playerDirection.normalized;
  77.  
  78.             Vector3 deltaMovement = playerDirection * movementSpeed * Time.deltaTime;
  79.  
  80.             transform.position -= deltaMovement;
  81.  
  82.             float playerDistance = Vector3.Distance(player.position, this.transform.position);
  83.             //Debug.Log ("Player Distance: " + playerDistance + "Mesh radius: " + meshRadius);
  84.  
  85.             if(playerDistance <= meshRadius && !bitting)
  86.             {
  87.                 zBite();
  88.                 bitting = true;
  89.             }
  90.  
  91.             if(playerDistance >= meshRadius && bitting)
  92.             {
  93.                 Debug.Log ("Saiu de perto player");
  94.                 zStopBite();
  95.                 bitting = false;
  96.             }
  97.  
  98.  
  99.  
  100.             zWalk();
  101.             yield return 0;
  102.         }
  103.     }
  104. }
  105.  
  106. using UnityEngine;
  107. using System.Collections;
  108.  
  109. public class ChangeAnimation : MonoBehaviour {
  110.  
  111.     Animator anim;
  112.     public TriggerDetectPlayer tdp;
  113.    
  114.     void Start()
  115.     {
  116.         anim = GetComponent<Animator> ();
  117.         tdp.zWalk = ZombieWalk;
  118.         tdp.zIdle = ZombieIdle;
  119.         tdp.zBite = ZombieNeckBite;
  120.         tdp.zStopBite = ZombieStopBiting;
  121.  
  122.     }
  123.  
  124.     void ZombieWalk()
  125.     {
  126.         anim.SetBool ("playerDetected", true);
  127.     }
  128.  
  129.     void ZombieIdle()
  130.     {
  131.         anim.SetBool ("playerDetected", false);
  132.     }
  133.  
  134.     void ZombieNeckBite()
  135.     {
  136.         anim.SetBool ("bite", true);
  137.     }
  138.  
  139.     void ZombieStopBiting()
  140.     {
  141.         anim.SetBool ("bite", false);
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement