Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1. public class Movement : MonoBehaviour
  2. {
  3.     // stuff
  4.     public Camera gameCamera;
  5.     public CharacterController characterController;
  6.     public GameObject gun;
  7.  
  8.     // mouse
  9.     public float mouseSensitivity = 150f;
  10.     private Vector3 desiredCameraRotation = new Vector3();
  11.     public float sway = 1f;
  12.     public float swayDissipation = 1.1f;
  13.  
  14.     // movement settings
  15.     public float speed = 15f;
  16.     public float sidewaysSpeed = 13f;
  17.     public float strafeSpeed = 10f;
  18.  
  19.     bool firstFrame = true; // for fixing movement glitch when game starts
  20.     private Vector3 swayMovement = new Vector3();
  21.     void Start()
  22.     {
  23.         Screen.lockCursor = true;
  24.     }
  25.  
  26.     void Update()
  27.     {
  28.         float mouseInputX = Input.GetAxis("Mouse X");
  29.         float mouseInputZ = Input.GetAxis("Mouse Y");
  30.  
  31.         swayMovement += new Vector3(mouseInputX * sway, -mouseInputZ * sway, 0);
  32.         swayMovement /= swayDissipation;
  33.  
  34.         gun.transform.localPosition = Vector3.Lerp(swayMovement, new Vector3(), 0.1f);    
  35.     }
  36.     void FixedUpdate()
  37.     {
  38.         // movement values
  39.         float mouseInputX = Input.GetAxis("Mouse X");
  40.         float mouseInputZ = Input.GetAxis("Mouse Y");
  41.         float horizontalInput = Input.GetAxis("Horizontal");
  42.         float verticalInput = Input.GetAxis("Vertical");
  43.  
  44.         if (firstFrame == true) { mouseInputX = 0f; mouseInputZ = 0f; firstFrame = false; }
  45.  
  46.         float usedSpeed = speed;
  47.         float usedSidewaysSpeed = sidewaysSpeed;
  48.  
  49.         // speed is just retarded by default so decrease it
  50.         horizontalInput /= 50;
  51.         verticalInput /= 50;
  52.  
  53.         // nerf strafing wtf
  54.         if (horizontalInput != 0 && verticalInput != 0 )
  55.         {
  56.             usedSpeed = (strafeSpeed);
  57.             usedSidewaysSpeed = (strafeSpeed);
  58.         }
  59.  
  60.         // desired camera rotation for smoothing
  61.         desiredCameraRotation += new Vector3(-mouseInputZ * mouseSensitivity, mouseInputX * mouseSensitivity, 0);
  62.  
  63.         if (desiredCameraRotation.x > 90)
  64.         {
  65.             desiredCameraRotation = new Vector3(90, 0, 0);
  66.         } else if (desiredCameraRotation.x < -90)
  67.         {
  68.             desiredCameraRotation = new Vector3(-90, 0, 0);
  69.         }
  70.  
  71.         // camera rotation
  72.         transform.Rotate(new Vector3(0, mouseInputX * mouseSensitivity, 0));
  73.         gameCamera.transform.position = transform.position + new Vector3(0,1f,0);
  74.         gameCamera.transform.localRotation = Quaternion.Euler(desiredCameraRotation);
  75.  
  76.         //movement
  77.         Vector3 movement = transform.right * usedSidewaysSpeed * horizontalInput + transform.forward * usedSpeed * verticalInput;
  78.  
  79.         characterController.Move(movement);
  80.  
  81.  
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement