Vstudio 2022/Unity2021.3.3f1 CS0103 和 CS0168
Vstudio 2022/Unity2021.3.3f1 CS0103 and CS0168
我目前正在做一个“类灵魂”项目来帮助我学习 Unity/c#。我在 youtube 上找到了一个很棒的教程,并按照它进行了 T。但是在尝试测试第一段代码时,我 运行 进入许多 errors/warnings.
Error CS0103 The name 'inputHandler' does not exist in the current context
这个错误出现在我多次命名和使用的多个变量上,但它无法弄清楚它们引用的是什么?我认为?我是一个完全的初学者,这真的让我很沮丧。我不确定该教程是否已过时并且 unity 中的输入控制系统是否已更新以对其中一些部分使用不同的名称?在这一点上,我什至不知道这是否有意义大声笑。这是“playerLocomotion”脚本的代码,“InputHandler”脚本没有错误,看起来没问题。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace NN
{
public class PlayerLocomotion : MonoBehaviour
{
void Start()
{
Transform cameraObject;
InputHandler inputHandler;
Vector3 moveDirection;
}
[HideInInspector]
public Transform myTransform;
public new Rigidbody rigidbody;
public GameObject normalCamera;
[Header("Stats")]
[SerializeField]
float movementSpeed = 5;
[SerializeField]
float rotationSpeed = 10;
void start()
{
rigidbody = GetComponent<Rigidbody>();
inputHandler = GetComponent<InputHandler>();
cameraObject = Camera.main.transform;
myTransform = transform;
}
public void update()
{
float delta = Time.deltaTime;
inputHandler.tickInput(delta);
moveDirection = cameraObject.forward * inputHandler.vertical;
moveDirection += cameraObject.right * inputHandler.horizontal;
moveDirection.Normalize();
float speed = movementspeed;
moveDirection *= speed;
Vector3 projectedVelocity = Vector3.ProjectOnPlane(moveDirection, normalVector);
rigidbody.velocity = projectedVelocity;
}
#region Movement
Vector3 normalVector;
Vector3 targetPosition;
private void HandleRotation(float delta)
{
Vector3 targetDir = Vector3.zero;
float moveOverride = inputHandler.moveAmount;
targetDir = cameraObject.forward * inputHandler.vertical;
targetDir = normalcamera.right * inputHandler.Horizontal;
targetDir.Normalize();
targetDir.y = 0;
if (targetDir == Vector3.zero)
targetDir = myTransform.forward;
float rs = rotationspeed;
Quaternion tr = Quaternion.LookRotation(targetDir);
Quaternion targetRotation = Quaternion.Slerp(myTransform.rotation, tr, rs * delta);
myTransform.rotation = targetRotation;
}
#endregion
}
}
Start 方法中定义的变量因其作用域而在其他任何地方都不可用。尝试将它们放在方法之外。本教程介绍了 c# https://www.tutorialsteacher.com/articles/variable-scopes-in-csharp
中的作用域
还有一些大写错误,所以我也在示例中更改了它们。我在它抛出错误的地方添加了评论。
我还想补充一点,最好在 class 的开头定义所有 class 关卡变量。这样你就不会忘记分散在 class.
中的变量
using UnityEngine;
namespace NN
{
public class PlayerLocomotion : MonoBehaviour
{
//Putting these variables inside a method makes them method scoped variables, meaning that you can only access them inside this method
//If you want to access them in the rest of the class they need to be defined in the class
// void Start()
// {
// Transform cameraObject;
// InputHandler inputHandler;
// Vector3 moveDirection;
// }
//This would be class level variables and these are now accessible in the rest of the class
Transform cameraObject;
InputHandler inputHandler;
Vector3 moveDirection;
[HideInInspector]
public Transform myTransform;
public new Rigidbody rigidbody;
public GameObject normalCamera;
[Header("Stats")]
[SerializeField]
float movementSpeed = 5;
[SerializeField]
float rotationSpeed = 10;
void start()
{
rigidbody = GetComponent<Rigidbody>();
inputHandler = GetComponent<InputHandler>();
cameraObject = Camera.main.transform;
myTransform = transform;
}
public void update()
{
float delta = Time.deltaTime;
inputHandler.tickInput(delta);
moveDirection = cameraObject.forward * inputHandler.vertical;
moveDirection += cameraObject.right * inputHandler.horizontal;
moveDirection.Normalize();
// float speed = movementspeed; //This also contained a capitalization error
float speed = movementSpeed; //Should be movementSpeed instead of movementspeed
moveDirection *= speed;
Vector3 projectedVelocity = Vector3.ProjectOnPlane(moveDirection, normalVector);
rigidbody.velocity = projectedVelocity;
}
#region Movement
Vector3 normalVector;
Vector3 targetPosition;
private void HandleRotation(float delta)
{
Vector3 targetDir = Vector3.zero;
float moveOverride = inputHandler.moveAmount;
targetDir = cameraObject.forward * inputHandler.vertical;
// targetDir = normalcamera.right * inputHandler.Horizontal; //should also be normalCamera instead of normalcamera
//The normalcamera is also a gameobject so it doesn't have a right, so you'll first have to get the transform
targetDir = normalCamera.transform.right * inputHandler.Horizontal;
targetDir.Normalize();
targetDir.y = 0;
if (targetDir == Vector3.zero)
targetDir = myTransform.forward;
// float rs = rotationspeed;//This should also be rotationSpeed instead of rotationspeed
float rs = rotationSpeed;
Quaternion tr = Quaternion.LookRotation(targetDir);
Quaternion targetRotation = Quaternion.Slerp(myTransform.rotation, tr, rs * delta);
myTransform.rotation = targetRotation;
}
#endregion
}
}
我目前正在做一个“类灵魂”项目来帮助我学习 Unity/c#。我在 youtube 上找到了一个很棒的教程,并按照它进行了 T。但是在尝试测试第一段代码时,我 运行 进入许多 errors/warnings.
Error CS0103 The name 'inputHandler' does not exist in the current context
这个错误出现在我多次命名和使用的多个变量上,但它无法弄清楚它们引用的是什么?我认为?我是一个完全的初学者,这真的让我很沮丧。我不确定该教程是否已过时并且 unity 中的输入控制系统是否已更新以对其中一些部分使用不同的名称?在这一点上,我什至不知道这是否有意义大声笑。这是“playerLocomotion”脚本的代码,“InputHandler”脚本没有错误,看起来没问题。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace NN
{
public class PlayerLocomotion : MonoBehaviour
{
void Start()
{
Transform cameraObject;
InputHandler inputHandler;
Vector3 moveDirection;
}
[HideInInspector]
public Transform myTransform;
public new Rigidbody rigidbody;
public GameObject normalCamera;
[Header("Stats")]
[SerializeField]
float movementSpeed = 5;
[SerializeField]
float rotationSpeed = 10;
void start()
{
rigidbody = GetComponent<Rigidbody>();
inputHandler = GetComponent<InputHandler>();
cameraObject = Camera.main.transform;
myTransform = transform;
}
public void update()
{
float delta = Time.deltaTime;
inputHandler.tickInput(delta);
moveDirection = cameraObject.forward * inputHandler.vertical;
moveDirection += cameraObject.right * inputHandler.horizontal;
moveDirection.Normalize();
float speed = movementspeed;
moveDirection *= speed;
Vector3 projectedVelocity = Vector3.ProjectOnPlane(moveDirection, normalVector);
rigidbody.velocity = projectedVelocity;
}
#region Movement
Vector3 normalVector;
Vector3 targetPosition;
private void HandleRotation(float delta)
{
Vector3 targetDir = Vector3.zero;
float moveOverride = inputHandler.moveAmount;
targetDir = cameraObject.forward * inputHandler.vertical;
targetDir = normalcamera.right * inputHandler.Horizontal;
targetDir.Normalize();
targetDir.y = 0;
if (targetDir == Vector3.zero)
targetDir = myTransform.forward;
float rs = rotationspeed;
Quaternion tr = Quaternion.LookRotation(targetDir);
Quaternion targetRotation = Quaternion.Slerp(myTransform.rotation, tr, rs * delta);
myTransform.rotation = targetRotation;
}
#endregion
}
}
Start 方法中定义的变量因其作用域而在其他任何地方都不可用。尝试将它们放在方法之外。本教程介绍了 c# https://www.tutorialsteacher.com/articles/variable-scopes-in-csharp
中的作用域还有一些大写错误,所以我也在示例中更改了它们。我在它抛出错误的地方添加了评论。
我还想补充一点,最好在 class 的开头定义所有 class 关卡变量。这样你就不会忘记分散在 class.
中的变量using UnityEngine;
namespace NN
{
public class PlayerLocomotion : MonoBehaviour
{
//Putting these variables inside a method makes them method scoped variables, meaning that you can only access them inside this method
//If you want to access them in the rest of the class they need to be defined in the class
// void Start()
// {
// Transform cameraObject;
// InputHandler inputHandler;
// Vector3 moveDirection;
// }
//This would be class level variables and these are now accessible in the rest of the class
Transform cameraObject;
InputHandler inputHandler;
Vector3 moveDirection;
[HideInInspector]
public Transform myTransform;
public new Rigidbody rigidbody;
public GameObject normalCamera;
[Header("Stats")]
[SerializeField]
float movementSpeed = 5;
[SerializeField]
float rotationSpeed = 10;
void start()
{
rigidbody = GetComponent<Rigidbody>();
inputHandler = GetComponent<InputHandler>();
cameraObject = Camera.main.transform;
myTransform = transform;
}
public void update()
{
float delta = Time.deltaTime;
inputHandler.tickInput(delta);
moveDirection = cameraObject.forward * inputHandler.vertical;
moveDirection += cameraObject.right * inputHandler.horizontal;
moveDirection.Normalize();
// float speed = movementspeed; //This also contained a capitalization error
float speed = movementSpeed; //Should be movementSpeed instead of movementspeed
moveDirection *= speed;
Vector3 projectedVelocity = Vector3.ProjectOnPlane(moveDirection, normalVector);
rigidbody.velocity = projectedVelocity;
}
#region Movement
Vector3 normalVector;
Vector3 targetPosition;
private void HandleRotation(float delta)
{
Vector3 targetDir = Vector3.zero;
float moveOverride = inputHandler.moveAmount;
targetDir = cameraObject.forward * inputHandler.vertical;
// targetDir = normalcamera.right * inputHandler.Horizontal; //should also be normalCamera instead of normalcamera
//The normalcamera is also a gameobject so it doesn't have a right, so you'll first have to get the transform
targetDir = normalCamera.transform.right * inputHandler.Horizontal;
targetDir.Normalize();
targetDir.y = 0;
if (targetDir == Vector3.zero)
targetDir = myTransform.forward;
// float rs = rotationspeed;//This should also be rotationSpeed instead of rotationspeed
float rs = rotationSpeed;
Quaternion tr = Quaternion.LookRotation(targetDir);
Quaternion targetRotation = Quaternion.Slerp(myTransform.rotation, tr, rs * delta);
myTransform.rotation = targetRotation;
}
#endregion
}
}