Advertisement
Guest User

terer

a guest
Sep 17th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class TP_Controller : MonoBehaviour
  5. {
  6. public static CharacterController CharacterController;
  7. public static TP_Controller Instance;
  8.  
  9. void Awake ()
  10. {
  11. CharacterController = GetComponent("CharacterController") as CharacterController;
  12. Instance = this;
  13. }
  14.  
  15.  
  16. void Update ()
  17. {
  18. if (Camera.main == null)
  19. return;
  20.  
  21. GetLocomotionInput();
  22.  
  23. TP_Motor.Instance.UpdateMotor();
  24. }
  25.  
  26. void GetLocomotionInput()
  27. {
  28. var deadZone = 0.1f;
  29. TP_Motor.Instance.MoveVector = Vector3.zero;
  30.  
  31. if (Input.GetAxis ("Vertical") > deadZone || Input.GetAxis("Vertical") < -deadZone)
  32. TP_Motor.Instance.MoveVector += new Vector3(0, 0, Input.GetAxis("Vertical"));
  33.  
  34. if (Input.GetAxis ("Horizontal") > deadZone || Input.GetAxis("Horizontal") < -deadZone)
  35. TP_Motor.Instance.MoveVector += new Vector3(Input.GetAxis("Horizontal"), 0, 0);
  36. }
  37. }
  38.  
  39.  
  40.  
  41.  
  42. ------------------------------------
  43.  
  44. using UnityEngine;
  45. using System.Collections;
  46.  
  47. public class TP_Motor : MonoBehaviour
  48. {
  49. public static TP_Motor Instance;
  50.  
  51. public float MoveSpeed = 10f;
  52.  
  53. public Vector3 MoveVector { get; set; }
  54.  
  55. void Awake ()
  56. {
  57. Instance = this;
  58. }
  59.  
  60.  
  61. public void UpdateMotor ()
  62. {
  63. SnapAlignCharacterWithCamera();
  64. ProcessMotion();
  65. }
  66.  
  67. void ProcessMotion()
  68. {
  69. //transform MoveVector to World Space
  70. MoveVector = transform.TransformDirection(MoveVector);
  71.  
  72. //normalize MoveVector if Magnitude > 1
  73. if(MoveVector.magnitude > 1)
  74. MoveVector = Vector3.Normalize(MoveVector);
  75. //Multiply MoveVector by MoveSpeed
  76. MoveVector *= MoveSpeed;
  77.  
  78. //Multiply MoveVector by DeltaTime
  79. MoveVector *= Time.deltaTime;
  80.  
  81. //Move the Character in World Space
  82. TP_Controller.CharacterController.Move(MoveVector);
  83.  
  84.  
  85. }
  86.  
  87. void SnapAlignCharacterWithCamera()
  88. {
  89. if(MoveVector.x != 0 || MoveVector.z != 0)
  90. {
  91. transform.rotation = Quaternion.Euler(transform.eulerAngles.x,
  92. Camera.main.transform.eulerAngles.y,
  93. transform.eulerAngles.z);
  94. }
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement