How to make a spectator/free camera in Unity! Quick & Easy!

Sdílet
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

Komentáře • 8

  • @enragementgame
    @enragementgame Před rokem +5

    Exactly what I was looking for, super easy to modify and add to while also being easy to understand. Keep up the great work!

  • @abdulkareemmousa3891
    @abdulkareemmousa3891 Před 7 měsíci +1

    you got me 5 grades in my multimedia project. thanks .

  • @olivierbabu4094
    @olivierbabu4094 Před 6 měsíci

    Thanks for the script, helped me a lot !

  • @paremyoutube
    @paremyoutube Před rokem +1

    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.

    • @pineappledevv
      @pineappledevv  Před rokem

      I am glad you liked it! Movement scripts in 3D are still confusing to me at times too, so don’t worry! :)

  • @madluper
    @madluper Před 6 měsíci +1

    Great stuff thanks. It should be built-in like in UE.

  • @ivymothz
    @ivymothz Před 10 měsíci

    is anyone else having an issue where the camera is spinning extremely fast on the y axis when looking straight up or down?

  • @richierich450
    @richierich450 Před 8 měsíci +1

    Thanks for the Script, it saves me time