I’ve been trying all day to get the animation for my 2D character working but, no matter what I do, it’s stuck on its idle animation. I made an animation script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAnimation : MonoBehaviour
{
private Animator anim;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow))
{
anim.SetBool("isRunning", true);
}
else
{
anim.SetBool("isRunning", false);
}
}
}
Set the bool parameter “isRunning” in the animator, made the transition with the “isRunning” parameter and dragged the animator script on the Player in the inspector. Can anyone help me?
Olmi
October 11, 2020, 8:09pm
2
Hi,
Can you show what the transitions you have made in the Animator?
I managed to fix it, but thanks for replying!
I have a new issue now, though.
I’m trying to:
Jump as long as the Space key is pressed and playing the corresponding jump up animation;
Start falling when the Space key is released and play the fall animation;
Transition from the fall animation to run animation if the player is grounded AND there’s an active A or D input;
Transition from fall to idle if the player is grounded without any input.
That’s a lot to ask, but I would really appreciate it if you could help me!
Here’s the player movement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed;
public float jumpForce;
private float moveInput;
private Rigidbody2D rb2d;
private bool facingRight = true;
public bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
private int extraJumps;
public int extraJumpsValue;
private void Start()
{
extraJumps = extraJumpsValue;
rb2d = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
moveInput = Input.GetAxis("Horizontal");
rb2d.velocity = new Vector2(moveInput * speed, rb2d.velocity.y);
if(facingRight == false && moveInput > 0)
{
Flip();
}
else if(facingRight == true && moveInput < 0)
{
Flip();
}
}
private void Update()
{
if(isGrounded == true)
{
extraJumps = extraJumpsValue;
}
if(Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
{
rb2d.velocity = Vector2.up * jumpForce;
extraJumps--;
}
else if(Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true)
{
rb2d.velocity = Vector2.up * jumpForce;
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}
Player animation script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAnimation : MonoBehaviour
{
private Animator anim;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
{
anim.SetBool("isRunning", true);
}
else
{
anim.SetBool("isRunning", false);
}
if(Input.GetKeyDown(KeyCode.Space))
{
anim.SetTrigger("jump");
}
else if(Input.GetKeyUp(KeyCode.Space))
{
anim.SetTrigger("fall");
}
if(GetComponent<PlayerController>().isGrounded == true && Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
{
anim.SetBool("isRunning", true);
}
else
{
anim.SetBool("isRunning", false);
}
}
}
Screenshots of the animator:
the controller work but the animation don’t work (for me)