• 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 luniac · Jun 13, 2012 at 09:21 PM · collisionaddforceballfloor

Ball colliding with floor as it's rolling

I have a ball that has forces applied to it when i press the corresponding arrow keys. I didn't realize before that even if the ball is in the air, the arrow keys will still apply forces to the ball and it will be able to move in the air.

I'm thinking of fixing it by putting the movement code with the forces inside the OnCollisionStay check. That way the ball will only have forces applied to it when it's touching the floor(I assume I can put some kind of ignore tags on walls, etc later).

Is this a proper and efficient way of solving this problem, or is there a simpler more elegant solution, perhaps a unity function i'm not aware of?

Comment
Add comment · Show 6
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 luniac · Jun 13, 2012 at 09:38 PM 0
Share

To be more clear, what i mean is: OnCollisionStay ill set a toggle variable such as moveallow = 1; Then in the FixedUpdate() function ill have an if statement check the moveallow variable, and the movement addforce code will be inside.

And im just wondering if thats an efficient way of doing it, or is there some function im not aware of that's simpler and more elegant.

avatar image luniac · Jun 13, 2012 at 09:39 PM 0
Share

And OnCollisionExit i can set moveallow = 0 right???

avatar image luniac · Jun 13, 2012 at 09:41 PM 0
Share

OH OH maybe i can use OnCollisionEnter()to make moveallow = 1 and just use a floor tag to make sure it's touching the floor! Then OnCollisionExit() i can see if it's exiting the floor and then set moveallow = 0! This means the Collision check only happens 2 times and the entire time the object is on the floor, no collision calculations have to be done!!!

This sounds pretty elegant to me. Any opinions?

avatar image whydoidoit · Jun 13, 2012 at 10:05 PM 0
Share

Looks like you've thought that one through yourself :)

Sounds like it would work to me....

avatar image luniac · Jun 14, 2012 at 01:49 AM 0
Share

but how would YOU do it lol?

avatar image luniac · Jun 19, 2012 at 07:17 PM 0
Share

O$$anonymous$$ i should report my final result. Using a boolean doesn't work. If you have a floor and a ramp and both have the tag floor, then what happens is when you touch floor at the beginning, onFloor = true. but if touch the ramp and then untouch it WHILE STILL ON THE FLOOR, the onFloor variable gets set to true again and then is set to false EVEN THOUGH YOURE ON THE FLOOR.

I fixed this by changing the boolean to an int, and essentially made it into a type of gamey semaphore. So on contact i do onFloor++ and when i leave i do onFloor--, and that work perfectly!!!

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Help with Android accelerometer 1 Answer

How can I apply a force to an object in the opposite direction of the object it's colliding with? 2 Answers

iPhone touch raycasting 1 Answer

i want to make an animation throw code 0 Answers

Ball jump on collision problem 1 Answer


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