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 = 180;
- float movementSpeed = 1;
- float meshRadius = 1;
- IEnumerator turnTowardsPlayer;
- IEnumerator moveTowardsPlayer;
- 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);
- }
- }
- }
- 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;
- yield return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement