Je rencontre un problème avec ma variable "characterController.isGrounded" dans ma State Machine.
Je suis dans le "Grounded State", puis j'appuie sur le bouton de saut et je me retrouve dans le "Jump State".
Jusqu'ici tout va bien, les animations sont bonnes, le personnage saute à la bonne hauteur, redescend comme je veux mais une fois au sol, donc "isGrounded" doit être égal à true. Cependant, le personnage ne revient pas à "Grounded State" et la variable isGrounded reste fausse. Donc mon perso reste dans l'état JUMP STATE avec l'animation qui va avec.
Cependant quand je "Debug.Log" la gravité, elle n'est pas nulle et s'applique bien sur le personnage. J'ai également vérifié le script de mon passage de "Jump State" à "Grounded State" et je ne vois aucune erreur. Ainsi, une force descendante s'applique toujours mais la variable isGrounded ne passent pas à true. C'est bien la variable "isGrounded" qui ne passe pas en true et reste en false qui pose mon souci.
J'ai posé la varible Min Move Distance = 0 mais cela n'a rien changé
Je mets les scripts ci-dessous. Quelqu'un peut-il m'aider à comprendre pourquoi "isGrounded" ne renvoie pas true s'il vous plaît ?
GROUNDED STATE :
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerGroundedState : PlayerBaseState
{
public PlayerGroundedState(PlayerStateMachine currentContext, PlayerStateFactory playerStateFactory)
: base(currentContext, playerStateFactory)
{
InitializeSubState();
IsRootState = true;
}
public override void EnterState()
{
Debug.Log("HELLO WORLD FROM GROUNDED STATE");
Ctx.CurrentMovementY = Ctx.GroundedGravity;
//Debug.Log("Valeur de CurrentMovementY : " + Ctx.CurrentMovementY);
Ctx.AppliedMovementY = Ctx.GroundedGravity;
//Debug.Log("Valeur de AppliedMovementY : " + Ctx.AppliedMovementY);
}
public override void UpdateState()
{
CheckSwitchState();
Debug.Log("Ctx.CharacterController.isGrounded DEPUIS LE GROUNDED STATE : " + Ctx.CharacterController.isGrounded);
}
public override void ExistState()
{
Debug.Log("BYE BYE GROUNDED STATE");
}
public override void InitializeSubState()
{
//Debug.Log("Ctx.IsMovementPressed : " + Ctx.IsMovementPressed + "Ctx.IsRunPressed : " + Ctx.IsRunPressed);
if(!Ctx.IsMovementPressed && !Ctx.IsRunPressed)
{
SetSubState(Factory.Idle());
}
else if (Ctx.IsMovementPressed && !Ctx.IsRunPressed)
{
SetSubState(Factory.Walk());
}
else if (Ctx.IsMovementPressed && Ctx.IsRunPressed)
{
SetSubState(Factory.Run());
}
}
public override void CheckSwitchState()
{
if(Ctx.IsJumpPressed && !Ctx.RequireNewJumpPress)
{
SwitchState(Factory.Jump());
}
else if(!Ctx.CharacterController.isGrounded)
{
SwitchState(Factory.Fall());
}
}
}
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerJumpState : PlayerBaseState
{
public PlayerJumpState(PlayerStateMachine currentContext, PlayerStateFactory playerStateFactory)
: base(currentContext, playerStateFactory)
{
InitializeSubState();
IsRootState = true;
}
public override void EnterState()
{
Debug.Log("HELLO WORLD FROM JUMP STATE !");
HandleJump();
}
public override void UpdateState()
{
Debug.Log("Ctx.CharacterController.isGrounded DEPUIS LE JUMP STATE : " + Ctx.CharacterController.isGrounded);
CheckSwitchState();
HandleGravity();
}
public override void ExistState()
{
Debug.Log("BYE BYE JUMP STATE!");
Ctx.Animator.SetBool(Ctx.IsJumpingHash, false);
//Ctx.IsJumpAnimating = false;
if (Ctx.IsJumpPressed)
{
Ctx.RequireNewJumpPress = true;
}
}
public override void InitializeSubState()
{
if (!Ctx.IsMovementPressed && !Ctx.IsRunPressed)
{
SetSubState(Factory.Idle());
}
else if (Ctx.IsMovementPressed && !Ctx.IsRunPressed)
{
SetSubState(Factory.Walk());
}
else if (Ctx.IsMovementPressed && Ctx.IsRunPressed)
{
SetSubState(Factory.Run());
}
}
public override void CheckSwitchState()
{
if( Ctx.CharacterController.isGrounded)
{
SwitchState(Factory.Grounded());
}
}
void HandleJump()
{
//Set _animator here
Ctx.Animator.SetBool(Ctx.IsJumpingHash, true);
//Ctx.IsJumpAnimating = true;
Ctx.IsJumping = true;
/*float previousYVelocity = _currentMovement.y;
float newYVelocity = _currentMovement.y + _initialJumpVelocity;
float nextYVelocity = (previousYVelocity + newYVelocity) * 0.5f;
*/
Ctx.CurrentMovementY = Ctx.InitialJumpVelocity;
//Debug.Log("Valeur de CurrentMovementY in Handle Jump : " + Ctx.CurrentMovementY);
Ctx.AppliedMovementY = Ctx.InitialJumpVelocity;
//Debug.Log("Valeur de AppliedMovementY in Handle Jump : " + Ctx.AppliedMovementY);
}
void HandleGravity()
{
bool isFalling = Ctx.CurrentMovementY <= 0.0f || !Ctx.IsJumpPressed;
float fallMultiplier = 2.0f;
Debug.Log("is Falling From JUMP STATE : " + isFalling);
if (isFalling)// isFalling check if the player is currently falling after have reached the apex of the jump. This is not checking if player falling from a border
{
float previousYVelocity = Ctx.CurrentMovementY;
Ctx.CurrentMovementY = Ctx.CurrentMovementY + (Ctx.Gravity * fallMultiplier * Time.deltaTime);
Ctx.AppliedMovementY = Mathf.Max((previousYVelocity + Ctx.CurrentMovementY) * 0.5f, -20.0f);
//Debug.Log("Valeur de CurrentMovementY in Handle gravity when falling : " + Ctx.CurrentMovementY);
//Debug.Log("Valeur de AppliedMovementY in Handle gravity when falling: " + Ctx.AppliedMovementY);
}
else
{
float previousYVelocity = Ctx.CurrentMovementY;
Ctx.CurrentMovementY = Ctx.CurrentMovementY + (Ctx.Gravity * Time.deltaTime);
//Debug.Log("Valeur de CurrentMovementY in Handle gravity : " + Ctx.CurrentMovementY);
Ctx.AppliedMovementY = (previousYVelocity + Ctx.CurrentMovementY) * 0.5f;
//Debug.Log("Valeur de AppliedMovementY in Handle gravity : " + Ctx.AppliedMovementY);
}
}
}