unity3d Unity/C#If语句始终为真[已关闭]

zxlwwiss  于 2022-11-16  发布在  C#
关注(0)|答案(2)|浏览(139)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

11天前关闭。
Improve this question
为什么总是这样?

public float speed;
    bool working = false;
    
    // Start is called before the first frame update
    void Start()
    {
        skilling = false;
        speed = 3f;
        
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 playerInput = new Vector3(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"), 0);
        transform.position = transform.position + playerInput.normalized * speed * Time.deltaTime;
        
        if(speed >= 500f)
            {
                print("test");
            }
    }

最初我想使条件基于“工作”布尔值,以及键输入。它总是真的。
我快疯了
测试测试测试测试测试
我试着用例子中的Float、bool和collision来实现If语句,结果总是正确的,我一定是在什么地方搞砸了,但是我找不到。
编辑:完整代码:`

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class player : MonoBehaviour
{
    float speed;
    bool working = false;
    
    // Start is called before the first frame update
    void Start()
    {
        
        speed = 3f;
        
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 playerInput = new Vector3(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"), 0);
        transform.position = transform.position + playerInput.normalized * speed * Time.deltaTime;
        
        if(speed >= 500f)
            {
                print("test");
            }
    }
    /*void OnTriggerEnter2D(Collider2D other) {
        if(other.tag == "Skill")
            {
                //skilling = true;
            }
        else skilling = false;
    }*/
}

“我把公众从速度变量中去掉了,这仍然是真的。

ajsxfq5m

ajsxfq5m1#

我很抱歉浪费你的时间,我已经找到了解决办法。我在另一个脚本中有一个循环,也返回“测试”。这是我得到一个愚蠢的原因2小时生气了。

3bygqnnd

3bygqnnd2#

可能的解决方案:
1-请确保您没有从检查器中修改速度变量,因为它是公共的。
2-确保其他等级在开始时没有改变速度变量。

相关问题