Question by
dittizardichu · Jun 01, 2020 at 09:40 PM ·
c#spriteanimatorunity 2d
How to change animator states when a key is held through a C# script for a sprite?
I am working on a small project with a friend. At the moment, I am trying to swap between animations when a key is held down for a sprite. I cannot seem to do this however.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
Animator anim;
void Start()
{
anim = gameObject.GetComponent<Animator>();
}
void Update()
{
if (this.anim.GetCurrentAnimatorStateInfo(0).IsName("Jotaro Idle"))
{
if (Input.GetKey(KeyCode.D))
anim.SetTrigger("WalkRightfromIdle");
else if (Input.GetKey(KeyCode.A))
anim.SetTrigger("WalkLeftFronIdle");
}
}
}
Here is a screenshot of my animator window (each transition is controlled by a different trigger): https://imgur.com/a/f93GrAn
Any help would be appreciated.
Comment
Your answer
Follow this Question
Related Questions
How to change animator states from a key press? 1 Answer
Delay while using GetButtonDown and Animator 0 Answers
creating a 2d peeling system in unity 0 Answers
Calculate BoxCollider2D based on the actual player sprite 2 Answers
How do i make character eyes blink after some seconds have passed? (on 2D sprite) 1 Answer