简体   繁体   中英

[Unity]How to rotate the camera automatically without dragging?

I am not good in English. I'm sorry.

My question is "How can the camera rotate automatically without dragging?"

I implemented it until camera rotation using drag.

But I don't know how to let my camera rotate as the mouse moves.

You can make a boolean that checks if the camera rotation is activated, and then have a function that sets that false/true respectively when input is received.

In your inputmanager make the following:

public bool isRotating;
void ToggleIfShouldRotate(){
    isRotating = !isRotating;
}

!IsRotating will always be the opposite of isRotating.

You also need to decide which key should activate or deactivate the rotation, you can do this with a Keycode variable.

[SerializeField]
KeyCode ToggleRotatingKey = KeyCode.Mouse0;

I have set it as default to Mouse0, which I believe is the left mouse button. You can change the value in the inspector or in code.

Now use this key in the Update method to toggle the rotation on and off, like this.

void Update()
{
    if (Input.GetKeyDown(ToggleRotatingKey))
    {
        ToggleIfShouldRotate();
    }
}

EDIT:

Now in your rotation script, simply check the boolean value isRotating from the InputManager.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM