gpt4 book ai didi

c# - 如何在Unity中创建简单的FirstPersonController : Compiler ERROR messages?

转载 作者:行者123 更新时间:2023-12-02 10:56:39 24 4
gpt4 key购买 nike

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

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

在此先感谢您的帮助!

错误信息

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

2)Assets \ Scripts \ PlayerController.cs(14,10):错误CS0111:类型“PlayerController”已经使用相同的参数类型定义了一个名为“Awake”的成员

3)Assets \ Scripts \ PlayerController.cs(19,10):错误CS0111:类型“PlayerController”已经使用相同的参数类型定义了一个名为“Update”的成员

4)Assets \ Scripts \ PlayerController.cs(30,10):错误CS0111:类型“PlayerController”已经使用相同的参数类型定义了一个名为“FixedUpdate”的成员

5)Assets \ Scripts \ PlayerController.cs(39,10):错误CS0111:类型“PlayerController”已经使用相同的参数类型定义了一个名为“Move”的成员

CODE/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;
}




}

最佳答案

用Unity开始类似C#的工作是很艰难的,但是经过艰苦的努力,您才能取胜。在代码public class PlayerController : MonoBehaviour的最开始,可能在您的 Assets 文件夹中的某个位置已经有另一个名为PlayerController的脚本。因此,本质上有两个类。尝试在Unity中搜索以查看是否有两个脚本。查看您需要删除的那一个。让我知道这是否对您有帮助。

关于c# - 如何在Unity中创建简单的FirstPersonController : Compiler ERROR messages?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61714948/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com