Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by zurek · Sep 04, 2018 at 05:03 PM · objectdetectionmoving

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!");
     }
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zurek · Sep 04, 2018 at 07:34 PM 0
Share

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

avatar image zurek · Sep 05, 2018 at 08:42 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zurek · Sep 04, 2018 at 05:35 PM 0
Share

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 :)

avatar image myzzie zurek · Sep 04, 2018 at 05:47 PM 2
Share

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.

avatar image zurek myzzie · Sep 04, 2018 at 05:57 PM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

95 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges