For the last 72 hours I have been trying to detect the moment at which my character lands on the ground. For that whole time I haven’t succeded once. Here is my code (without all the solutions I tried, so that you can see everything that DOES work):
void Update() {
isGrounded = Physics2D.Linecast (transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
if (Input.GetButtonDown ("Jump") && isGrounded) {
jump = true;
}
}
void FixedUpdate() {
cur_y = transform.position.y;
delta_y = cur_y - old_y;
horizontalAxis = Input.GetAxis ("Horizontal");
verticalAxis = Input.GetAxis ("Vertical");
if (horizontalAxis != 0f) {
transform.position = new Vector2 (transform.position.x + (1f * speed * Time.deltaTime * horizontalAxis), transform.position.y);
anim.SetBool ("Walk", true);
} else {
anim.SetBool ("Walk", false);
}
if (horizontalAxis > 0f && !facingRight)
Flip ();
else if (horizontalAxis < 0f && facingRight)
Flip ();
if (jump) {
anim.SetTrigger("Jump");
rb.AddForce(new Vector2 (0f, jumpForce));
jump = false;
}
old_y = cur_y;
}
void Flip() {
facingRight = !facingRight;
Vector3 scale = transform.localScale;
scale.x *= -1;
transform.localScale = scale;
}
I tried setting the trigger “Landed” when my character is grounded, but the jumping animation also starts when the character is grounded, so launching that “Landed” trigger would simply turn off the jumping animation just after the jump has started. Nothing works, I get either inconsistent triggers (that randomly work or don’t work) or just those ones that disable the jumping animation after the jump, so I thought that I would ask you just before I die of starvation. Please help me.