我正在做 tps 的一些原型,但我遇到了错误
NullReferenceException: Object reference not set to an instance of an object
IdleState.EnterState () (at Assets/Scripts/Character/Player/MovementState/States/Grounded/States/IdleState.cs:10)
StateMachine.ChangeState (PlayerBase newState) (at Assets/Scripts/StateMachine/StateMachine.cs:9)
Player.Start () (at Assets/Scripts/Character/Player/Player.cs:27)
&
NullReferenceException: Object reference not set to an instance of an object
PlayerMovementState.GetMovementSpeed () (at Assets/Scripts/Character/Player/MovementState/States/PlayerMovementState.cs:52)
PlayerMovementState.Move () (at Assets/Scripts/Character/Player/MovementState/States/PlayerMovementState.cs:37)
PlayerMovementState.UpdateState () (at Assets/Scripts/Character/Player/MovementState/States/PlayerMovementState.cs:24)
IdleState.UpdateState () (at Assets/Scripts/Character/Player/MovementState/States/Grounded/States/IdleState.cs:15)
StateMachine.UpdateState () (at Assets/Scripts/StateMachine/StateMachine.cs:14)
Player.Update () (at Assets/Scripts/Character/Player/Player.cs:33)
有文件代码
public abstract class PlayerBase
{
public abstract void EnterState();
public abstract void UpdateState();
public abstract void ExitState();
public abstract void HandleInput();
}
public abstract class StateMachine
{
protected PlayerBase _currentState;
public void ChangeState(PlayerBase newState)
{
_currentState?.ExitState();
_currentState = newState;
_currentState.EnterState();
}
public void UpdateState()
{
_currentState?.UpdateState();
}
public void ExitState()
{
_currentState?.ExitState();
}
public void HandleInput()
{
_currentState?.HandleInput();
}
}
using UnityEngine;
public class PlayerMovementState : PlayerBase
{
protected PlayerMovementStateMachine _stateMachine;
protected PlayerGroundedData _movementData;
public PlayerMovementState(PlayerMovementStateMachine stateMachine)
{
}
#region Base Methods
public override void EnterState()
{
Debug.Log("State " + GetType().Name);
}
public override void ExitState()
{
}
public override void UpdateState()
{
Move();
}
public override void HandleInput()
{
ReadMovementInput();
}
#endregion
#region Main Methods
private void Move()
{
float movementSpeed = GetMovementSpeed();
Vector2 direction = MovementDirection();
_stateMachine.Player.Controller.Move(direction * movementSpeed * Time.deltaTime);
}
private Vector2 ReadMovementInput()
{
_stateMachine.ReusableData.Movement = _stateMachine.Player.Input.PlayerAction.Movement.ReadValue<Vector2>();
return _stateMachine.ReusableData.Movement;
}
private float GetMovementSpeed()
{
return _movementData.BaseSpeed * _stateMachine.ReusableData.MovementSpeedModifier;
}
#endregion
#region Reusable Methods
private Vector2 MovementDirection()
{
return new Vector2(_stateMachine.ReusableData.Movement.x, _stateMachine.ReusableData.Movement.y);
}
#endregion
}
using UnityEngine;
public class Player : MonoBehaviour
{
public CharacterController Controller;
private PlayerMovementStateMachine _movementStateMachine;
public PlayerInput Input { get; private set; }
public Transform PlayerBody;
public Transform GroundPosition;
private void Awake()
{
Controller = GetComponent<CharacterController>();
_movementStateMachine = new PlayerMovementStateMachine(this);
PlayerBody = GameObject.FindGameObjectWithTag(Tags.PlayerBody).GetComponent<Transform>();
GroundPosition = GameObject.FindGameObjectWithTag(Tags.GroundCheck).GetComponent<Transform>();
}
private void Start()
{
if(_movementStateMachine != null)
{
_movementStateMachine.ChangeState(_movementStateMachine.IdleState);
}
}
private void Update()
{
_movementStateMachine.UpdateState();
_movementStateMachine.HandleInput();
}
}
public class IdleState : PlayerMovementState
{
public IdleState(PlayerMovementStateMachine stateMachine) : base(stateMachine)
{
}
public override void EnterState()
{
base.EnterState();
_stateMachine.ReusableData.MovementSpeedModifier = 0;
}
public override void UpdateState()
{
base.UpdateState();
if(_stateMachine.Player.Controller.velocity.magnitude > 0f)
{
_stateMachine.ChangeState(_stateMachine.RunState);
}
}
}
using UnityEngine;
public class PlayerStateReusableData
{
public Vector2 Movement { get; set; }
public float MovementSpeedModifier { get; set; } = 1f;
}
我在 ytb 上搜索,询问了 chatgpt,但我仍然不明白缺少什么。 如果我能收到一些提示,我将不胜感激
............................................... ..................................................... .