Weird Rigidbody2D.velocity.x problem, float out of control

Hello. I have this really weird problem when using a rigidbody2D to move my character. I am programming basic RPG 4-way movement using rigidbody2D.velocity. Everything works fine and dandy except for when moving in Horizontal direction. rigidbody2D.velocity.x totally freaks out when I get to a wall with a collision2D box on it. It does not happen with the y direction at all, only the x. When I print out the velocity.x i get a number as high as: 6.658959E-08, but it only does this if I mash the right or left key when my character stands next to a colliding wall.

I cannot figure out what causes this problem, is it my code? Or is it a physics setting in Unity? My code for moving my char is below, oh and this is my first crack at this, so it is not the most beautiful solution. I have scoured it several times, but have failed to see what I have done wrong, if I have done anything wrong. :stuck_out_tongue:

Thnx for the help :smiley:

using UnityEngine;
using System.Collections;

public class MainCharControllerScript : MonoBehaviour {
	
	public float Speed;
	//bool facingDown = true;
	//bool facingUp = false;
	bool facingRight = false;
	bool buttonCheck;
	Animator anim;
	float moveHorizontal;
	float moveVertical;

	Vector3 vel;

	
	
	// Use this for initialization
	void Start ()
		
	{
		anim = GetComponent<Animator> ();
			
	}

	// Update is called once per frame
	void FixedUpdate () 
		
	{
	


		//print (rigidbody2D.velocity);
		moveHorizontal = Input.GetAxis("Horizontal");
		moveVertical = Input.GetAxis ("Vertical");

		// check direction
		if(rigidbody2D.velocity.y == 0)
		{
			anim.SetBool ("directionUp", false);
			anim.SetBool ("directionDown", false);
		}

		if (rigidbody2D.velocity.x == 0) {
			anim.SetBool ("directionLeftRight", false);		
		}

		if (rigidbody2D.velocity.y > 0) {
			anim.SetBool ("directionUp", true);	
			anim.SetBool ("directionDown", false);
		}

		if (rigidbody2D.velocity.y < 0) {
			anim.SetBool ("directionDown", true);
			anim.SetBool ("directionUp", false);
		}

		if (rigidbody2D.velocity.x < 0 || rigidbody2D.velocity.x > 0) {
			anim.SetBool ("directionLeftRight", true);
		}


		//anim.SetInteger ("SpeedH", Mathf.Abs ((int)rigidbody2D.velocity.y));
		//anim.SetInteger ("SpeedV", Mathf.Abs ((int)rigidbody2D.velocity.x));
			

		print (rigidbody2D.velocity.x);
		print (rigidbody2D.velocity.y);


		//print (moveVertical);
		//rigidbody2D.velocity = new Vector2 (moveVertical * maxSpeed, rigidbody2D.velocity.x);
		
		//make sure player cannot press two directions at once
		if (Input.GetButton("Horizontal") && Input.GetButton("Vertical")) {
			buttonCheck = true;		
		}


		if (buttonCheck) {
			moveHorizontal = 0;
			moveVertical = 0;
		}
		


		if (!Input.GetButton ("Horizontal")) {
			rigidbody2D.velocity = new Vector2 (0,0);
			//anim.SetBool("WalkingLeftRight", false);
			//anim.SetBool("WalkingUpTrue", false);
			//anim.SetBool("WalkingDown", false);
				}

		if (!Input.GetButton ("Vertical"))
		{
			rigidbody2D.velocity = new Vector2 (0,0);
			//anim.SetBool("WalkingUpTrue", false);
			//anim.SetBool("WalkingDown", false);
		}

		if(Input.GetButton("Horizontal") && Input.GetButton("Vertical") == false)
		{
			buttonCheck = false;

			//anim.SetBool("WalkingLeftRight", true);

			if(moveHorizontal > 0)
			{
				rigidbody2D.velocity = (transform.right * Speed);
			}

			 if(moveHorizontal < 0)
			{
				rigidbody2D.velocity = (transform.right *- Speed);
			}
				
			
			
			if (moveHorizontal > 0 && !facingRight) 
				Flip ();
			else if (moveHorizontal < 0 && facingRight)
				Flip ();
			
		}
			

		//anim.SetFloat ("Speed", Mathf.Abs (moveVertical));
		 if (Input.GetButton ("Vertical") && Input.GetButton("Horizontal") == false) {
			buttonCheck = false;


			
			if(moveVertical > 0)
			{
				rigidbody2D.velocity = (transform.up * Speed);
				//anim.SetBool("WalkingUpTrue", true);
				//anim.SetBool("WalkingDown", false);
			
			}
				
					

			  if(moveVertical < 0)
				
			{
				rigidbody2D.velocity = (transform.up *- Speed);
				//anim.SetBool("WalkingDown", true);
				//anim.SetBool("WalkingUpTrue", false);
			}


			
		}

		
	}

	void OnCollisionEnter2D(Collision2D coll)
	{
		if (coll.gameObject.name == "Wall") {
			
			print ("Coll!");
			
		}
	}
	
	void Flip()
		
	{
		facingRight = !facingRight;
		Vector3 theScale = transform.localScale;
		theScale.x *= -1;
		transform.localScale = theScale;
		
	}

	
}

Your code is WAD, the E-08 6.658959E-08 is the exponent of 10 the number is multiplied by, so your number is 6.658959 * 10 to the power of -8, so it’s 6.658959 / 100000000, a number so pathetically low it can be practically counted as zero.

Read through my comments and truncate your code, it’ll make problem solving a lot faster.
I barely scratched the surface there, on reading through your code you can probably get rid of 80% of the lines. If you’re practicing coding, PLEASE exersize in truncation as your next task. It’ll make problem solving easier if there’s less and more logical coding.