'Unity inbuilt function CharacterController.Move not working
I'm trying to implement a dash/teleport function where the player moves forward a short distance when left shift is pressed. The CharacterController.Move() function isn't having any effect on the player. I've tried it with hardcoding different directions and speeds and that doesn't have any difference. The same function is working elsewhere in the code, just not inside my teleport script, and I don't know why.
Script attached below.
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;
}
}
}
}
Solution 1:[1]
I found the issue! Apparently the move function gets broken if you try to use it more than once per frame. So instead of using the move function in my teleport script, I'm now only using the teleport script to change velocity and am then combining all my movement info together in one main movement script that runs the CharacterController.Move function.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | The Impossible Squish |
