【问题标题】:How to create a simple FirstPersonController in Unity: Compiler ERROR messages?如何在 Unity 中创建一个简单的 FirstPersonController:编译器错误消息?
【发布时间】:2020-05-10 16:05:53
【问题描述】:

我对 Unity 和编码非常陌生,所以我绝对是初学者。我正在尝试在遵循教程的同时从头开始创建自己的“FirstPersonController”。但是,大多数教程都提供了代码,但是当我在 Unity 脚本中使用它时它不起作用,我不断地看到错误。

我将提供错误消息和我编写的代码,希望有人能提供帮助,因为我不知道自己在做什么。

提前感谢您的帮助!

错误消息

1) Assets\Scripts\PlayerController.cs(3,14):错误 CS0101:命名空间 '' 已包含 'PlayerController' 的定义

2) Assets\Scripts\PlayerController.cs(14,10): error CS0111: Type 'PlayerController' 已经定义了一个名为 'Awake' 的成员,具有相同的参数类型

3) Assets\Scripts\PlayerController.cs(19,10): error CS0111: Type 'PlayerController' 已经定义了一个名为 'Update' 且参数类型相同的成员

4) Assets\Scripts\PlayerController.cs(30,10): error CS0111: Type 'PlayerController' 已经定义了一个名为 'FixedUpdate' 的成员,具有相同的参数类型

5) Assets\Scripts\PlayerController.cs(39,10): error CS0111: Type 'PlayerController' 已经定义了一个名为 'Move' 的成员,具有相同的参数类型

代码/C# 脚本

    using UnityEngine;

public class PlayerController : MonoBehaviour
{
    //Public Variables

    public float walkspeed;

    //Private Variables

    Rigidbody rb;
    Vector3 moveDirection;

    void Awake()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        //Non-Physics steps
        //Get directional input from the user

        float horizontalMovement = Input.GetAxisRaw("Horizontal");
        float verticalMovement = Input.GetAxisRaw("Vertical");

        moveDirection = (horizontalMovement * transform.right + verticalMovement * transform.forward).normalized;
    }

    void FixedUpdate()
    {
        //Physics steps
        //Call the Move function

        Move();

    }

    void Move()
    {
        //Here we define the move funtion
        //Rigid.velocity is a method which takes a Vector3 and controls the speed and direction of the GameObject

        rb.velocity = moveDirection * walkspeed * Time.deltaTime;
    }




}

【问题讨论】:

    标签: c# visual-studio unity3d compiler-errors game-physics


    【解决方案1】:

    用 Unity 开始像 C# 这样的东西很困难,但只要努力工作,你就能取得胜利。在您的代码public class PlayerController : MonoBehaviour 的最开始处,您的资产文件夹中的某处可能已经存在另一个名为PlayerController 的脚本。所以本质上有两个类。尝试在 Unity 中搜索以查看是否有两个脚本。看看你需要删除哪一个。如果这对您有帮助,请告诉我。

    【讨论】:

      猜你喜欢
      • 2011-02-27
      • 1970-01-01
      • 1970-01-01
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-26
      • 1970-01-01
      相关资源
      最近更新 更多