- Home /
Mecanim and trigger code
Thanks to everyone that's helped me with learning how to use mecanim. My character can move in the direction I want (and the walk/Idle transitions are smooth). But I want to be able to use a run animation and a jump animation.
My questions are:
Is it possible to set "shift" as a parameter so that when it's pressed my character will start running? Will shift be a bool or trigger?
Is it possible to set "spacebar" as a parameter so that when it's press the character will jump? Will it be a bool or trigger?
As you can tell I'm not that good at C# coding (as you already know coding is the largest obstacle for new unity users). And I was wondering can I code certain triggers into mecanim as a parameter so that I don't have to code it in C# (or javascript).
Thank you for your time.
P.S. Below is my C#code I learned from a tutorial to make my character move. How would I modify it (or would I have to) if I set the parameters within mecanim?
using UnityEngine;
using System.Collections;
public class IdleWalk : MonoBehaviour
{
Animator animator;
public float DirectionDampTime = 0.25f;
// Use this for initialization
void Start ()
{
animator = GetComponent<Animator> ();
}
// Update is called once per frame
void Update ()
{
float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical");
animator.SetFloat ("Speed", h*h + v*v);
animator.SetFloat ("Direction", h, DirectionDampTime, Time.deltaTime);
}
}
Answer by JCX · Jan 28, 2014 at 10:23 PM
i would use bool right not, because trigger is buggy and will be repaired in 4.5 (it's actually fixed in first 4.5 beta)
and code look like:
if(Input.GetKeyDown("space"))
{
animator.SetBool("space", true);
}
else if(Input.GetKeyUp("space"))
{
animator.SetBool("space", false);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613131929im_/https://answers.unity.com/themes/thub/images/avi.jpg)