- Home /
Please help me with my 2D animations - c#
I am learning to make games for a school project but i have encountered an error. the error is Parameter 'heroStop' does not exist. UnityEngine.Animator:SetTrigger(String) PlayerMovement:Update() (at Assets/PlayerMovement.cs:45)
I have tried to make a script where, when i press 'd' (which is in an input called 'playerrun'), my sprite will change its animation. I have created parameters called 'heroMove', and 'heroStop' and i have assigned them to the appropriate animations. Below is my code and some pictures. Please help me, i will deeply appreciate it. code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public CharacterController2D controller;
public float runSpeed = 35f;
float horizontalMove = 0f;
bool jump = false;
bool crouch = false;
public Animator anim;
public KeyCode playerrun;
void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
if (Input.GetButtonDown("Jump"))
{
jump = true;
}
if (Input.GetButtonDown("Crouch"))
{
crouch = true;
}
else if (Input.GetButtonUp("Crouch"))
{
crouch = false;
}
if (Input.GetButtonDown("playerrun"))
{
GetComponent<Animator>().SetTrigger("heroMove");
}
else if (Input.GetButtonUp("playerrun"))
{
GetComponent<Animator>().SetTrigger("heroStop");
}
}
void FixedUpdate ()
{
// Move our character
controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
jump = false;
}
}
Pictures:
I have already tried to restart unity as that was a common fix, but i had no luck with that... Thanks!
Answer by kalen_08 · Jul 22, 2018 at 02:29 AM
Okay so I completely tidied up your script. first of all
// this is not good. instead of two opposite triggers, use a bool
// especially since its either one or the other.
if (Input.GetButtonDown("playerrun"))
{
GetComponent<Animator>().SetTrigger("heroMove");
}
else if (Input.GetButtonUp("playerrun"))
{
GetComponent<Animator>().SetTrigger("heroStop");
}
so I fixed it up and look how little you need to write:
public class PlayerMovement : MonoBehaviour
{
public KeyCode playerRunKey;
public float runSpeed = 35f;
CharacterController2D controller;
Animator anim;
void Start()
{
anim = GetComponent<Animator>();
controller = GetComponent<CharacterController2D> ();
}
void Update () {
var horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
var jump = Input.GetButtonDown("Jump");
var crouch = Input.GetButton("Crouch");
anim.SetBool("playerRun", Input.GetButton(playerRunKey));
controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
}
}
Answer by ModLunar · Jul 22, 2018 at 03:01 AM
Hmm it seems like your two parameters in the Animator aren't triggers, but are instead booleans. (So calling anim.SetTrigger(...) won't work with either of them).
If you want them to be triggers, make sure you change them in the Unity editor there in the Animator window. You'll wanna delete them and create new "Trigger" parameters from the (+) button drop down menu.
However, if you do like it being able to maintain both an on and an off state for true and false, then you can keep them as booleans. But your code will need to call anim.SetBool("playerRun", true)
to set it on, and anim.SetBool("playerRun", false)
to set it false.
Your answer
Follow this Question
Related Questions
Space opening main menu (undesired) 0 Answers
How to let death animation play when enemy dies, and stop player from taking damage 1 Answer
Collision on specific Frames 1 Answer
How can I make this GameObjects array remain permanent when i activate? 1 Answer
How can I code an enemy attack? (Top Down Melee Attack) 1 Answer