- Home /
Checking when the player is stopped, without velocity?
My game is an endless runner, so velocity is a constant. But I need to find out when the player has stopped due to colliding with a platform. I cannot check for when velocity is very small, because velocity is constant, even when the player is halted by a platform. But I also cannot check for collision with a platform, because then what I want to happen will be triggered every time the player collides with a platform.
It has to be something with the player's position, right? Can someone point me in the right direction please? I've been at this for hours.
Does your object automatically reach that constant velocity at start? If so then you can check to see if the velocity has changed due to the collision because if an object collides with an object it does lose velocity.
I put the velocity in update (the speed is changing as time progresses), so the velocity is constantly set to speed.
Well then all you need is to check the velocity and have a variable that holds what your current velocity is. After that check to see if the velocity goes below it. That would mean that you collided with something because when you collide with something it accelerates negatively
i think he's constantly adding velocity which I think is where he's saying he's running into the issue, basically he's got the car against the wall and is flooring the gas and it's making it hard to use a force measurement, because he is pushing against the wall. he's faililng to move, but he's trying.
You can try to create constantly repeating function, so you won't execute this piece of code at each frame, but only for example each 0.1f. And each time when you invoke function it'll take current position of your player and compare it with previous position of your player when this function was called. If it's the same or lower than some number, then, probably, player stopped. Here you even can check only for the same position on X or Y or Z axes.
Answer by ransomink · Jan 20, 2018 at 09:29 AM
No problem, you'll want to check the player's previous position against their current position. If the distance is the same or minimal (using a tolerance), they're not moving. When the player moves, set their position to lastPos. Then, check the difference of the x value between the lastPos and curPos variables...
Here is a sample of how it could be done:
public bool moving;
public float tolerance;
public Vector2 prevPos;
public Vector2 curPos;
private Transform t;
void Awake()
{
t = transform;
}
void Update()
{
// Move the game object however you set it up
// This line is where you set the velocity to move your game object.
// Moving? (along the horizontal axis)
if ( t.position.x - tolerance > prevPos.x )
{
// Set last position
prevPos = t.position;
}
else
{
// We're not moving
moving = false;
}
}
Alternatively, you can get the distance between both positions but since your game is an endless runner, you only need to check against one axis...
Answer by sparkzbarca · Jan 20, 2018 at 05:44 AM
Endless runners normally use you leaving the screenspace to determine death if that's what your trying to do.
To do that a simple bounds check will work.
if your just wanting to know if someone is stuck on a wall as it were for whatever reason there are a few ways.
firstly oncollsionstay()
not on collision enter or on collision exit but on collision stay.
you can simply run a timer -= time.fixedtime or whatever and every physics update they stay in contact the timer will count down, if timer
as for using position, you could use position to determine if your starting to go backwards basically and you could even do something where you store the current frames position in global space as lastpos, then next frame you compare lastXpos - screen movement per frame and now if someone isn't moving, there position will be just that. if your "standing still" on a moving screen, your position will be in one frame, the position you were in the last frame but negative at least on the X axis the value at which the screen is moving right.