• 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
10
Question by Dr. Watson · Oct 14, 2010 at 06:34 PM · collisionraycast

How expensive is ray casting???

I'm making a game for the iPhone and I was wondering exactly how expensive ray casting is??? The main character in the game has the ability to scale during runtime and before they do I check around the player to make sure they're not going to scale through anything! Well my issue is I've done a lot of research on how to check left/right/up/down and see if it's clear and then stop the character from scaling or scoot them over, the whole nine yards... But the only thing that seems to work correctly is ray casting. So, I'm going to cast a bunch of rays each direction to make sure the player isn't going to blaze through some small object because they've scaled themselves way up. But I don't want to over do it if ray casting is super expensive. Thanks ahead of time for your help and any alternatives you can suggest to me would be greatly appreciated.

Things I've tried:

  1. Trigger colliders on my objects and when the player hits that collider tell me what side it's on but when I get above a certain scale the trigger collider no longer detects the collision that's happening(even though I can physically see the character controller overlapping the trigger)

  2. Line cast a box around my character... I'm not quite sure why this didn't work but it didn't.

  3. Check sphere but you can't pass it a RaycastHit! So, I can't manually check to see where the object I'm colliding with is at in relation to my character.

  4. Capsule cast... I was hoping to do this but I couldn't find any examples and the way it works is weird or I just can't wrap my head around why I can't just give the capsule an origin and then tell it how tall and wide it is. If anybody could give me an example of this....I would probably fall in love with you XD jk.

  5. Sphere cast would've worked but to make the radius big enough to cover the width of my character it made the height to tall...

  6. Check capsule...once again you can't tell where the object is that hit you...only that something is in the area.

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

3 Replies

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

Answer by skovacs1 · Oct 14, 2010 at 07:24 PM

How do you define expensive? The cost of a raycast is the cost of the intersection algorithm for each object checked until a hit is registered if the objects are depth sorted, otherwise, it must check every object. Simple object's have much simpler intersection algorithms. Raycasts that are far enough away from an object being checked should not check against the object, but should quit out early. Raycasts against convex 3D geometry even with a fast optimized algorithm is the cost of several subtractions, additions and in many cases at most like one division per triangle. The number of cycles taken by each of these operations is generally hardware dependent. If your scene has your colliders spaced enough, you are using simple colliders and you are not raycasting too often, it should be quite cheap.

These are some approaches to consider (some of which you have tried):

  1. If you know how big you are getting, you can limit the distance of your rays accordingly.
  2. Have you considered Physics.OverlapSphere? It returns the colliders that were overlapping. You can then check through the results of the overlap test to see where the colliders are in relation to your object, either by checking their positions or raycasting against them explicitly.
  3. Or you could try Physics.SphereCast which is about as expensive as OverlapSphere + a raycast and takes a RaycastHit. or Physics.SphereCastAll which returns all of the RaycastHits and is about the same as Physics.RaycastAll with your sphere's radius. These also include stuff for sweeping your sphere in space, but you probably don't need this.
  4. Or you could try Physics.CapsuleCast or Physics.CapsuleCastAll. The docs provide all the example you need and description of the input params. Point1 and Point2 are the ends (top and bottom) of your capsule, radius is the radius of your capsule, direction and distance are used for sweeping the capsule in space (which you probably don't care about) and the layerMask is a LayerMask. These can also give you RaycastHits.
Comment
Add comment · Show 6 · 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 Dr. Watson · Oct 14, 2010 at 07:37 PM 0
Share

Thanks for your reply! I'm glad to know that raycasting isn't very performance expensive! Right after I posted this I went in and made ray casts in 16ths along the side of my guy and it solved my issue and doesn't seem to bog down my system....however I still have to test this on the iPhone. If that doesn't work...your solution should work perfectly. Thanks again.

avatar image skovacs1 · Oct 14, 2010 at 07:51 PM 1
Share

I would say that if used sparingly in the proper environment, raycasting isn't too performance expensive. With simple enough colliders in a simple enough scene (or at least somewhat optimally set up scene), raycasts themselves every so often are not terribly costly (depending on your hardware some). If Unity is depth-sorting and performing proximity tests, then it becomes even less costly. Ray-Cube, Ray-Sphere, Ray-Triangle, Ray-Plane and Ray-Capsule intersection algorithms are fairly well-discussed and optimized. But this cost gets added to rendering and other costs so beware.

avatar image Jeston 1 · Oct 14, 2010 at 08:35 PM 0
Share

If you setup your layers correctly, you can be very clever in masking only the objects you really care about colliding with. In my game I have things like outter terrain and fancy stuff is ignoring raycasts but things like players and the level geometry turned on

avatar image skovacs1 · Oct 14, 2010 at 08:54 PM 0
Share

@Jeston: Agreed. The use of layermasks was the answer that I gave Dr. Watson in a previous question related to this topic. I would consider that part of "the proper environment," but it is worth mentioning.

avatar image Dr. Watson · Oct 16, 2010 at 11:06 PM 0
Share

Yea the rendered parts of my prefabs are the only parts, in all of my prefabs, that aren't ingnoring raycasts.

Show more comments
avatar image
1

Answer by jason-fisher · Jan 16, 2015 at 06:25 PM

I think the simplest/most aesthetically approach would be to add a 'scale check' game object with a collider the size of the max scale.

Enable that game object when you start to scale and use its collider impact to trigger force to try to 'pop' the character out of the area that is too small.

If there is no collider impact there, you are safe. If there is, you have a specific body you can ray cast against -- or cone cast into the direction you are attempting to slide into as you scale (which you get from the impact point vs character point). Repeat over a few frames and you can get a smooth motion. If there is nowhere to 'slide' to, your head is going to hit the ceiling and explode.

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 mafio89 · Apr 16, 2014 at 03:34 AM

What about making a mix of the two? You use the trigger and the second a trigger is detected you enter in RayCast mode and auto-disable raycast mode if raycast detect nothing in 10 seconds as an example.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Collision detection with raycast 1 Answer

Collision point on surface? 1 Answer

Raycast for 2d with touch. 0 Answers

AttackDammage errors 1 Answer

Raycast from camera not hitting Terrain 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges