简体   繁体   English

Object 在 Unity 的旋转循环中

[英]Object in a rotate loop in Unity

I was making a function so that my object can turn around.我正在制作 function 以便我的 object 可以转身。 So I made this function:所以我做了这个function:

void Drehen(){
    if(Input.GetAxis("Horizontal") > 0.1){
        transform.Rotate(new Vector3(0f, 0f, 0f));
        vorne = true;
    }

    if(Input.GetAxis("Horizontal") > -0.1){
        transform.Rotate(new Vector3(0f, 180f, 0f));
        vorne = false;
    }

    
}

The function checks the input if the player goes forwards or backwards and rotates him in the direction via transform.Rotate(new Vector3(0f, 180f, 0f)); function 检查输入是否玩家前进或后退,并通过transform.Rotate(new Vector3(0f, 180f, 0f));

Now, every time I start the game I am able to go forward but as soon as I go backward it flips every side every frame.现在,每次我开始游戏时,我都能够向前 go 但只要我向后 go 它每帧都会翻转每一侧。

Try using尝试使用

transform.Rotate(new Vector3(0f, 180f, 0f), Space.World);

Well that's pretty obvious.嗯,这很明显。

Rotate rotates the object from the current rotation about the given amount so Rotate将 object 从当前旋转旋转到给定的量,所以

transform.Rotate(0,0,0);

does absolutely nothing at all and in绝对什么都不做

transform.Rotate(0,180,0);

you rotate it by 180° every frame.每帧旋转 180°。

What you want is probably rather eg你想要的可能是例如

transform.rotation = Quaternion.Euler(0, 180, 0);

Or actually as alternative you could also do或者实际上作为替代方案,您也可以这样做

var horizontal = Input.GetAxis("Horizontal");

if(Mathf.Abs(horizontal) > 0.1f)
{
    transform.forward = new Vector3(0, 0, horizontal);
}

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

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