Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5.  
  6. public class ClickToMove : MonoBehaviour {
  7.  
  8. public float speed;
  9. public CharacterController controller;
  10. public Animator Animator;
  11. private Vector3 position;
  12.  
  13.  
  14. void locatePosition()
  15. {
  16. RaycastHit hit;
  17. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  18.  
  19. if(Physics.Raycast(ray, out hit, 1000))
  20. {
  21. position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
  22. }
  23. }
  24.  
  25. void moveToPosition()
  26. {
  27. if(Vector3.Distance(transform.position, position)>1)
  28. {
  29. Quaternion newRotation = Quaternion.LookRotation(position-transform.position);
  30.  
  31. newRotation.x = 0f ;
  32. newRotation.z = 0f ;
  33.  
  34. transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
  35. controller.SimpleMove(transform.forward * speed);
  36.  
  37. }
  38. }
  39.  
  40. // Use this for initialization
  41. void Start ()
  42. {
  43.  
  44. }
  45.  
  46. // Update is called once per frame
  47. void Update ()
  48. {
  49.  
  50. if(Input.GetMouseButton(0)){
  51.  
  52. locatePosition();
  53. moveToPosition();
  54.  
  55. }
  56.  
  57. bool andando = Vector3.Distance (transform.position, position) > 1f;
  58. Animator.SetBool("Andar", andando);
  59.  
  60.  
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement