(Sorry for my bad english)
I currently making a basic 2D player controller, and i have a weird issue. I using addForce to move, and jump, but when the character landing from a jump, and the collide happens the playertranform.velocity is resetting to 0.0, even if I held down left or right arrow, and because of this, a player stopping for a moment before start to run again.
Input code:
public bool IsJump()
{
if (Input.GetKey(KeyCode.UpArrow)) return true;
else return false;
}
public bool IsLeft()
{
if (Input.GetKey(KeyCode.LeftArrow)) return true;
else return false;
}
public bool IsRight()
{
if (Input.GetKey(KeyCode.RightArrow)) return true;
else return false;
}
Run state:
int direction = 0;
float speed = 60f;
float maxSpeed = 5f;
GameObject player;
Rigidbody2D playerRigid;
public override void StateDestroy()
{
}
public override void StateStart()
{
SetStateName("Run");
player = GameObject.FindGameObjectWithTag("Player");
playerRigid = player.gameObject.GetComponent<Rigidbody2D>();
Debug.Log("Run started");
}
public override void StateUpdate()
{
if (stateMachine.playerInput.IsRight())
{
direction = 1;
}
else if (stateMachine.playerInput.IsLeft())
{
direction = -1;
}
else
StopMoving();
if (stateMachine.playerInput.IsJump())
{
stateMachine.ChangeState(new Jump());
}
}
public override void StateFixedUpdate()
{
playerRigid.AddForce(new Vector2(direction, 0) * speed * Time.deltaTime, ForceMode2D.Impulse);
NormalizePlayer();
}
void StopMoving()
{
stateMachine.ChangeState(new Idle());
}
void NormalizePlayer()
{
if (playerRigid.velocity.x < -maxSpeed || playerRigid.velocity.x > maxSpeed)
{
Vector2 vec = playerRigid.velocity.normalized * maxSpeed;
playerRigid.velocity = vec;
}
}
Jump state:
GameObject player;
Rigidbody2D playerRigid;
float jumpForce = 300f;
float maxJump = 6f;
bool isJump = false;
public override void StateDestroy()
{
}
public override void StateFixedUpdate()
{
if (isJump)
{
playerRigid.AddForce(new Vector2(0, 1) * jumpForce * Time.deltaTime, ForceMode2D.Impulse);
NormalizeJump();
}
}
public override void StateStart()
{
SetStateName("Jump");
player = GameObject.FindGameObjectWithTag("Player");
playerRigid = player.gameObject.GetComponent<Rigidbody2D>();
isJump = true;
}
public override void StateUpdate()
{
if (!stateMachine.playerInput.IsJump())
{
isJump = false;
}
}
void NormalizeJump()
{
if (playerRigid.velocity.y > maxJump)
{
Vector2 vec = playerRigid.velocity.normalized * maxJump;
playerRigid.velocity = vec;
isJump = false;
}
}
Where the issue happens:
GameObject player;
Rigidbody2D playerRigid;
private void Awake()
{
playerInput = GetComponent<IPlayerInput>();
}
private void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
playerRigid = player.gameObject.GetComponent<Rigidbody2D>();
ChangeState(new Idle());
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.transform.tag != "Ground") return;
if (currentState.GetStateName() == "Jump")
{
Debug.Log(playerRigid.velocity); // <-- here is the velocity is Zero. dunno why;
if (playerInput.IsLeft() || playerInput.IsRight())
{
ChangeState(new Run());
}
else
ChangeState(new Idle());
}
}
Note: here the playerRigid is only for tracking the velocity at the collision.