- Home /
Get Direction based on Velocity
Currently, I'm working on a type of teleportation for my FPS and I've gotten it to preserve the local position and rotation of the player based on where you are on the object.
However, after teleporting and if the player releases the movement button, the players remaining velocity won't adjust to the local rotation so it continues in it's original direction. like so: Currently, this is how the player velocity is calculated:
MovementVector = (((directionIntentY * zSaved) + (directionIntentX * xSaved)) * TotalSpeed);
The directionIntentY and X are the Camera direction, Total Speed is the movement speed and xSaved+zSaved are the movement axis retrieved from the input.
x = Input.GetAxis("Horizontal");
z = Input.GetAxis("Vertical");
if (movement button is being held)
{
xSaved = x;
zSaved = z;
}
Originally, I was adjusting the Camera look position during the teleport to preserve the direction which at first glance works however If the player changes where they are looking before teleporting, their velocity direction will change after teleporting based on their velocity and where they were looking. All I want is the original velocity direction to be preserved.
//PlayerMove is just a reference to the player object.
PlayerMove.directionIntentX = PlayerMove.Camera.right;
PlayerMove.directionIntentX.y = 0;
PlayerMove.directionIntentX.Normalize();
PlayerMove.directionIntentY = PlayerMove.Camera.forward;
PlayerMove.directionIntentY.y = 0;
PlayerMove.directionIntentY.Normalize();
Looking deeper into the issue, I'm hoping I can solve the issue via the xSaved & zSaved variable since those 2 variables control the direction of the movement. I'm hoping by changing those variables to match the new direction, the velocity direction will match up with the local velocity direction of the teleporter.
From the Teleport Object script, I tried using the movement vector, Camera direction, transform.forward to get the appropriate normalised direction:
Vector3 DirNorm =transform.InverseTransformDirection(PlayerMove.MovementVector).normalized;
//PlayerMove is just a reference to the player object.
PlayerMove.xSaved = DirNorm.x;
PlayerMove.zSaved = DirNorm.z;
However none of the vector3 values I got matched up with the intended direction I can avoid this issue by ensuring all teleporters share the same rotation however I just want to allow some creativity with their placement so what can I do?