为什么Unity 2020给出错误CS1503



我是Unity和C#代码的新手。我正在做Brackeys的一个基本的循序渐进的教程,没有明显的原因,我得到了这个错误,说";错误CS1503:参数1:无法从"float"转换为"UnityEngine.Vector3";。错误出现在两个rb上。AddForce行。有人知道这里出了什么问题吗?我使用的是Unity 2020.3.29f1个人版。

谢谢你的帮助。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody rb;
public float forwardForce = 2000f;
void FixedUpdate()
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d") )
{
rb.AddForce(500 * Time.deltaTime);
}
if (Input.GetKey("a") )
{
rb.AddForce(-500 * Time.deltaTime);
}
}
}

AddForce有不同的过载。

AddForce(Vector3 [, ForceMode])

AddForce(float, float, float [, ForceMode])

其中CCD_ 2对于两者都是可选的。

您只传入了一个参数float,因此编译器认为您想要使用第一个重载,并试图将给定的float转换为Vector3,但没有成功,显然找不到任何实现方法。

你可能更想要

rb.AddForce(Vector3.right * 500 * Time.deltaTime);

rb.AddForce(500 * Time.deltaTime, 0, 0);

相关内容

最新更新