I have a vector2 for my 2d game and would like to know how to read the vectors, take them into the animator window and use the vector as a parameter for starting an animaton.
I would like to make my game that when SpeedX >= 0.01 then my animator plays PlayerWalkRight, but I am using Input.GetAxisRaw(“Horizontal”/“Vertical”) (I know it’s outdated and there are better ways to do user input, GetAxisRaw its just a way I understand)
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float movSpeed;
public Vector2 velocity;
Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
velocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
velocity.Normalize();
rb.linearVelocity = velocity * movSpeed;
}
}
Basically, how do i get the values from “velocity” and take them to animator to activate an animation?
Thanks in advance!
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Animator playerAnim; // Get reference to Animator component
public float movSpeed;
public Vector2 velocity;
Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
velocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
velocity.Normalize();
rb.linearVelocity = velocity * movSpeed;
// Use Animators SetFloat function to set animation controller values
playerAnim.SetFloat("SpeedX", velocity.x);
playerAnim.SetFloat("SpeedY", velocity.y);
}
}
You’ll want to get a reference to the Animator component that uses your animation controller, then use the SetFloat function to change the float variables you set up (documentation here: Unity - Scripting API: Animator.SetFloat)
As for getting the values from a Vector, 1st value is the X value, 2nd is the Y value (for a Vector3, the third would be Z) You can see the values accessed at the bottom of the code
Hope this helps
It helps greatly!
Just a few follow-up questions:
Does the code at the bottom link SpeedX and velocity.x together? Almost as if telling unity that SpeedX should equal velocity.x?
Also, something that confuses me, the url for the link you sent and just the unity docs in general as I have seen when trying to do research on movement and whatnot, it says “https://docs.unity3d.com/”, unity 3d?
Is there a separate docs for untiy 2d?
Thanks for your help!
Happy to help!
SetFloat does not link the two variables (if it did you’d be fine just doing it in Start() )
its the same as doing
SpeedX = velocity.x;
The Animator hides the varibles from the script, so you have to use the function instead, but it does the same thing.
I checked the documentation on the animator component, there doesn’t seem to be a function that lets you directly link a variable to it, so this is the best method I know of.
I have no idea why its referred to as Unity 3d, but you can find documentation for 2d components on there (Like this rigidbody2d component: Unity - Manual: Introduction to Rigidbody 2D) So presumably everything 2d Unity can be found on there
Thankyou! It is very cool to learn these things imo
I think I understand now, if I did the SetFloat in the start it would link them for the very first frame and never after, so it has to go in the update so that - every frame - it checks what velocity.x is and tells SpeedX that is should match? It works in my code so I am happy! Lmk if you find a better way to do this and as will I!
Yes that is very confusing lmao