Unity内置函数CharacterController.移动不工作



我正在尝试执行一个短跑/传送功能,当玩家按下左键时向前移动一小段距离。CharacterController.Move()函数对玩家没有任何影响。我试过用硬编码不同的方向和速度,这没有任何区别。同样的函数在代码的其他地方工作,只是不在我的传送脚本中,我不知道为什么。

脚本附在下面。

using Unity.FPS.Game;
using UnityEngine;
using UnityEngine.Events;
namespace Unity.FPS.Gameplay
{
public class Teleport : MonoBehaviour // Set up function for teleporting
{
// Initialise variables
PlayerCharacterController m_PlayerCharacterController;
PlayerInputHandler m_InputHandler;
public bool IsPlayergrounded() => m_PlayerCharacterController.IsGrounded;
public bool canUseTeleport;
public bool teleporting;
CharacterController m_Controller;
public Vector3 TeleportDirection { get; set; }
public int TeleportSpeed;
void Start()
{
print("The teleport function was called");
// Get variable values
m_PlayerCharacterController = GetComponent<PlayerCharacterController>();
m_InputHandler = GetComponent<PlayerInputHandler>();
m_Controller = GetComponent<CharacterController>();
TeleportDirection = m_Controller.velocity;
TeleportSpeed = 2;
canUseTeleport = true;
}
void Update()
{
// teleport can only be used if not grounded
if (IsPlayergrounded())
{
teleporting = false;
canUseTeleport = true; // reset teleport when landed
}
else if (!m_PlayerCharacterController.HasJumpedThisFrame && m_InputHandler.GetTeleportInputDown() && canUseTeleport)
{
teleporting = true;
}
if (teleporting)
{
// add teleport speed boost
m_PlayerCharacterController.stackedSpeedBoosts += 1;
if (m_PlayerCharacterController.stackedSpeedBoosts > m_PlayerCharacterController.stackLimit)
{
m_PlayerCharacterController.stackedSpeedBoosts = m_PlayerCharacterController.stackLimit;
}
// use the player's velocity to calculate the teleport direction
TeleportDirection = m_Controller.velocity;
// change player's coordinates TODO make this work
m_Controller.Move(TeleportDirection * Time.deltaTime * TeleportSpeed);
// increase player's speed after teleporting
m_PlayerCharacterController.TeleportBoost = true;
// can only teleport once before hitting the ground
canUseTeleport = false;
teleporting = false;
}
}
}
}

我发现问题了!显然,如果你试图在每帧中多次使用移动函数,它就会被破坏。所以比起在我的传送脚本中使用移动功能,我现在只使用传送脚本来改变速度,然后将我所有的移动信息组合在一个运行CharacterController的主移动脚本中。移动功能。

最新更新