繁体   English   中英

为什么整数不能从统一转换?

[英]why can't integer convert from unity?

我刚刚遇到错误 CS1504: argument 4: cannot convert from 'int' to 'UnityEngine.ForceMode' in Unity。

这是我写的代码

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    // This is a reference to the Rigidbody component called "rb"
    public Rigidbody rb;

    public float forwardForce = 2000f;  // Variable that determines the forward force
    public float sidewaysForce = 500f;  // Variable that determines the sideways force

    // We marked this as "Fixed"Update because we
    // are using it to mess with physics.
    void FixedUpdate()
    {
        // Add a forward force
        rb.AddForce(0, 0, forwardForce * Time.deltaTime);

        if (!Input.GetKey("d"))  // If the player is pressing the "d" key
        {
        }
        else
        {
            // Add a force to the right
            global::System.Object value = rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, 50);
        }

        if (Input.GetKey("a"))  // If the player is pressing the "a" key
        {
            // Add a force to the left
            global::System.Object value = rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, 50);
        }
    }
}

有人可以帮助我并指出我做错了什么吗?

来自rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, 50); ,最后一个值应该类似于ForceMode.Impulse

例如。 rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.Impulse);

参考: https ://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

rb.AddForce(0, 0, forwardForce * Time.deltaTime); 工作是因为它已经有了 ForceMode 的默认值,即 ForceMode.Force。

public void AddForce(float x, float y, float z, ForceMode mode = ForceMode.Force);

暂无
暂无

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

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