I need help with 2d.

I need help with my project. My sprite starts moving before the walk animation begin. Can anyone help me please
this is the script im using i know its not great, but im still new to this.
Any help would be great
using UnityEngine;
using System.Collections;

public class KnightMovement : MonoBehaviour {

public float Speed = 2;

Animator anime;

// Use this for initialization
void Start()
{
anime = GetComponent();

}

void FixedUpdate()
{

GetComponent().velocity = new Vector2(Input.GetAxis(“Horizontal”) * Speed, 0);
anime.SetFloat(“Hspeed”, Mathf.Abs(GetComponent().velocity.x));

if(GetComponent().velocity.x < 0 && transform.localScale.x > 0)
{
transform.localScale = new Vector3(-1, 1, 1);
}
if (GetComponent().velocity.x > 0 && transform.localScale.x < 0)
{
transform.localScale = new Vector3(1, 1, 1);
}
}
}

Please use Code Tags when posting snippets of code.

A couple tips:

  • The issue is probably in your animation transition inside your animator. You probably have some transition time set or something. Watch the animator window while you’re playing to see where the delay is coming from.

  • You should only be doing physics updates within FixedUpdate. If you’re polling inputs, like “Input.GetAxis”, you should do it in the regular Update function.

  • Call “GetComponent()” once in the Start function, and save it in a Rigidbody2D type variable, then use that variable in your update function. Calling GetComponent every frame is unnecessary and wasteful.

1 Like

Thank you. It really helped alot! i actually figured it out.

1 Like