Last active
January 29, 2017 02:46
-
-
Save Yecats/60587744e827dd7c52f34f0f597571e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class GeneralInteraction : MonoBehaviour | |
| { | |
| private Transform _objectToLookAt = null; | |
| private bool _isRotating = false; | |
| private Quaternion _defaultRotation; | |
| public void Start() | |
| { | |
| //Store the default rotation of the NPC in case it is rotated towards the player object later. | |
| _defaultRotation = transform.rotation; | |
| } | |
| public void Update() | |
| { | |
| if (_isRotating) | |
| { | |
| RotateTowards(2f); | |
| } | |
| } | |
| /// <summary> | |
| /// This method is similiar to what was written on the episode for the player. | |
| /// The differences are: | |
| /// 1. Adjusted the done condition to check the players navigation distance. (Otherwise it can stop rotating befoer we're done.) | |
| /// 2. Checked to see if we have an object to look at. If not, it rotates back to default. | |
| /// </summary> | |
| /// <param name="speed">How quickly the rotation will occur.</param> | |
| public void RotateTowards(float speed) | |
| { | |
| Quaternion lookRotation; | |
| //Check to see if there's an object to rotate towards. If not, we're going back to the default location. | |
| if (_objectToLookAt != null) | |
| { | |
| Vector3 direction = (_objectToLookAt.position - transform.position).normalized; | |
| lookRotation = Quaternion.LookRotation(direction); | |
| } | |
| else | |
| { | |
| lookRotation = _defaultRotation; | |
| } | |
| //Rotate | |
| transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * speed); | |
| //Check if we should be done rotating. Null check as sometimes it drops the object before we've finished this call. | |
| if (_objectToLookAt != null && _objectToLookAt.tag.Equals("Player")) | |
| { | |
| if (Quaternion.Angle(transform.rotation, lookRotation) < 1 && && _objectToLookAt.GetComponent<MovementController>().GetIsNavigationDonePathing()) | |
| { | |
| _isRotating = false; | |
| } | |
| } | |
| else | |
| { | |
| if (Quaternion.Angle(transform.rotation, lookRotation) < 1) | |
| { | |
| _isRotating = false; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment