- Home /
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?
I edited your question to provide the code. I'm looking into a solution involving the dot product now :P
And do notice that your using vectors for position, so they aren't "greater" than one another in the sense that your using them.
Well the solution is quite easy, actually. Check my updated answer.
Seems like I am getting there, the only problem is Unity doesn't like me attaching trasform to a variable
Answer by Un4given · May 29, 2011 at 10:01 PM
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
Answer by Meltdown · May 29, 2011 at 07:12 PM
If your player has a rigidbody attached you can check the velocity of your player.
if(rigidbody.velocity.magnitude > 0)
{
// Player is moving
}
This would work if he is using a rigidbody, but what if he isn't? ;)
Answer by $$anonymous$$ · Feb 19, 2019 at 01:08 AM
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
i just logged in , just to say thank you really much it worked like a charm i am really appreciative about that!
Answer by Catlard · May 29, 2011 at 07:14 PM
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...
This is what I had in $$anonymous$$d but I am unsure how to implement this.
Could you help me out?
I edited your comment and original answer so that the code was in the question.
Cool Story Bro btw :/
Answer by SirGive · May 29, 2011 at 08:04 PM
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?
Still getting this error:
ArgumentException: You are not allowed to call get_transform when declaring a variable. $$anonymous$$ove it to the line after without a variable declaration. Don't use this function in the constructor or field initializers, ins$$anonymous$$d move initialization code to the Awake or Start function.
so I have tried:
var previous_position; var current_position;
then in Start:
previous_position = transform.position.x; current_position = transform.position.x;
but still no joy :(
don't use a transform, use Vector3. And make sure you declare it at the top of your script, before all of the functions.
edited again using original code. and you wouldn't want to set previous_position to the same position as current. You want to wait until the position is new, then you would check it.
Your answer
Follow this Question
Related Questions
Move player or background 0 Answers
Player isn't affected by AddForce(), Lerp() etc. 1 Answer
Player making Zigzag movement 0 Answers
auto run key 1 Answer
How do I fix collisions while using this PlayerController script? 1 Answer