这个问题是来自超级用户的migrated,因为它可以在Stack Overflow. Migrated上回答2天前。
我和一些朋友正在开发一个Unity游戏,无意中发现了主移动脚本中的一个错误。你们中的任何一个人可以帮助我吗?这是移动脚本的代码。我对第一个和第二个错误有问题,因为第三个重复变量已经被修复。我使用his tutorials进行游戏。Image of the error messages
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
using UnityEngine.Events;
public class Player : MonoBehaviour{
[SerializeField] float mouseSensitivity=3f;
[SerializeField] float movementSpeed=5f;
[SerializeField] float mass=1f;
[SerializeField] float acceleration=20f;
[SerializeField] Transform cameraTransform;
public event Action OnBeforeMove;
internal float movementSpeedMultiplier;
public bool IsGrounded=>controller.isGrounded;
public event Action<bool> OnGroundStateChange;
internal float movementSpeedMultiplier;
CharacterController controller;
internal Vector3 velocity;
Vector2 look;
bool wasGrounded;
PlayerInput playerInput;
InputAction moveAction;
InputAction lookAction;
InputAction sprintAction;
void Awake(){
controller=GetComponent<CharacterController>();
playerInput=GetComponent<PlayerInput>();
moveAction=playerInput.actions["move"];
lookAction=playerInput.actions["look"];
sprintAction=playerInput.actions["sprint"];
}
void Start(){
Cursor.lockState=CursorLockMode.Locked; //Locks Cursor//
}
void Update(){
UpdateGround();
UpdateGravity();
UpdateMovement();
UpdateLook();
}
void UpdateGround(){
if(wasGrounded!=IsGrounded){
OnGroundStateChange?.Invoke();
wasGrounded=IsGrounded;
}
}
void UpdateGravity(){
var gravity=Physics.gravity*mass*Time.deltaTime;
velocity.y=controller.isGrounded?-1f:velocity.y+gravity.y;
}
Vector3 GetMovementInput(){
var moveInput=moveAction.ReadValue<Vector2>();
var input=new Vector3();
input+=transform.forward*moveInput.y;
input+=transform.right*moveInput.x;
input=Vector3.ClampMagnitude(input,1f);
input*=movementSpeed*movementSpeedMultiplier;
return input;
}
void UpdateMovement(){
movementSpeedMultiplier=1f;
OnBeforeMove?.Invoke();
var input=GetMovementIput();
var factor=acceleration*Time.deltaTime;
velocity.x=Mathf.Lerp(velocity.x,input.x,factor);
velocity.z=Mathf.Lerp(velocity.z,input.z,factor);
controller.Move(velocity*Time.deltaTime);
}
void UpdateLook(){
var lookInput=lookAction.ReadValue<Vector2>();
look.x+=lookInput.x*mouseSensitivity;
look.y+=lookInput.y*mouseSensitivity;
look.y=Mathf.Clamp(look.y,-89f,89f);
//Debug.Log(look);//
cameraTransform.localRotation=Quaternion.Euler(-look.y,0,0);
transform.localRotation=Quaternion.Euler(0,look.x,0);
}
}
我还将链接错误消息
1条答案
按热度按时间xzv2uavs1#
您缺少
using System;
,请将其添加到文件的顶部。Action
委托家族(Action
,Action<T>
等)来自System
命名空间。