You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

首次制作游戏遇PlayerMovement脚本编译错误求助

解决Unity玩家移动脚本的两个编译错误

错误1:CS0101 命名空间中已包含'PlayerMovement'的定义

  • 检查项目Assets/Scripts目录,确认是否存在两个同名的PlayerMovement.cs文件(可能是重复创建或误复制),删除多余文件。
  • 若只有一个文件,打开脚本检查是否在同一文件内重复定义了PlayerMovement类(比如不小心粘贴了两次完整代码),删掉重复的类代码块。

错误2:CS0111 类型'PlayerMovement'已定义了一个名为'FixedUpdate'的同名参数成员

  • 打开PlayerMovement.cs脚本,检查是否存在两个FixedUpdate()方法(可能是复制代码时重复粘贴了函数块),删除多余的方法。
  • 确认方法签名完全一致(返回值、参数均相同),Unity不允许同一类中存在完全同名同参数的方法。

修正后的验证脚本

确保你的脚本最终内容如下(与你提供的代码一致,无重复定义即可):

using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
    public Rigidbody rb;
    public float forwardForce = 2000f;
    public float sidewaysForce = 500f;
    
    // Update is called once per frame
    void FixedUpdate()
    {
        rb.AddForce(0, 0, forwardForce * Time.deltaTime);
        if (Input.GetKey("d"))
        {
            rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }
        if (Input.GetKey("a"))
        {
            rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }
    }
}

额外注意:需给玩家对象挂载Rigidbody组件,否则运行时会触发空引用错误。

内容的提问来源于stack exchange,提问作者Dragos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 09:20:59