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 cojoc07 · Feb 16, 2017 at 01:36 PM · collisionraycastdetectionplatform

Detect if ball is on a platform or if it fell

I have a game where a ball goes on a series of tiles. The tiles are all tagged "tile" and I'm using a raycast to detect if the ball is still on the platform.

However, that raycast starts at the center of ball, and if i'm going on the edge of the platform(gravity won't affect the ball) the raycast no longer collides with the tile.

Is there a better way of checking if the ball is still on the platform?alt text

untitled.png (10.4 kB)
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

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ThisGuyThatsHere · Feb 16, 2017 at 03:06 PM

If you don't use meshcolliders for platforms, just normal box, sphere or capsule colliders you should be fine with raycasting on the right layer, but anyway OnCollisionEnter() and OnCollisionExit() seems a lot better, just need slightly more thinking to be done.

Example (keep in mind that syntax might be a little off as I'm not testing this in editor)

 public LayerMask platformLayer;
 int occurances; //This will prevent fake change if it collides with 2 platforms and leaves one of them
 bool onPlatform;
 
 void OnCollisionEnter(Collision other) {
     if (other.gameObject.Layer == platformLayer) {
         occurances++;
         onPlatform = true;
     }
 } 
 void OnCollisionExit(Collision other) {
     if (other.gameObject.Layer == platformLayer) {
         occurances--;
         if (occurances == 0) onPlatform = false;
     }
 } 

Comment
Add comment · Show 2 · 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 cojoc07 · Feb 17, 2017 at 01:17 PM 0
Share

Works like a charm! Thanks !

However, I feel like it's little heavy on the CPU...

avatar image ThisGuyThatsHere cojoc07 · Feb 17, 2017 at 02:24 PM 0
Share

Not really, until you do this on platform's side (a lot of rigidbodies). Rigidbody does it as it works, e.g. it's just a little line added to 2 actions that rigidbody notices either it required or not and there is no "OnCollisionStay()" which makes it a little faster, but overall it's fast anyway.

avatar image
0

Answer by Raresh · Feb 16, 2017 at 01:37 PM

Why not use OnCollisionStay()?

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
avatar image
0

Answer by Kossuranta · Feb 16, 2017 at 02:20 PM

I would probably add collider on top of that tile as trigger. OnTriggerEnter happens when ball enters the area of the tile and OnTriggerExit happens when the ball isn't on the tile anymore.

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
avatar image
0

Answer by gameplay4all · Feb 16, 2017 at 02:46 PM

If you insist on using a raycast sort of approach, you can use Physics.SphereCast() which also gives you information on the angle of the hit which you could use for jumping directions etc. Eteeski (or Master Indie) has a great tutorial on this. But performace wise OnCollisionStay(), as Raresh suggested, would be the better option I believe.

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

7 People are following this question.

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

Related Questions

Multiple/Different footsteps sounds (solved) 2 Answers

Enemy got lit by flashlight detection 1 Answer

How can I make it so that my player only moves if there is no wall in the direction he wishes to move? 1 Answer

Capturing a point 2 Answers

Raycast sometimes ignore objects. 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