- Home /
2D animation problems!!!
Hello! I am pretty new to coding, so this might look like an easy fix to you, but i can't figure out myself! The problem i have is: All of my animations work perfectly fine, but when i jump and land on the ground, he keeps doing the jump animation. When he lands and i press a button afterwards, he goes back to his Idle animation. What i want is that when he lands on the ground i want him to go back to his Idle animation immediatly. I appriciate all the help i can get! (But please try to keep it as easy to understand as possible. Again, i am pretty new!)
My c# script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character : MonoBehaviour
{
Rigidbody2D rb;
private Animator anim;
public float speed;
private float moveInput;
private bool isGrounded;
public Transform feetpos;
public float checkradius;
public LayerMask WhatisGround;
public float Jumpforce;
AudioSource audioSource;
public AudioClip ActualJump2D;
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (moveInput > 0)
{
transform.eulerAngles = new Vector3(0, 0, 0);
}
else if (moveInput < 0)
{
transform.eulerAngles = new Vector3(0, -180, 0);
}
if (moveInput == 0)
{
anim.SetBool("isRunning", false);
}
else
{
anim.SetBool("isRunning", true);
}
isGrounded = Physics2D.OverlapCircle(feetpos.position, checkradius, WhatisGround);
if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
anim.SetBool("isJumping", true);
rb.velocity = Vector2.up * Jumpforce;
audioSource.PlayOneShot(ActualJump2D, 0.2F);
}
if (rb.velocity.y == 0)
{
anim.SetBool("isJumping", false);
}
}
void FixedUpdate()
{
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name.Equals("MovingSlab11"))
this.transform.parent = col.transform;
}
void OnCollisionExit2D(Collision2D col)
{
if (col.gameObject.name.Equals("MovingSlab11"))
this.transform.parent = null;
}
}
Comment