【问题标题】:Unity Rigidbody统一刚体
【发布时间】:2021-06-14 15:49:41
【问题描述】:

我是 Unity 的新手,我试图让玩家在开始时跳起来。 我有以下代码

using System.Collections;
using UnityEngine;

public class playermove : MonoBehaviour
{
    public float Force = 20f;
    // Start is called before the first frame update
    void Start()
    {
        Rigidbody.AddForce(0f, 0f, 200f);
    }

    // Update is called once per frame
    void Update()
    {
    }
}

它向我抛出以下错误

Assets\playermove.cs(10,9): error CS0120: An object reference is required for the non-static field, method, or property 'Rigidbody.AddForce(float, float, float)'

【问题讨论】:

  • 请使用正确的标签!仅仅因为您使用的是某个 IDE 并不意味着您的问题实际上与该 IDE 有关。请注意,unityscript 是或更好的是曾经是一种 JavaScript 风格,类似于早期 Unity 版本中使用的自定义语言,并且现在早已弃用。你的代码当然在c#

标签: c# unity3d


【解决方案1】:

你需要一个 Rigidbody 类的实例

using System.Collections;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class playermove : MonoBehaviour
{
public Rigidbody rb;
public float Force = 20f;
// Start is called before the first frame update
void Start()
{
    rb = GetComponent<Rigidbody>();
    rb.AddForce(0f, 0f, Force);
}

// Update is called once per frame
void Update()
{
}
}

【讨论】:

  • @derHugo 我不知道 unityscript 是一种已弃用的语言。我认为它与Unity中的脚本有关。对不起。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-06
  • 1970-01-01
  • 2023-02-09
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
  • 1970-01-01
相关资源
最近更新 更多