unity3d 当尝试检测RayCastHit时,为什么每秒钟“if input.axis”语句都失败而没有错误?

fhg3lkii  于 2022-12-30  发布在  其他
关注(0)|答案(2)|浏览(137)

我正在创建一个3D自上而下的游戏,我想让一个字符移动,虽然我使用光线投射,以确保当字符碰撞时,它确实继续改变它的位置时,字符试图移动对它。
我已经创建了如下代码:

[SerializeField] private float speed = 5;

    public float XMovement;
    public float ZMovement;
    public Vector3 RotationY;

    void FixedUpdate()
    {

        MovementSetting();
        

        var dir = new Vector3(XMovement, 0, ZMovement);
        transform.Translate(dir * speed * Time.deltaTime);
    }

    
    void MovementSetting()
    {
        var ray = new Ray();
        RaycastHit hit;

        // If input is "a"
        if (Input.GetAxis("Horizontal") < 0)
        {
            RotationY = transform.TransformDirection(Vector3.left);
            ray = new Ray(this.transform.position, RotationY);

            if (Physics.Raycast(ray, out hit, 0.6f)) { XMovement = 0; }
            else { XMovement = Input.GetAxis("Horizontal"); }
        }
        else { XMovement = 0; }

        // If input is "d"
        if (Input.GetAxis("Horizontal") > 0)
        {
            RotationY = transform.TransformDirection(Vector3.right);
            ray = new Ray(this.transform.position, RotationY);

            if (Physics.Raycast(ray, out hit, 0.6f)) { XMovement = 0; }
            else { XMovement = Input.GetAxis("Horizontal"); }
        }
        else { XMovement = 0; }

        // If input is "s"
        if (Input.GetAxis("Vertical") < 0)
        {
            RotationY = transform.TransformDirection(Vector3.back);
            ray = new Ray(this.transform.position, RotationY);

            if (Physics.Raycast(ray, out hit, 0.6f)) { ZMovement = 0; }
            else { ZMovement = Input.GetAxis("Vertical"); }
        }
        else { ZMovement = 0; }

        // If input is "w"
        if (Input.GetAxis("Vertical") > 0)
        {
            RotationY = transform.TransformDirection(Vector3.forward);
            ray = new Ray(this.transform.position, RotationY);

            if (Physics.Raycast(ray, out hit, 0.6f)) { ZMovement = 0; }
            else { ZMovement = Input.GetAxis("Vertical"); }
        }
        else { ZMovement = 0; }

        
    }

理论上,这意味着工作,虽然,只有每一秒“如果输入”语句100%工作
我发现,对于每一个奇数“if input”,只要它达到if (Physics.Raycast(ray, out hit, 0.6f)),它就停止工作,尽管不会抛出错误代码
我尝试使用debug.log来查看“if physics”响应中是否有任何响应,但我没有得到。
我还尝试了(愚蠢的想法)看看我是否将“if input”语句加倍两次,因此每隔一次都应该起作用,但那也不起作用

kx7yvsdv

kx7yvsdv1#

您使用else条件,然后创建一个条件,要求输入else中已经输入的case,这会让您觉得“奇怪”的事情起作用,只需删除“else's”并将其放入一个条件中,以检查值是否为0

eivnm1vs

eivnm1vs2#

它们没有失败,但是第二个if语句可能会恢复第一个语句设置的XMovement

// Let's assume the horizontal axis is < 0 ...
if (Input.GetAxis("Horizontal") < 0) {
    XMovement = ...; // ... then a value will be assigend here.
} else {
    XMovement = 0;
}

if (Input.GetAxis("Horizontal") > 0) {
    // We said it's < 0, so this code will not be executed
}
else {
    XMovement = 0; // however, this one will
                   // and thus the value we assigned earlier will be cleared!
}

让我们重新组织代码

float horizontalAxis = Input.GetAxis("Horizontal");
if (horizontalAxis < 0) {
    XMovement = ...;
} else if (horizontalAxis > 0) {
    XMovement = ...;
} else { // horizontalAxis == 0
    XMovement = 0;
}

现在,总是恰好一个分支被执行。

相关问题