Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- [RequireComponent(typeof(Rigidbody))]
- public class PlayerMotor : MonoBehaviour {
- [SerializeField]
- private Camera cam;
- private Vector3 velocity = Vector3.zero;
- private Vector3 rotation = Vector3.zero;
- private Vector3 cameraRotation = Vector3.zero;
- private Rigidbody rb;
- void Start ()
- {
- rb = GetComponent<Rigidbody>();
- }
- // Gets a movement vector
- public void Move(Vector3 _velocity)
- {
- velocity = _velocity;
- }
- // Gets a Rotate vector
- public void Rotate(Vector3 _rotation)
- {
- rotation = _rotation;
- }
- // Gets a Rotate vector for camera
- public void RotateCamera(Vector3 _cameraRotation)
- {
- cameraRotation = _cameraRotation;
- }
- //Run Physicals Literation
- void FixedUpdate()
- {
- PerformMovement();
- PerformRotation();
- }
- void PerformMovement ()
- {
- if (velocity != Vector3.zero)
- {
- rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
- }
- }
- //Perform Rotation
- void PerformRotation ()
- {
- rb.MoveRotation(rb.rotation * Quaternion.Euler (rotation));
- if (cam != null)
- {
- cam.transform.Rotate(-cameraRotation);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment