darkstaride

PlayerController.cs

Mar 24th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.35 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(PlayerMotor))]
  4. public class PlayerController : MonoBehaviour{
  5.  
  6.     [SerializeField]
  7.     private float speed = 5f;
  8.     [SerializeField]
  9.     private float LookSensitivity = 3f;
  10.    
  11.     //Caching
  12.     private PlayerMotor motor;
  13.  
  14.     void Start ()
  15.     {
  16.         motor = GetComponent<PlayerMotor>();
  17.     }
  18.  
  19.     void Update ()
  20.     {
  21.         //Calculate movement velocity as a 3D vector
  22.         float _xMov = Input.GetAxis("Horizontal");
  23.         float _zMov = Input.GetAxis("Vertical");
  24.  
  25.         Vector3 _movHorizontal = transform.right * _xMov;
  26.         Vector3 _movVertical = transform.forward * _zMov;
  27.  
  28.         // Final movement vector
  29.         Vector3 _velocity = (_movHorizontal + _movVertical) * speed;
  30.  
  31.         //Apply movement
  32.         motor.Move(_velocity);
  33.  
  34.         //Calculate rotation as a 3D vector (turning around)
  35.         float _xRot = Input.GetAxisRaw("Mouse X");
  36.  
  37.         Vector3 _rotation = new Vector3(0f, _xRot, 0f) * LookSensitivity;
  38.  
  39.         //Apply rotation
  40.         motor.Rotate(_rotation);
  41.  
  42.         //Calculate rotation as a 3D vector (turning around)
  43.         float _yRot = Input.GetAxisRaw("Mouse Y");
  44.  
  45.         Vector3 _cameraRotation = new Vector3(0f, _yRot, 0f) * LookSensitivity;
  46.  
  47.         //Apply rotation
  48.         motor.RotateCamera(_cameraRotation);
  49.  
  50.  
  51.     }
  52. }
Add Comment
Please, Sign In to add comment