Thanks for watching :)
script:
public float sensitivity;
public float slowSpeed;
public float normalSpeed;
public float sprintSpeed;
float currentSpeed;
void Update()
{
if(Input.GetMouseButton(1)) //if we are holding right click
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
Movement();
Rotation();
}
else
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}
}
public void Rotation()
{
Vector3 mouseInput = new Vector3(-Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0);
transform.Rotate(mouseInput * sensitivity);
Vector3 eulerRotation = transform.rotation.eulerAngles;
transform.rotation = Quaternion.Euler(eulerRotation.x, eulerRotation.y, 0);
}
public void Movement()
{
Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
if(Input.GetKey(KeyCode.LeftShift))
{
currentSpeed = sprintSpeed;
}
else if(Input.GetKey(KeyCode.LeftAlt))
{
currentSpeed = slowSpeed;
}
else
{
currentSpeed = normalSpeed;
}
transform.Translate(input * currentSpeed * Time.deltaTime);
}