- Home /
Question by
rogueghost · Jun 07, 2019 at 09:12 PM ·
c#movementcontrollerjumping
CharacterController Jump from moving platform.
Hi,
I followed the example in a Unity Answers question (link in the bottom) and implemented support for moving surfaces on the CharacterController, but now i can't jump off of surfaces that are moving, only when they are not moving. Does anyone know how to fix this issue i'm having? Could you test this? Here is the full code:
public class Player : MonoBehaviour
{
public CharacterController controller;
public Transform activePlatform;
private Vector3 activeLocalPlatformPoint;
private Vector3 activeGlobalPlatformPoint;
private Vector3 lastPlatformVelocity;
private void Update()
{
if(activePlatform != null)
{
var newGlobalPlatformpoint = activePlatform.TransformPoint(activeLocalPlatformPoint);
var moveDistance = (newGlobalPlatformpoint - activeGlobalPlatformPoint);
if (moveDistance != Vector3.zero)
controller.Move(moveDistance);
lastPlatformVelocity = (newGlobalPlatformpoint - activeGlobalPlatformPoint) / Time.deltaTime;
}
else
{
lastPlatformVelocity = Vector3.zero;
}
activePlatform = null;
//ACTUAL MOVEMENT:
Vector3 vectorInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
Move(vectorInput);
if (activePlatform != null)
{
activeGlobalPlatformPoint = transform.position;
activeLocalPlatformPoint = activePlatform.InverseTransformPoint(transform.position);
}
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.moveDirection.y < -0.9 && hit.normal.y > 0.5)
activePlatform = hit.collider.transform;
}
public void Move(Vector3 input)
{
if (controller.isGrounded)
{
speedVertical = 0f;
if (Input.GetButtonDown("Jump"))
{
speedVertical = Mathf.Sqrt(2 * speedJump * gravity);
print("Jumping...");
}
}
speedVertical -= gravity * Time.deltaTime;
Vector3 vectorMove = input.normalized * speedMove;
vectorMove.y = speedVertical;
vectorMove = Vector3.ClampMagnitude(vectorMove, speedMax);
controller.Move(vectorMove * Time.deltaTime);
}
}
Thanks in advance! Link: https://answers.unity.com/questions/8207/charactercontroller-falls-through-or-slips-off-mov.html
Comment