spacepaste

  1.  
  2. using System;
  3. using UnityEngine;
  4. using UnityStandardAssets.CrossPlatformInput;
  5. namespace UnityStandardAssets.Characters.FirstPerson
  6. {
  7. [RequireComponent(typeof (Rigidbody))]
  8. public class Aim : MonoBehaviour
  9. {
  10. public Camera cam;
  11. public MouseLook mouseLook = new MouseLook();
  12. private float m_YRotation;
  13. private Rigidbody m_RigidBody;
  14. private void Start()
  15. {
  16. m_RigidBody = GetComponent<Rigidbody>();
  17. mouseLook.Init (transform, cam.transform);
  18. }
  19. private void Update()
  20. {
  21. RotateView();
  22. }
  23. private void RotateView()
  24. {
  25. //avoids the mouse looking if the game is effectively paused
  26. if (Mathf.Abs(Time.timeScale) < float.Epsilon) return;
  27. // get the rotation before it's changed
  28. float oldYRotation = transform.eulerAngles.y;
  29. mouseLook.LookRotation (transform, cam.transform);
  30. // Rotate the rigidbody velocity to match the new direction that the character is looking
  31. Quaternion velRotation = Quaternion.AngleAxis(transform.eulerAngles.y - oldYRotation, Vector3.up);
  32. m_RigidBody.velocity = velRotation*m_RigidBody.velocity;
  33. }
  34. }
  35. }
  36.