- Home /
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!");
}
}
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.
Answer by eses · Sep 04, 2018 at 05:24 PM
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 :) 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 :)
my_rigid_body.velocity magnitude > 0 // Is moving, ideally you'd want a threshold, like 0.1f.
Ins$$anonymous$$d of checking positions, check if the rigidbody has velocity.
I am checking magnitude now if (my_rigid_body.velocity.magnitude <= 0.1f)
and when object is colliding with block and cant go any further (he is not moving) its not damaging now. $$anonymous$$aybe he has always speed and he is moving, but can't go any further because of that collision. (Animation of running is still active).
Your answer
![](https://koobas.hobune.stream/wayback/20220612174041im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
making an object move up and down 3 Answers
How Can I Make a Moving Object with a trigger reaction ? [C#] 1 Answer
Raycast/Non-Physics Collider Discrepancy 0 Answers
Object detection in front of Character 1 Answer
unity 4.6 UI disabling click/touch detection on objects behind user interface 0 Answers