【发布时间】: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