Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. When I move using WASD, and my camera can rotate around my figure using my mouse, it gets really confusing. Because WASD don't change, so if I rotate around my figure, W doesn't move me forward based on my camera, it moves me forward based on the world. How Can I change this? Here is my character script and camera script:
  2.  
  3.  
  4.  
  5. Character:
  6.  
  7.  
  8. public class testscript : MonoBehaviour {
  9.  
  10. public float speed = 6.0F;
  11. public float jumpSpeed = 8.0F;
  12. public float gravity = 20.0F;
  13. private Vector3 moveDirection = Vector3.zero;
  14.  
  15. private CharacterController controller;
  16.  
  17. private void Awake()
  18. {
  19. controller = GetComponent<CharacterController>();
  20. }
  21.  
  22. void Update()
  23. {
  24. if (controller.isGrounded)
  25. {
  26. moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  27.  
  28. Quaternion rotation = Quaternion.LookRotation(moveDirection, Vector3.up);
  29. if (moveDirection != Vector3.zero)
  30. transform.rotation = rotation;
  31.  
  32. moveDirection *= speed;
  33. if (Input.GetButton("Jump"))
  34. {
  35. moveDirection.y = jumpSpeed;
  36. }
  37.  
  38. Debug.DrawLine(transform.position, moveDirection);
  39.  
  40. }
  41. moveDirection.y -= gravity * Time.deltaTime;
  42. controller.Move(moveDirection * Time.deltaTime);
  43. }
  44. }
  45.  
  46.  
  47.  
  48. Camera:
  49.  
  50. using UnityEngine;
  51. using System.Collections;
  52.  
  53. [AddComponentMenu("Camera-Control/Mouse Orbit with zoom")]
  54. public class MouseOrbitImproved : MonoBehaviour {
  55.  
  56. public Transform target;
  57. public float distance = 5.0f;
  58. public float xSpeed = 120.0f;
  59. public float ySpeed = 120.0f;
  60.  
  61. public float yMinLimit = -20f;
  62. public float yMaxLimit = 80f;
  63.  
  64. public float distanceMin = .5f;
  65. public float distanceMax = 15f;
  66.  
  67. private Rigidbody rigidbody;
  68.  
  69. float x = 0.0f;
  70. float y = 0.0f;
  71.  
  72. // Use this for initialization
  73. void Start ()
  74. {
  75. Vector3 angles = transform.eulerAngles;
  76. x = angles.y;
  77. y = angles.x;
  78.  
  79. rigidbody = GetComponent<Rigidbody>();
  80.  
  81. // Make the rigid body not change rotation
  82. if (rigidbody != null)
  83. {
  84. rigidbody.freezeRotation = true;
  85. }
  86. }
  87.  
  88. void LateUpdate ()
  89. {
  90. if (target)
  91. {
  92. x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
  93. y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
  94.  
  95. y = ClampAngle(y, yMinLimit, yMaxLimit);
  96.  
  97. Quaternion rotation = Quaternion.Euler(y, x, 0);
  98.  
  99. distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
  100.  
  101. RaycastHit hit;
  102. if (Physics.Linecast (target.position, transform.position, out hit))
  103. {
  104. distance -= hit.distance;
  105. }
  106. Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
  107. Vector3 position = rotation * negDistance + target.position;
  108.  
  109. transform.rotation = rotation;
  110. transform.position = position;
  111. }
  112. }
  113.  
  114. public static float ClampAngle(float angle, float min, float max)
  115. {
  116. if (angle < -360F)
  117. angle += 360F;
  118. if (angle > 360F)
  119. angle -= 360F;
  120. return Mathf.Clamp(angle, min, max);
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement