Can you improve my animation/movement code? *Bugs in details

So I’m sort of new to 2D game creation. I’ve successfully animated my character and added controls, etc. However the code is awfully simple and I have a couple of bugs to iron out, could you improve my code on my PlayerManager script?:

using UnityEngine;
using System.Collections;

public class PlayerManager : MonoBehaviour {

	Animator anim;
	private Rigidbody2D myRigidbody;
	[SerializeField]
	private float movementSpeed;

	// Use this for initialization
	void Start () 
	{
		anim = GetComponent<Animator> ();
		myRigidbody = GetComponent<Rigidbody2D> ();
	}
	
	// Update is called once per frame
	void FixedUpdate () 
	{

		//Animations
		if (Input.GetKey ("d")) 
		{
			anim.SetInteger ("State", 1);
		}
		if (Input.GetKeyUp ("d")) 
		{
			anim.SetInteger ("State", 0);
		}


		if (Input.GetKey ("a")) 
		{
			anim.SetInteger ("State", 2);
		}
		if (Input.GetKeyUp ("a")) 
		{
			anim.SetInteger ("State", 3);
		}

		float horizontal = Input.GetAxis ("Horizontal");
		HandleMovement (horizontal);
	}

	private void HandleMovement(float horizontal)
	{
		myRigidbody.velocity = new Vector2 (horizontal * movementSpeed, myRigidbody.velocity.y);
	}
}

Here are pictures of my animation setup as well:

The bugs can be seen in this (rushed) GIF:

  • Weird Sliding delay when changing
    directions.
  • If I nudge the movement keys enough the walk animation repeats even though there is no movement and the key is not being pressed. (At the end)

So could anybody help me Iron out these bugs and fix my (hackish) code by offering better solutions? Thanks!

Hope this this helps with ideas, this is where i got my basics from if it helps: Unity RPG Tutorial #1 - Setting Up The Basics - YouTube

@SkeetokTheOtter

public class PlayerMove : MonoBehaviour

{
//sounds
public AudioSource Hit;

public string LevelToLoad;

public float moveSpeed;
private Animator anim;
private Rigidbody2D myRigidbody;

private bool playerMoving;
private Vector2 lastMove;

private static bool playerExists;

public float waitToReload;
private  bool reloading;
private bool thePlayer;

public Transform target;
public float speedSpawn;
// Use this for initialization
void Start () 
{
    Hit = GetComponent<AudioSource> ();
	thePlayer = gameObject.name == "DemoPlayer";
	anim = GetComponent<Animator>();
	myRigidbody = GetComponent<Rigidbody2D>();

	if(!playerExists)
	{
		playerExists = true;
		DontDestroyOnLoad(transform.gameObject);
	}else{
		
		Destroy (gameObject);
		}
		
	}
	

// Update is called once per frame
void FixedUpdate () 
{
	playerMoving = false;

	if(Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f )
	{
		//transform.Translate (new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
		myRigidbody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed, myRigidbody.velocity.y);
		playerMoving = true;
		lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
	}


	if (Input.GetKeyDown(KeyCode.Escape))
	{

	}

	{

		if(Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f )
		{
			//transform.Translate (new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
			myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, Input.GetAxisRaw("Vertical")* moveSpeed);
			playerMoving = true;
			lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
		}

		if(Input.GetAxisRaw("Horizontal") < 0.05f && Input.GetAxisRaw("Horizontal") > -0.5f)
		{
			myRigidbody.velocity = new Vector2(0f, myRigidbody.velocity.y);
		}
		if(Input.GetAxisRaw("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f )
		{
			myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, 0f);
		}

		anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
		anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
		anim.SetBool("PlayerMoving", playerMoving);
	}

}

IEnumerator OnCollisionEnter2D(Collision2D other)

{
if(other.gameObject.tag == “Enemy”)
while(true)
{

		playerExists = false;
		yield return new WaitForSeconds(1);
		playerExists = true;
		myRigidbody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed, myRigidbody.velocity.y);
		playerMoving = true;
		Debug.Log("PlayerKilled...Respawning");
		}
	
	}

}

97260-picture2.png