浏览代码

Fixed player movement axis bug

/main
Nathan 4 年前
当前提交
b09b90e6
共有 1 个文件被更改,包括 57 次插入50 次删除
  1. 107
      UOP1_Project/Assets/Scripts/Characters/Protagonist.cs

107
UOP1_Project/Assets/Scripts/Characters/Protagonist.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine;
public InputReader inputReader;
public Transform gameplayCamera;
public InputReader inputReader;
public Transform gameplayCamera;
private Character charScript;
private bool controlsEnabled = true;
private Character charScript;
private Vector2 previousMovementInput;
private bool controlsEnabled = true;
private void Awake()
{
charScript = GetComponent<Character>();
}
private void Awake()
{
charScript = GetComponent<Character>();
}
//Adds listeners for events being triggered in the InputReader script
private void OnEnable()
{
inputReader.jumpEvent += OnJumpInitiated;
inputReader.jumpCanceledEvent += OnJumpCanceled;
inputReader.moveEvent += OnMove;
//...
}
//Adds listeners for events being triggered in the InputReader script
private void OnEnable()
{
inputReader.jumpEvent += OnJumpInitiated;
inputReader.jumpCanceledEvent += OnJumpCanceled;
inputReader.moveEvent += OnMove;
//...
}
//Removes all listeners to the events coming from the InputReader script
private void OnDisable()
{
inputReader.jumpEvent -= OnJumpInitiated;
inputReader.jumpCanceledEvent -= OnJumpCanceled;
inputReader.moveEvent -= OnMove;
//...
}
private void Update()
{
RecalculateMovement();
}
//Removes all listeners to the events coming from the InputReader script
private void OnDisable()
{
inputReader.jumpEvent -= OnJumpInitiated;
inputReader.jumpCanceledEvent -= OnJumpCanceled;
inputReader.moveEvent -= OnMove;
//...
}
private void RecalculateMovement()
{
//Get the two axes from the camera and flatten them on the XZ plane
Vector3 cameraForward = gameplayCamera.forward;
cameraForward.y = 0f;
Vector3 cameraRight = gameplayCamera.right;
cameraRight.y = 0f;
//---- EVENT LISTENERS ----
//Use the two axes, modulated by the corresponding inputs, and construct the final vector
Vector3 adjustedMovement = cameraRight.normalized * previousMovementInput.x +
cameraForward.normalized * previousMovementInput.y;
private void OnMove(Vector2 movement)
{
if(controlsEnabled)
{
//Get the two axes from the camera and flatten them on the XZ plane
Vector3 cameraForward = gameplayCamera.forward;
cameraForward.y = 0f;
Vector3 cameraRight = gameplayCamera.right;
cameraRight.y = 0f;
charScript.Move(Vector3.ClampMagnitude(adjustedMovement, 1f));
}
//Use the two axes, modulated by the corresponding inputs, and construct the final vector
Vector3 adjustedMovement = cameraRight.normalized * movement.x + cameraForward.normalized * movement.y;
//---- EVENT LISTENERS ----
charScript.Move(Vector3.ClampMagnitude(adjustedMovement, 1f));
}
}
private void OnMove(Vector2 movement)
{
if (controlsEnabled) previousMovementInput = movement;
}
private void OnJumpInitiated()
{
if(controlsEnabled) charScript.Jump();
}
private void OnJumpInitiated()
{
if (controlsEnabled) charScript.Jump();
}
private void OnJumpCanceled()
{
if(controlsEnabled) charScript.CancelJump();
}
private void OnJumpCanceled()
{
if (controlsEnabled) charScript.CancelJump();
}
}
正在加载...
取消
保存