How do I rotate this?

using UnityEngine;
using UnityEngine.InputSystem;

public class VirtualCamController : MonoBehaviour
{
    private Controls controls;
    public Transform virtualcam;
    private Vector2 vector2;

    void Awake()
    {
        controls = new Controls();
    }

    void OnEnable()
    {
        controls.Enable();
    }

    void OnDisable()
    {
        controls.Disable();
    }

    void LateUpdate()
    {
        virtualcam.Rotate(GetAxisCustom("Mouse X"), GetAxisCustom("Mouse Y"), 0f);
    }

    public float GetAxisCustom(string axisname)
    {
        vector2 = controls.AllControls.Camera.ReadValue<Vector2>();

        if (axisname == "Mouse X")
        {
            return vector2.x;
        }

        if (axisname == "Mouse Y")
        {
            return -vector2.y;
        }

        return 0;
    }
}

At the moment this is rotating the camera all over including all axes. I just want to rotate the camera transform only by the X and Y rotation, but this is also doing the Z. I need to clamp as well I think, could anyone help me edit this please? :slight_smile:

The easiest way to do this is to not use .Rotate, which adds the value you supply to its current rotation. The issue is just geometry: If you rotate right 90 degrees, then up 90 degrees, then left 90 degrees, you’ll end up facing the same way you started, but with a 90 degree roll. This is where your phantom Z rotation is coming from.

If you fully overwrite the rotation every frame, you won’t get this problem. With your code, you can do that with:

virtualcam.rotation = Quaternion.Euler(GetAxisCustom("Mouse X"), GetAxisCustom("Mouse Y"), 0f);

...

vector2 += controls....

This basically moves the “cumulative” aspect of the rotation to the Vector2 instead of the quaternion, where it won’t combine in strange ways.

By the way, I think you should be swapping your x and y? When you move the mouse X, usually you want the object to rotate on the Y axis (around the vertical axis, so it’s like shaking your head “no”), and vice versa. I don’t know the rest of your input setup though, so maybe this is swapped elsewhere.

1 Like

Hi, thanks for that. I think I get what you mean by going around all the way and coming back to the same point.

And yes I had to change both the X and Y and it looks like this now:

using UnityEngine;
using UnityEngine.InputSystem;

public class VirtualCamController : MonoBehaviour
{
    private Controls controls;
    public Transform virtualcam;
    private Vector2 vector2;

    void Awake()
    {
        controls = new Controls();
    }

    void OnEnable()
    {
        controls.Enable();
    }

    void OnDisable()
    {
        controls.Disable();
    }

    void LateUpdate()
    {
        virtualcam.rotation = Quaternion.Euler(GetAxisCustom("Mouse X"), GetAxisCustom("Mouse Y"), 0f);
        //virtualcam.Rotate(GetAxisCustom("Mouse X"), GetAxisCustom("Mouse Y"), 0f);
    }

    public float GetAxisCustom(string axisname)
    {
        vector2 += controls.AllControls.Camera.ReadValue<Vector2>();

        if (axisname == "Mouse X")
        {
            return -vector2.y;
        }

        if (axisname == "Mouse Y")
        {
            return vector2.x;
        }

        return 0;
    }
}

Currently it does the rotation but not quite where I want it. Is it possible to have a starting rotation by default and then modify it with the controls? Also how do I add restrictions of going too far with clamping? :slight_smile:

If I understand what you are trying to do, the easiest way to do it is child the camera. Rotate the parent on one axis and the camera on the other.

1 Like

Yep, you can assign a default value to your vector2 variable on line 2. You could also make that public and tweak the starting value in the inspector.

vector2.x = Mathf.Clamp(vector2.x, -45f, 45f);
1 Like

Thank you! I will try that next time, sounds easy.

Thanks! I got it to work somehow. But right now I am faced with a new problem, how do I get this virtual cinemachine camera to also follow the player around and not have the rotations affected by it?

I tried the Look At and Follow, but that overrides the transform:

The position in particular. I can’t have my own position.

I also tried parenting it to the player, but my player rotates around all the time which is how my game goes, so I can’t have the camera rolling as well. Any ideas?

EDIT: Nvm, I just realized I can set the position to the player’s position, and then add an offset of choice to get it where I want it. Thanks again.