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!
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.