脚本问题 - Unity5 C#

Je-*_*ora -3 c# unity-game-engine

码:

if (direction.magnitude > 5f) { 
    this.transform.Translate (0, 0, 0.001f);
    anim.SetBool ("isWalking", true);
}// if 

else
    print ("MAGNITUDE: " + direction.magnitude + " IS GREATER THAN 5");
Run Code Online (Sandbox Code Playgroud)

结果:

MAGNITUDE: 0.786 IS GREATER THAN 5
Run Code Online (Sandbox Code Playgroud)

0.786如何变得超过5

方向变量是Vector3,Vector3.magnitude返回float我真的不知道发生了什么.

谢谢.

Hel*_*ium 5

我认为,打印消息应该在if块内

    if (direction.magnitude > 5f) {
        this.transform.Translate (0, 0, 0.001f);
        anim.SetBool ("isWalking", true);
        print ("MAGNITUDE: " + direction.magnitude + " IS GREATER THAN 5");
    }
Run Code Online (Sandbox Code Playgroud)

另一种可能性:比较符号.我想你想要<而不是>

    if (direction.magnitude < 5f) { // ? HERE
        this.transform.Translate (0, 0, 0.001f);
        anim.SetBool ("isWalking", true);
    }// if 
    else
        print ("MAGNITUDE: " + direction.magnitude + " IS GREATER THAN 5");
Run Code Online (Sandbox Code Playgroud)

或许,你需要改变你的信息;)

    if (direction.magnitude > 5f) {
        this.transform.Translate (0, 0, 0.001f);
        anim.SetBool ("isWalking", true);
    }// if 
    else
        print ("MAGNITUDE: " + direction.magnitude + " IS LOWER THAN 5");
Run Code Online (Sandbox Code Playgroud)