How to make a spectator/free camera in Unity! Quick & Easy!
Vložit
- čas přidán 16. 03. 2023
- 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);
} - Hry
Exactly what I was looking for, super easy to modify and add to while also being easy to understand. Keep up the great work!
you got me 5 grades in my multimedia project. thanks .
Thanks for the script, helped me a lot !
I know a fair bit of coding but I am awful at movement scripts in 3D, stupendous job! This was exactly what I was looking for after hours of searching, works perfectly.
I am glad you liked it! Movement scripts in 3D are still confusing to me at times too, so don’t worry! :)
Great stuff thanks. It should be built-in like in UE.
is anyone else having an issue where the camera is spinning extremely fast on the y axis when looking straight up or down?
Thanks for the Script, it saves me time