Last active
January 29, 2017 20:38
-
-
Save Yecats/6b6b418e4dcdd9f511ea3f5d9c7d69d5 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
| void Update() | |
| { | |
| if (Input.GetMouseButtonDown(0)) | |
| { | |
| RaycastHit hit; | |
| if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100f, _layerMask)) | |
| { | |
| Debug.Log(hit.transform.gameObject.layer); | |
| //If we've clicked specifically on the ground layer | |
| if (hit.transform.gameObject.layer == 8) | |
| { | |
| Instantiate(PositionIndicator, new Vector3(hit.point.x, 0.1f, hit.point.z), Quaternion.Euler(-90f, 0, 0)); | |
| Move(hit.point); | |
| //Turn off rotating when the ground is clicked just in case the player hadn't finished rotating | |
| _needsRotation = false; | |
| } | |
| //we've clicked on an NPC | |
| else | |
| { | |
| _npcToView = hit.transform; | |
| Move(hit.transform.position + (transform.forward * -6)); | |
| _needsRotation = true; | |
| } | |
| } | |
| } | |
| //Stop the navMesh once the destination has been reached | |
| if (_isMoving && GetIsNavigationDonePathing()) | |
| { | |
| _navMeshAgent.Stop(); | |
| _isMoving = false; | |
| } | |
| //Only start rotating if we need to and we've stopped moving. | |
| if (_needsRotation && !_isMoving) | |
| { | |
| RotateTowards(2f); | |
| } | |
| } | |
| // Checks to see if the game object has reached it's destination. | |
| public bool GetIsNavigationDonePathing() | |
| { | |
| return Vector3.Distance(_navMeshAgent.destination, transform.position) <= _navMeshAgent.stoppingDistance; | |
| } | |
| //Updated this method to resume pathing once a new destination was provided | |
| private void Move(Vector3 newPosition) | |
| { | |
| _navMeshAgent.SetDestination(newPosition); | |
| _navMeshAgent.Resume(); | |
| } | |
| //Dropped the distance check from this method | |
| private void RotateTowards(float speed) | |
| { | |
| Vector3 direction = (_npcToView.position - transform.position).normalized; | |
| Quaternion lookRotation = Quaternion.LookRotation(direction); | |
| transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * speed); | |
| if (Quaternion.Angle(transform.rotation, lookRotation) < 1) | |
| { | |
| _isMovingAndNeedsRotation = false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment