Lag Problem in 2D Game

I’m a beginner so I started watching tutorials and I’ve been following my first one, but the result is different from the guy on youtube. My character lags from time to time, the animations were fine when i built them (the character stood in place), but now that it moves it’s weird. His player is responsive but mine takes a time to move and like twitches back and forth from time to time. I guess this has something to do with either rigidbody2d settings, textures or script. Here’s the script:

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

    public int maxSpeed = 3;
    public float speed = 50f;
    private Animator anim;
    private Rigidbody2D rb2d;

    void Start () {
        anim = gameObject.GetComponent<Animator>();
        rb2d = gameObject.GetComponent<Rigidbody2D>();
    }
	
	
	void Update () {
        anim.SetFloat("speed", Mathf.Abs(Input.GetAxis("Horizontal")));
        if (Input.GetAxis("Horizontal") < -0.1f)
        {
            transform.localScale = new Vector3(-1, 1, 1);
        }

        if (Input.GetAxis("Horizontal") > 0.1f)
        {
            transform.localScale = new Vector3(1, 1, 1);
        }
    }

    void FixedUpdate()
    {
        float h=Input.GetAxis("Horizontal");
        rb2d.AddForce(Vector2.right*speed*h);

        if (rb2d.velocity.x > maxSpeed)
            rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);

        if (rb2d.velocity.x < -maxSpeed)
            rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
            
    }
}

Maybe it’s the way i imported the images, they may take up too much memory: i set them all to Pixels per unit: 32 or 10, Filter: Point(no filter), Format True-Color
I guess it could also be from collider, since the problem happens when i move. I set box colliders.

Please help me.

Edit: Also, in Visual Studio there’s a warning:
Warning CS0649 Field ‘Player.anim’ is never assigned to, and will always have its default value null

I have a few suggestions:

  • Don’t scale the object you’re moving. Make your Animator gameobject a child and “rotate” it 180°. Leave the moving gameobject as is.
  • Add a PhysicsMaterial2D to the player collider. Set its values to 0 so there’s no friction.
  • Disable root motion on the animator

The warning from VS shouldn’t be there since the variable is assigned to in Start(). Try syncing VS by closing it and then in Unity under “Assets” select “Open C# Project”