Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine.SceneManagement;
  4. using UnityEngine;
  5.  
  6. public class Controls : MonoBehaviour {
  7. public float speed;
  8. private Rigidbody rb;
  9. Vector3 movement;
  10. Vector3 movementRotation;
  11.  
  12. public Vector3 jump;
  13. public float jumpForce;
  14. public float gravity;
  15. public float jumpModifier;
  16.  
  17. public ThirdPersonCamera gamecam;
  18. private float directionSpeed = 3.0f;
  19.  
  20. private float moveHorizontal;
  21. private float moveVertical;
  22. private float speedPlus = 0.0f;
  23. private float direction = 0f;
  24. private Vector3 moveDirection;
  25.  
  26. public bool isGrounded = true;
  27.  
  28. private Vector3 curLoc;
  29. private Vector3 prevLoc;
  30.  
  31. void Awake(){
  32. rb = GetComponent<Rigidbody> ();
  33. jump = new Vector3(0.0f, 2.0f, 0.0f);
  34. Physics.gravity = new Vector3(0, -gravity, 0);
  35. }
  36.  
  37. void OnCollisionStay() {
  38. isGrounded = true;
  39. }
  40.  
  41. void Update () {
  42.  
  43. StickToWorldspace (this.transform, gamecam.transform, ref direction, ref speedPlus);
  44. ControllPlayer();
  45. Debug.Log (moveDirection);
  46. }
  47.  
  48. void ControllPlayer()
  49. {
  50. moveHorizontal = Input.GetAxis ("Horizontal");
  51. moveVertical = Input.GetAxis ("Vertical");
  52. movement = new Vector3 (direction, 0.0f, direction);
  53. movementRotation = new Vector3 (moveHorizontal, 0.0f, moveVertical);
  54.  
  55. if (isGrounded == true) {
  56. rb.velocity = new Vector3 (moveHorizontal, 0f, moveVertical).normalized * speed;
  57. } else {
  58. rb.AddForce (movement * speed * jumpModifier);
  59. }
  60.  
  61. if (Input.GetKey (KeyCode.Space) || Input.GetKey (KeyCode.Joystick1Button1) && isGrounded){
  62. rb.velocity = new Vector3 (rb.velocity.x, 12.0f, rb.velocity.z);
  63. isGrounded = false;
  64. }
  65.  
  66.  
  67. if (movement.sqrMagnitude > 0.1f && isGrounded){
  68. transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (movementRotation), 1F);
  69. }
  70.  
  71. }
  72.  
  73. public void StickToWorldspace(Transform root, Transform camera, ref float directionOut, ref float speedOut){
  74.  
  75. Vector3 rootDirection = root.forward;
  76.  
  77. Vector3 stickDirection = new Vector3 (moveHorizontal, 0, moveVertical);
  78.  
  79. speedOut = stickDirection.sqrMagnitude;
  80.  
  81. Vector3 CameraDirection = camera.forward;
  82. CameraDirection.y = 0.0f;
  83. Quaternion referentialShift = Quaternion.FromToRotation (rootDirection, Vector3.Normalize(CameraDirection));
  84.  
  85. Vector3 moveDirection = referentialShift * stickDirection;
  86. Vector3 axisSign = Vector3.Cross (moveDirection, rootDirection);
  87.  
  88. Debug.DrawRay (new Vector3 (root.position.x, root.position.y + 5f, root.position.z), moveDirection, Color.green);
  89. Debug.DrawRay (new Vector3 (root.position.x, root.position.y + 5f, root.position.z), axisSign * 2, Color.red);
  90. Debug.DrawRay (new Vector3 (root.position.x, root.position.y + 5f, root.position.z), rootDirection, Color.magenta);
  91.  
  92. float angleRootToMove = Vector3.Angle (rootDirection, moveDirection) * (axisSign.y >= 0 ? -1f : 1f);
  93.  
  94. angleRootToMove /= 0f;
  95.  
  96. directionOut = angleRootToMove * directionSpeed;
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement