Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- [RequireComponent(typeof(Rigidbody))]
- public class PlayerMotor : MonoBehaviour {
- public static PlayerMotor instance;
- private Vector3 velocity, rotation, cameraRotation;
- private Rigidbody rb;
- [SerializeField]
- private Camera playerCamera;
- private void Start(){
- if (instance == null){
- instance = this;
- }
- rb = GetComponent<Rigidbody>();
- }
- private void FixedUpdate(){
- //Perform Movement Using Vector Property
- Move();
- //Perform Player Rotation Using rotation Property - (Y-Axis)
- //Perform Camera Rotation Using cameraRotation Property - (X-Axis)
- Rotate();
- }
- private void Move(){
- if (velocity != Vector3.zero){
- rb.MovePosition(transform.position + (velocity * Time.fixedDeltaTime));
- }
- }
- private void Rotate(){
- rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation));
- if(playerCamera != null){
- playerCamera.transform.Rotate(-cameraRotation);
- }
- }
- public void UpdateVelocity(Vector3 _velocity){
- velocity = _velocity;
- }
- public void UpdateRotation(Vector3 _rotation){
- rotation = _rotation;
- }
- public void UpdateCameraRotation(Vector3 _cameraRotation){
- cameraRotation = _cameraRotation;
- }
- }
Add Comment
Please, Sign In to add comment