Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 刚体保持统一运动(AddForce)_C#_Unity3d - Fatal编程技术网

C# 刚体保持统一运动(AddForce)

C# 刚体保持统一运动(AddForce),c#,unity3d,C#,Unity3d,我想用“WASD”键使我的刚体球体移动,到目前为止,除了一个小问题,它还能工作。当我移动刚体时,它会无限地移动 这是我的代码: rig.AddForce(cam.transform.forward * movementForce * Time.deltaTime, ForceMode.VelocityChange); 完整代码:GetKey返回true,同时用户按住key-KeyCode-enum参数标识的键。检查。所以你可能会增加很多力量 可能您需要尝试在帧期间返回true,用户开始按下按名

我想用“WASD”键使我的刚体球体移动,到目前为止,除了一个小问题,它还能工作。当我移动刚体时,它会无限地移动

这是我的代码:

rig.AddForce(cam.transform.forward * movementForce * Time.deltaTime, ForceMode.VelocityChange);

完整代码:

GetKey
返回true,同时用户按住key-KeyCode-enum参数标识的键。检查。所以你可能会增加很多力量


可能您需要尝试在帧期间返回true,用户开始按下按名称标识的键。

如果您不按任何键,则不会停止。。。无论流速是多少,你都要继续前进

还要注意,对于
s
键,将
movementForce*Time.deltaTime*tempAirMultiplier
相乘两次,这很可能会导致预期的力大大降低

我可能会做一些类似的事情

    // Tracks if any of the WASD keys is pressed
    var gotInput = false;

    if (Input.GetKey(KeyCode.W))
    {
        rig.AddForce(cam.transform.forward * movementForce * Time.deltaTime * tempAirMultiplier, ForceMode.VelocityChange);
        gotInput = true;
    }
    if (Input.GetKey(KeyCode.A))
    {
        rig.AddForce(cam.transform.right * -movementForce * Time.deltaTime * tempAirMultiplier, ForceMode.VelocityChange);
        gotInput = true;
    }
    if (Input.GetKey(KeyCode.S))
    {
        var force = cam.transform.forward * -movementForce * Time.deltaTime * tempAirMultiplier; // Get relative movement
        // you can simply erase one component like this, no need to create a new Vector3 instance
        force.y = 0; 
        // Note that force already contains your multipliers "Time.deltaTime * movementForce * tempAirMultiplier"
        rig.AddForce(force, ForceMode.VelocityChange);
        gotInput = true;
    }
    if (Input.GetKey(KeyCode.D))
    {
        rig.AddForce(cam.transform.right * tempAirMultiplier, ForceMode.VelocityChange);
        gotInput = true;
    }

    // If none of the keys is pressed stop the rigidbody
    if(!gotInput)
    {
        rig.veolcity = Vector3.zero;
    }

实际上,我可以通过这样做来解决这个问题。 我的答覆是:

rigid.drag = 1;

在删除FixedUpdate()函数时

为什么要在
FixedUpdate
中执行此操作。。只需一次,甚至已经通过检查员完成即可。。