darkstaride

PlayerMotor.cs

Mar 24th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3.  
  4. [RequireComponent(typeof(Rigidbody))]
  5. public class PlayerMotor : MonoBehaviour {
  6.  
  7.     [SerializeField]
  8.     private Camera cam;
  9.  
  10.     private Vector3 velocity = Vector3.zero;
  11.     private Vector3 rotation = Vector3.zero;
  12.     private Vector3 cameraRotation = Vector3.zero;
  13.  
  14.     private Rigidbody rb;
  15.  
  16.     void Start ()
  17.     {
  18.         rb = GetComponent<Rigidbody>();
  19.     }
  20.  
  21.     // Gets a movement vector
  22.     public void Move(Vector3 _velocity)
  23.     {
  24.         velocity = _velocity;
  25.     }
  26.  
  27.     // Gets a Rotate vector
  28.     public void Rotate(Vector3 _rotation)
  29.     {
  30.         rotation = _rotation;
  31.     }
  32.  
  33.     // Gets a Rotate vector for camera
  34.     public void RotateCamera(Vector3 _cameraRotation)
  35.     {
  36.         cameraRotation = _cameraRotation;
  37.     }
  38.  
  39.  
  40.     //Run Physicals Literation
  41.     void FixedUpdate()
  42.     {
  43.         PerformMovement();
  44.         PerformRotation();
  45.     }
  46.  
  47.     void PerformMovement ()
  48.     {
  49.         if (velocity != Vector3.zero)
  50.         {
  51.             rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
  52.         }
  53.     }
  54.  
  55.     //Perform Rotation
  56.     void PerformRotation ()
  57.     {
  58.         rb.MoveRotation(rb.rotation * Quaternion.Euler (rotation));
  59.         if (cam != null)
  60.         {
  61.             cam.transform.Rotate(-cameraRotation);
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment