- Home /
Set animation for movement help??
Hello, I am kinda new to coding movement itself and am still learning. I have done some movement for my player, but cannot really see how to set the animation for the running. I have an animation already set and a Parameter for "Speed" already set, for when it is greater than 0.01 then it will activate the animation, but on the script I am getting an error with the "movement" variable than I have. Basically just need to know how to set the animation for movement.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour { public float Speed = 3f; public bool isGrounded = false; public Animator animator;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Jump();
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
transform.position += movement * Time.deltaTime * Speed;
animator.SetFloat("Speed", Mathf.Abs(movement));
}
void Jump()
{
if (Input.GetButtonDown("Jump") && isGrounded == true)
{
gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
}
}
}
Answer by camdogcrawford · Apr 20, 2020 at 08:54 AM
I had the same problems so i used this code Vector3 movement;
public Animator animator;
public float speed = 20f;
public float mov;
void Update()
{
mov = Input.GetAxisRaw("Horizontal") * speed;
movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
transform.position += movement * Time.deltaTime * speed;
animator.SetFloat("Speed", Mathf.Abs(mov));
if (d == true)
this should fix it, what it does is take the inputs and put them into the animator, it moves the object and makes it into a float at the same time.,I had the same problem, so I instead used this code to figure it out
public Animator animator;
public float speed = 20f;
public float mov;
void Update()
{
mov = Input.GetAxisRaw("Horizontal") * speed;
movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
transform.position += movement * Time.deltaTime * speed;
animator.SetFloat("Speed", Mathf.Abs(mov));
so this basically just adds the float part and does the same thing but without moving your object, so the animation can run
Your answer
![](https://koobas.hobune.stream/wayback/20220612225629im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to Apply Animations to Script 1 Answer
Interrupt a MoveTowards when triggering 2 Answers
A way to "freeze" the position of the animation? 1 Answer
I need help with player animation. 1 Answer
Unity Android movement 1 Answer