Detect if object is still moving failure.

Hello. I want to detect if my object is still moving. If not, start damage that object. Problem is, that the object is damaged also if he is moving. Simply everytime is taking damage.

My code looks like this:

last_position = my_rigid_body.transform.position;
my_rigid_body.velocity = new Vector2(move_speed, my_rigid_body.velocity.y);

if (last_position == my_rigid_body.transform.position)
{
    health_now -= Time.deltaTime;
    health_bar.fillAmount = health_now / health_max;
    if (health_now < 0)
    {
        // TODO: IMPLEMENT GAME OVER
        Debug.Log("GAME OVER!");
    }
}

Hi @zurek !

Where are you using this code? Maybe in Update?

I think your issue is just here:

last_position = my_rigid_body.transform.position;

and then you do:

 (last_position == my_rigid_body.transform.position)

…which will be true each time your code runs. Changing velocity between these lines doesn’t immediately change object position.

Maybe just do a check for rb velocity magnitude; if that is below certain value - start causing damage to your object.

Edit - added example:

if (my_rigid_body.velocity.magnitude < 0.1f)
{
	// Hurt
}

Hello :slight_smile: yes it is in update loop sorry for that. So you think that i should just assign to my last_position magnitude of that object and after that checks if last magnitude == magnitude now? And can be remain that code? And can you please tell me in what that magnitude will do a trick? Thanks :slight_smile:

Hello :slight_smile: yes it is in update loop sorry for that. So you think that i should just assign to my last_position magnitude of that object and after that checks if last magnitude == magnitude now? And can be remain that code? And can you please tell me in what that magnitude will do a trick? Thanks :slight_smile:

I just want to explain where my problem is. I dont know how to check if my player (which is running by velocity) in 2D platform game collide with box. Simple he is running on platform and if there is box he stop there (because of 2D Box Collider) and he is not moving only he has velocity and animation of running. So I want to know how to deal with this problem. If I must move player in different way or if i must check collision with that box (problem is that from that boxes is platform on which he is running) or if there is some checking if he moves (because in the game he is not moving but in my code he has always speed and velocity only he can’t go further because of that Box Collider with that box).

I will try to explain everything what can help you understand my problem. So i have platform game with some basic platform on which is running my player with velocity my_rigid_body.velocity = new Vector2(move_speed, my_rigid_body.velocity.y);. I do not control my player he is running automatically with constant speed move_speed.On the platform are random generating blocks, which player must jump over them. But if he does not, object player will collide with that block and he will cannot be able to run any further because in fron of that object is block which barred his path. So he has still that velocity and constant speed, but he is not moving any further because of that block. And in this moment he must take damage. But I dont now what method to use to detect that collision or detect that he is not moving or what. So If anybody has any idea please help me and explain why. Thank you so much.