Check if player is moving

I am sure there is way to do this but im struggling to find how.

I am making a 2d platform and basically I want to check if the player is actually moving left or right (x axis) so I can move my background accordingly.

I know I could use:

if(Input.GetAxis("Horizontal") > 0); 

But if my player hits a wall and the user continues to hold down the button, I dont want the background to continue moving.

Edit:

Here is some code. I haven’t tested it or anything, but I’ve done this exact thing before.

var previous_position = transform.position.z;
var current_position = transform.positon.z; 
//or whatever axis you're platforming along

function Update()
{
   if(previous_position > current_position)
   { 
      //do whatever you do when you move left
   }
   if(previous positoin < current_position)
   {
      //move right stuff
   }
   previous_position = current_position;
   current_position = transform.position.z;
}
// party time!

Make sense?

If your player has a rigidbody attached you can check the velocity of your player.

if(rigidbody.velocity.magnitude > 0)
{
  // Player is moving
}

Ok so I got it to work using:

var old_pos : float;
function Start(){
old_pos = transform.position.x;
}
function Update(){
if(old_pos < transform.position.x){
print("moving right");
}
if(old_pos > transform.position.x){
print("moving left");
}
old_pos = transform.position.x;
}

Seems to work find and dandy so thanks for all the help guys.

Especially SirGive

Yeah, you can also check to see if the player’s position changed from the previous frame. Just store the transform.position.x, or whatever axis you’re platforming along in a frame, and then during the next pass, before you reassign it, check to see if the player’s position has changed…

Ok i know im really really late but this might help someone so heres my solution for it its a bit more quicker than doing something that @Un4given has given.

if (transform.hasChanged)
        {
            animationPlayer.SetBool("isWalking", true);
            transform.hasChanged = false;
        }
        else
        {
            animationPlayer.SetBool("isWalking", false);
            stillTime++;
            animationPlayer.SetInteger("canIdle", stillTime);
            if (stillTime == 1030)
            {
                stillTime = 0;
            }
            print(stillTime);
        }

hope that helped

Put this in your update:

var old_pos : Vector3;


function Update()
{
   //movement code

   if(old_pos.x == transform.position.x)
   { 
      //move background
   }
   old_pos = transform.position;
}
// party time!

Its exactly as you said, you wanted to check the old position and see if it was the same, right?