Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Movement : MonoBehaviour
- {
- // stuff
- public Camera gameCamera;
- public CharacterController characterController;
- public GameObject gun;
- // mouse
- public float mouseSensitivity = 150f;
- private Vector3 desiredCameraRotation = new Vector3();
- public float sway = 1f;
- public float swayDissipation = 1.1f;
- // movement settings
- public float speed = 15f;
- public float sidewaysSpeed = 13f;
- public float strafeSpeed = 10f;
- bool firstFrame = true; // for fixing movement glitch when game starts
- private Vector3 swayMovement = new Vector3();
- void Start()
- {
- Screen.lockCursor = true;
- }
- void Update()
- {
- float mouseInputX = Input.GetAxis("Mouse X");
- float mouseInputZ = Input.GetAxis("Mouse Y");
- swayMovement += new Vector3(mouseInputX * sway, -mouseInputZ * sway, 0);
- swayMovement /= swayDissipation;
- gun.transform.localPosition = Vector3.Lerp(swayMovement, new Vector3(), 0.1f);
- }
- void FixedUpdate()
- {
- // movement values
- float mouseInputX = Input.GetAxis("Mouse X");
- float mouseInputZ = Input.GetAxis("Mouse Y");
- float horizontalInput = Input.GetAxis("Horizontal");
- float verticalInput = Input.GetAxis("Vertical");
- if (firstFrame == true) { mouseInputX = 0f; mouseInputZ = 0f; firstFrame = false; }
- float usedSpeed = speed;
- float usedSidewaysSpeed = sidewaysSpeed;
- // speed is just retarded by default so decrease it
- horizontalInput /= 50;
- verticalInput /= 50;
- // nerf strafing wtf
- if (horizontalInput != 0 && verticalInput != 0 )
- {
- usedSpeed = (strafeSpeed);
- usedSidewaysSpeed = (strafeSpeed);
- }
- // desired camera rotation for smoothing
- desiredCameraRotation += new Vector3(-mouseInputZ * mouseSensitivity, mouseInputX * mouseSensitivity, 0);
- if (desiredCameraRotation.x > 90)
- {
- desiredCameraRotation = new Vector3(90, 0, 0);
- } else if (desiredCameraRotation.x < -90)
- {
- desiredCameraRotation = new Vector3(-90, 0, 0);
- }
- // camera rotation
- transform.Rotate(new Vector3(0, mouseInputX * mouseSensitivity, 0));
- gameCamera.transform.position = transform.position + new Vector3(0,1f,0);
- gameCamera.transform.localRotation = Quaternion.Euler(desiredCameraRotation);
- //movement
- Vector3 movement = transform.right * usedSidewaysSpeed * horizontalInput + transform.forward * usedSpeed * verticalInput;
- characterController.Move(movement);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement