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 Edwin F · Aug 22, 2015 at 10:52 PM · 2dunity52d-platformervelocitycollider2d

Unity2D, how to stop game when colliding with an object or speed is 0?

I am making a Impossible game/geometry dash copy just to learn a few things and so, I am not intending on releasing it.

Anyway, I am trying to find a good way to move my cube in a constant speed to the right, but if you collide with an object and your velocity or movement is 0, the game should end as you are not allowed to stop nor hit the side of objects.

Currently my code of applying speed to the object is this:

         rb = GetComponent<Rigidbody2D>();
         var newVelocity = rb.velocity;
         newVelocity.x = maxSpeed * 7;
         rb.velocity = newVelocity;

I also have applied a rigidbody to my cube and I am really happy with the movement and the feeling of the controls, tho I am stuck at a point.

How would I do so when the cube hits the side of obstacle or another object and the velocity is 0 the game stops? alt text

I tried a bunch of things such as when the velocity is 0 kill the game, but my code for movement is always setting the velocity to 7, therefor that won't work.

I also tried adding a collision detection system on the right of the object like so: alt text That didn't work either as the jump animation rotates the cube 90 degrees and then the box collider will be at the bottom where the cube lands, and therefor kills the game at that point.

I tried alot of things I just dont know how to solve it, can anyone share me some ideas how to solve it, do I have to change my movement code a bit or what should I do?

Appreciate all the help I can get, thanks.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Spartan0751 · Aug 24, 2015 at 11:58 AM

One thing you could do is check what the velocity is before you set it. After a few tests, assuming you're using FixedUpdate, it seems the velocity does register as 0 if you check it before you set it, which makes sense.

  rb = GetComponent<Rigidbody2D>();
  var newVelocity = rb.velocity;
  if(newVelocity == 0)
        // Do things here
 else {
  newVelocity.x = maxSpeed * 7;
  rb.velocity = newVelocity;
 }

This way, you'll know if its velocity is nil, and if it isn't you can continue to make it move as normal.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2D Platformer Jumping at a 45 degree angle 2 Answers

2d collider stuck issue 1 Answer

Moving colliders that are part of a composite collider 1 Answer

Velocity in Unity2D 2 Answers

Constant velocity when colliding - 2D 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