Guest User

Untitled

a guest
Apr 8th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(Rigidbody))]
  4. public class PlayerMotor : MonoBehaviour {
  5.  
  6. public static PlayerMotor instance;
  7.  
  8. private Vector3 velocity, rotation, cameraRotation;
  9. private Rigidbody rb;
  10. [SerializeField]
  11. private Camera playerCamera;
  12.  
  13. private void Start(){
  14.  
  15.     if (instance == null){
  16.         instance = this;
  17.     }
  18.  
  19.     rb = GetComponent<Rigidbody>();
  20. }
  21.  
  22. private void FixedUpdate(){
  23.  
  24.     //Perform Movement Using Vector Property
  25.     Move();
  26.     //Perform Player Rotation Using rotation Property - (Y-Axis)
  27.     //Perform Camera Rotation Using cameraRotation Property - (X-Axis)
  28.     Rotate();
  29.  
  30. }
  31.  
  32. private void Move(){
  33.     if (velocity != Vector3.zero){
  34.         rb.MovePosition(transform.position + (velocity * Time.fixedDeltaTime));
  35.     }
  36. }
  37.  
  38. private void Rotate(){
  39.     rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation));
  40.  
  41.     if(playerCamera != null){
  42.         playerCamera.transform.Rotate(-cameraRotation);
  43.     }
  44. }
  45.  
  46. public void UpdateVelocity(Vector3 _velocity){
  47.     velocity = _velocity;
  48. }
  49.  
  50. public void UpdateRotation(Vector3 _rotation){
  51.     rotation = _rotation;
  52. }
  53.  
  54. public void UpdateCameraRotation(Vector3 _cameraRotation){
  55.     cameraRotation = _cameraRotation;
  56. }
  57. }
Add Comment
Please, Sign In to add comment