I can’t seem to find a solution to my jumping, when I press the jump button it looks like I re-enter the freelookstate and the jump animation will get cut.
Does anyone know what’s happening here?
I can’t seem to find a solution to my jumping, when I press the jump button it looks like I re-enter the freelookstate and the jump animation will get cut.
Does anyone know what’s happening here?
Is it consistently jumping the first time and then cutting off early like it’s trying to jump every time after that?
I wonder if this is an issue in the ForceReceiver…
Paste in your PlayerJumpState.cs, PlayerFallingState.cs, and ForceReceiver.cs and we’ll take a look.
Thanks Brian, hopefully it’s something as simple as me missing one thing that I can’t see.
public class ForceReceiver : MonoBehaviour
{
[SerializeField] private CharacterController controller;
[SerializeField] private NavMeshAgent agent;
[SerializeField] private float drag = 0.3f;
private Vector3 dampingVelocity;
private Vector3 impact;
private float verticalVelocity;
public Vector3 Movement => impact + Vector3.up * verticalVelocity;
private void Update()
{
if (verticalVelocity < 0f && controller.isGrounded)
{
verticalVelocity = Physics.gravity.y * Time.deltaTime;
}
else
{
verticalVelocity += Physics.gravity.y * Time.deltaTime;
}
impact = Vector3.SmoothDamp(impact, Vector3.zero, ref dampingVelocity, drag);
if (agent != null)
{
if(impact.sqrMagnitude < 0.2f * 0.2f)
{
impact = Vector3.zero;
agent.enabled = true;
}
}
}
public void Reset()
{
impact = Vector3.zero;
verticalVelocity = 0f;
}
public void AddForce(Vector3 force)
{
impact += force;
if (agent != null)
{
agent.enabled = false;
}
}
public void Jump(float jumpForce)
{
verticalVelocity += jumpForce;
}
}
public class PlayerJumpingState : PlayerBaseState
{
private readonly int JumpEnterHash = Animator.StringToHash("JumpEnter");
private Vector3 momentum;
private const float CrossFadeDuration = 0.1f;
public PlayerJumpingState(PlayerStateMachine stateMachine) : base(stateMachine){}
public override void Enter()
{
stateMachine.ForceReceiver.Jump(stateMachine.JumpForce);
momentum = stateMachine.Controller.velocity;
momentum.y = 0f;
stateMachine.Animator.CrossFadeInFixedTime(JumpEnterHash, CrossFadeDuration);
stateMachine.LedgeDetector.OnLedgeDetect += HandleLedgeDetect;
}
public override void Tick(float deltaTime)
{
Move(momentum, deltaTime);
if(stateMachine.Controller.velocity.y <= 0f)
{
stateMachine.SwitchState(new PlayerFallingState(stateMachine));
return;
}
FaceTarget();
}
public override void Exit()
{
stateMachine.LedgeDetector.OnLedgeDetect -= HandleLedgeDetect;
}
private void HandleLedgeDetect(Vector3 ledgeForward, Vector3 closestPoint)
{
stateMachine.SwitchState(new PlayerHangingState(stateMachine, ledgeForward, closestPoint));
}
}
public class PlayerFallingState : PlayerBaseState
{
private readonly int JumpFallHash = Animator.StringToHash("JumpFall");
private Vector3 momentum;
private float CrossFadeDuration = 0.1f;
public PlayerFallingState(PlayerStateMachine stateMachine) : base(stateMachine){}
public override void Enter()
{
momentum = stateMachine.Controller.velocity;
momentum.y = 0f;
stateMachine.Animator.CrossFadeInFixedTime(JumpFallHash, CrossFadeDuration);
stateMachine.LedgeDetector.OnLedgeDetect += HandleLedgeDetect;
}
public override void Tick(float deltaTime)
{
Move(momentum,deltaTime);
if(stateMachine.Controller.isGrounded)
{
ReturnToLocomotion();
}
FaceTarget();
}
public override void Exit()
{
stateMachine.LedgeDetector.OnLedgeDetect -= HandleLedgeDetect;
}
private void HandleLedgeDetect(Vector3 ledgeForward, Vector3 closestPoint)
{
stateMachine.SwitchState(new PlayerHangingState(stateMachine, ledgeForward, closestPoint));
}
}
I’m not seeing anything immediately apparent here.
Zip up your project and upload it to https://gdev.tv/projectupload
Be sure to remove the Library folder to remove space.
I have found where the issue is coming from now. My character controller isn’t moving with my animation and it is constantly returning isGrounded.
For the time being I’ve found a work around by setting to stay in the jumpingState for a small amount of time.
I’m looking at possibly using a boxcast to handle the ground detection.
Thanks Brian for taking the time to look at this.
remainingJumpTime -= deltaTime;
If(remainingJumpTime <= 0f)
{
stateMachine.SwitchState(new
PlayerFallingState(stateMachine));
return;
}