Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PlayerMovement : MonoBehaviour
- {
- //camera variables
- Vector2 _mouseAbsolute;
- Vector2 _smoothMouse;
- public Vector2 clampInDegrees = new Vector2(360, 180);
- public bool lockCursor;
- public Vector2 sensitivity = new Vector2(2, 2);
- public Vector2 smoothing = new Vector2(3, 3);
- public Vector2 targetDirection;
- public Vector2 targetCharacterDirection;
- public GameObject characterBody;
- //movement variables
- public float speedright;
- public float speedleft;
- public float speedforwards;
- public float speedback;
- public float heightspeed;
- public float timesincelastjump;
- public float jumpdelay;
- public float speedwhilesprintright;
- public float speedwhilesprintleft;
- public float speedwhilesprintforwards;
- public float speedwhilesprintback;
- public GameObject Player;
- public GameObject Camera;
- public Rigidbody PlayerRigid;
- private bool Sprinting;
- private bool jumping;
- void Start()
- {
- PlayerRigid = GetComponent<Rigidbody>();
- // Set target direction to the camera's initial orientation.
- targetDirection = transform.localRotation.eulerAngles;
- // Set target direction for the character body to its inital state.
- if (characterBody) targetCharacterDirection = characterBody.transform.localRotation.eulerAngles;
- }
- void Update ()
- {
- timesincelastjump += Time.deltaTime;
- if (Input.GetKey(KeyCode.W))
- {
- PlayerRigid.AddForce(transform.forward * speedforwards * Time.deltaTime);
- }
- if (Input.GetKey(KeyCode.A))
- {
- PlayerRigid.AddForce((transform.right * -1) * speedright * Time.deltaTime);
- }
- if (Input.GetKey(KeyCode.D))
- {
- PlayerRigid.AddForce(transform.right * speedright * Time.deltaTime);
- }
- if (Input.GetKey(KeyCode.S))
- {
- PlayerRigid.AddForce((transform.forward * -1) * speedright * Time.deltaTime);
- }
- if (Input.GetKeyDown(KeyCode.Space) && timesincelastjump >= jumpdelay)
- {
- PlayerRigid.AddForce(transform.up * heightspeed * Time.deltaTime);
- timesincelastjump = 0;
- }
- if (Input.GetKey(KeyCode.LeftShift))
- {
- Sprinting = true;
- speedright = speedwhilesprintright;
- speedforwards = speedwhilesprintforwards;
- speedleft = speedwhilesprintleft;
- speedback = speedwhilesprintback;
- }
- if (!Input.GetKey(KeyCode.LeftShift))
- //change this if you want to change base speeds
- {
- Sprinting = false;
- speedright = 4;
- speedforwards = 4;
- speedleft = 4;
- speedback = 4;
- }
- if(lockCursor)
- {
- if(Input.GetKeyDown(KeyCode.Escape))
- {
- lockCursor = !lockCursor;
- }
- // Ensure the cursor is always locked when set
- Screen.lockCursor = lockCursor;
- // Allow the script to clamp based on a desired target value.
- Quaternion targetOrientation = Quaternion.Euler(targetDirection);
- Quaternion targetCharacterOrientation = Quaternion.Euler(targetCharacterDirection);
- // Get raw mouse input for a cleaner reading on more sensitive mice.
- Vector2 mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
- // Scale input against the sensitivity setting and multiply that against the smoothing value.
- mouseDelta = Vector2.Scale(mouseDelta, new Vector2(sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));
- // Interpolate mouse movement over time to apply smoothing delta.
- _smoothMouse.x = Mathf.Lerp(_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
- _smoothMouse.y = Mathf.Lerp(_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);
- // Find the absolute mouse movement value from point zero.
- _mouseAbsolute += _smoothMouse;
- // Clamp and apply the local x value first, so as not to be affected by world transforms.
- if (clampInDegrees.x < 360)
- _mouseAbsolute.x = Mathf.Clamp(_mouseAbsolute.x, -clampInDegrees.x * 0.5f, clampInDegrees.x * 0.5f);
- Quaternion xRotation = Quaternion.AngleAxis(-_mouseAbsolute.y, targetOrientation * Vector3.right);
- transform.localRotation = xRotation;
- // Then clamp and apply the global y value.
- if (clampInDegrees.y < 360)
- _mouseAbsolute.y = Mathf.Clamp(_mouseAbsolute.y, -clampInDegrees.y * 0.5f, clampInDegrees.y * 0.5f);
- transform.localRotation *= targetOrientation;
- // If there's a character body that acts as a parent to the camera
- if (characterBody)
- {
- Quaternion yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, characterBody.transform.up);
- characterBody.transform.localRotation = yRotation;
- characterBody.transform.localRotation *= targetCharacterOrientation;
- }
- else
- {
- Quaternion yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, transform.InverseTransformDirection(Vector3.up));
- transform.localRotation *= yRotation;
- }
- }
- else if(Input.GetKeyDown(KeyCode.Escape))
- {
- lockCursor = !lockCursor;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement