0
\$\begingroup\$

So I want to make my animation transitions smooth, thats why I made transitions on the animator, but how can I activate them if I use trigger paramaters??? Heres the code:

public float speed = 0.3f;
public float sprintSpeed = 2.5f;
private Rigidbody rigidB;
private float midJump = 1f;
public float rotSpeed = 2;
Animator anim;

void Start()
{
    rigidB = GetComponent<Rigidbody>();
    anim = GetComponent<Animator>();
}
void Update()
{

    //Character rotation with A and D

    var z = Input.GetAxis("Horizontal") * rotSpeed;

    //Character movement with W and S

    var y = Input.GetAxis("Vertical") * speed;

    //Character animations

    if (Input.GetKey(KeyCode.RightShift))
        y = y * sprintSpeed;

    if (Input.GetKey(KeyCode.RightShift) && y > 0.31)
        anim.Play("Sprint");

    if (y < 0.001)
        anim.Play("Idle");

    if (y > 0.001 && y < 0.31)
        anim.Play("Run");


    transform.Translate(0, -y, 0);
    transform.Rotate(0, 0, -z);



    //Character Jump


    if (midJump == 1 && Input.GetKeyDown(KeyCode.Space))
    {
        rigidB.velocity = new Vector3(0, 45, 0);
        midJump = 2;

    }
    else if (rigidB.velocity.y == 0)
        midJump = 1;

}
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

You can set the parameters through the animator. And the transitions will play the same as you set it up.

for example if you wanted to set the horizontal speed in the animator parameters then have the transition play if the horizontal speed is > 0.1

var z = Input.GetAxis("Horizontal") * rotSpeed; 
anim.SetFloat("HorizontalSpeed", Mathf.Abs(z));

another example setting the vertical speed that is linked

anim.SetFloat("vSpeed", GetComponent<Rigidbody2D>().velocity.y);

you can also Set a boolean, int or Triggers the same way depending on what you need.

edit -added a pic to show the parameters getting set in the animator enter image description here

\$\endgroup\$
0
\$\begingroup\$

You are switching between your animations with anim.Play(animationName). If you do that, the animator switches to the new animation immediately without going through any transitions.

If you want the animator to use transitions, don't trigger the desired animation directly. Trigger the animations indirectly by:

  1. Define some parameters in your animation controller
  2. Defining conditions for your transitions in the animator window which trigger a transition when certain parameters have certain values.
  3. Cause those conditions to become true from your script by setting the values of those parameters with the methods anim.SetFloat, anim.SetInteger and anim.SetBool

One such parameter could be "speed". You could create transitions between "Idle", "Run" and "Sprint" which get triggered when "speed" is under or over a certain threshold. Then you just need to send the current speed from your script to the animator with anim.SetFloat("speed", y)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .