编程新手,我目前正在学习某个教程。 我已经按照代码从视频本身复制了它(手写),但是,unity 给了我标题中的问题。 这是代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField] private float moveSpeed = 7f;
[SerializeField] private GameInput gameInput;
private bool isWalking;
private void Update()
{
Vector2 inputVector = gameInput.GetMovementVectorNormalized();
Vector3 moveDir = new Vector3(inputVector.x, 0f, inputVector.y);
float moveDistance = moveSpeed * Time.deltaTime;
float playerRadius = .7f;
float playerHeight = 2f;
bool canMove = !Physics.CapsuleCast(transform.position, transform.position + Vector3.up * playerHeight, playerRadius, moveDir, moveDistance);
if (!canMove)
{
Vector3 moveDirX = new Vector3(moveDir.x, 0, 0).normalized;
canMove = !Physics.CapsuleCast(transform.position, transform.position + Vector3.up * playerHeight, playerRadius, moveDirX, moveDistance);
if (canMove)
{
moveDir = moveDirX;
}
else
{
Vector3 moveDirZ = new Vector3(0, 0, moveDir.z,).normalized;
canMove = !Physics.CapsuleCast(transform.position, transform.position + Vector3.up * playerHeight, playerRadius, moveDirZ, moveDistance);
if (canMove)
{
moveDir = moveDirZ;
}
else
{
//cannot move
}
}
}
if (canMove)
{
transform.position += moveDir * moveDistance;
}
isWalking = moveDir != Vector3.zero;
float rotateSpeed = 10f;
transform.forward = Vector3.Slerp(transform.forward, moveDir, Time.deltaTime * rotateSpeed);
}
public bool IsWalking()
{
return isWalking;
}
}
根据我的理解,问题应该是少了一个字符或者多了一个。我发现 ( 和 ) 括号的数量是匹配的。问题出在哪里?
第 36 行 - 有一个逗号后跟右括号:
moveDir.z,).标准化;
这是一个多余的逗号或者后面应该有一些变量
你在这一行有错别字 Vector3 moveDirZ = new Vector3(0, 0, moveDir.z,).normalized;
应该是
Vector3 moveDirZ = new Vector3(0, 0, moveDir.z).normalized;
Unity 完美地说明了正在发生的事情,请注意在提问之前您需要完美地复制代码。
如果你是编程新手,这是一个日常问题,你需要在寻求帮助之前了解发生了什么。