简体   繁体   English

[Unity]如何在不拖动的情况下自动旋转相机?

[英][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. 您可以创建一个布尔值,以检查是否激活了相机旋转,然后具有一个函数,该函数在接收到输入时分别设置为false / true。

In your inputmanager make the following: 在输入管理器中进行以下操作:

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

!IsRotating will always be the opposite of isRotating. !IsRotating将始终与isRotating相反。

You also need to decide which key should activate or deactivate the rotation, you can do this with a Keycode variable. 您还需要确定应激活或停用旋转的键,可以使用Keycode变量来执行此操作。

[SerializeField]
KeyCode ToggleRotatingKey = KeyCode.Mouse0;

I have set it as default to Mouse0, which I believe is the left mouse button. 我将其默认设置为Mouse0,我相信它是鼠标左键。 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. 现在,在Update方法中使用此键可以像这样切换开和关的旋转。

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

EDIT: 编辑:

Now in your rotation script, simply check the boolean value isRotating from the InputManager. 现在,在您的旋转脚本中,只需检查InputManager中的布尔值isRotating。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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