using UnityEngine;
public class bo : MonoBehaviour
{
public float MovementSpeed = 1;
public float JumpForce = 1;
public Animator anim;
private Rigidbody2D _rigidbody;
private void Start()
{
_rigidbody = GetComponent();
}
void Update()
{
var movement = Input.GetAxis(“Horizontal”);
transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * MovementSpeed;
if (Input.GetButtonDown(“Jump”) && Mathf.Abs(_rigidbody.velocity.y) < 0.001f)
{
_rigidbody.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
if (Input.GetKeyDown(KeyCode.RightArrow)) anim.SetFloat(“Speed”, 10);
if (Input.GetKeyDown(KeyCode.LeftArrow)) anim.SetFloat(“Speed”, 10);
else anim.SetFloat(“Speed”, 0);
}
}
}
It isn’t working even though I said if I clicked a key it should switch to run animation?
@Omnipotential
in place of checking arrow keys just check movement value if it is zero use ideal animation and if it is not zero then use run animation. and as per your code these animations only play when jump button is pressed. I this does not work then let me know.
using UnityEngine;
public class bo : MonoBehaviour
{
public float MovementSpeed = 1;
public float JumpForce = 1;
public Animator anim;
private Rigidbody2D _rigidbody;
private void Start()
{
_rigidbody = GetComponent<Rigidbody2D>();
}
void Update()
{
var movement = Input.GetAxis("Horizontal");
transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * MovementSpeed;
if (Input.GetButtonDown("Jump") && Mathf.Abs(_rigidbody.velocity.y) < 0.001f)
{
_rigidbody.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
}
if (movement != 0)
anim.SetFloat("Speed", 10);
else
anim.SetFloat("Speed", 0);
}
}
moderatory sidenote:
Don’t envelop big code blocks in “`” characters as this is for in-line, very short snippets only.