- Home /
Running animation
I made a running animation for my First Person Character, made a transition between the animation and the idle, and set the trigger "Run" for the Idle>RunningAnimation transition. I also made a script that callls it if the player is pressing left shift. The problem is that it goes and then returns, and loop indefinitely until i stop pressing shift.
I wanted the following to happen: The player is pressing shift, then the animation will play (the weapon will get up) and then stay like that until i stop pressing the shift button. If the player has stopped, then return to the Idle animation, BUT WITHOUT looping. How could i do that?
Here's my script:
using UnityEngine;
using System.Collections;
public class RunShotgun : MonoBehaviour
{
public Animator anim;
void Update()
{
if (Input.GetKey("left shift"))
{
anim.SetTrigger("Run");
}
}
}
I don't know what is wrong.
Answer by salamander555 · Nov 09, 2016 at 06:55 PM
i am not that experienced but i'll try helping
firts of all i think the main problem is that as long as you hold shift the animation will keep playing so if the animation only need to play once use the GetKeyDown
if (Input.GetKeyDown("left shift"))
{
anim.SetTrigger("run");
}
and next what I would do still i'm not the best is just change your player sprite when you pressed shift until you release shift it would look like this
using UnityEngine;
using System.Collections;
public class RunShotgun : MonoBehaviour
{
public Animator anim;
public Sprite weaponUp;
public Sprite normalSprite;
void Start()
{
normalSprite = GetComponent<SpriteRenderer>().Sprite
}
void Update()
{
if (Input.GetKeyDown("left shift"))
{
GetComponent<SpriteRenderer>().Sprite = weaponUp;
anim.SetTrigger("Run");
}
if (Input.GetKeyUp("left shift"))
{
GetComponent<SpriteRenderer>().Sprite = normalSprite;
}
}
}
make sure that you don't forgot the set you variable weaponUp in the unity menu (by setting you weaponUp sprite here if you wouldn't know)