Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
1
Question by NinjaSu · Dec 27, 2012 at 12:04 AM · raycastrayhit

Raycast Performance

What is more expensive on performance?

(A)

  if (collider.Raycast(ray, out hit, 1000))
     {
     }

(B)

    if (Physics.Raycast(ray, out hit, 1000))
     {
     }

I plan to have enemies and shoot at them with raycast. (A) I can either put the if statement inside the enemy as a collider and it would check if it got hit, or (B) I can put the if on the player and send a message to the hit enemy.

Which is better?

My logic says that (B) is better, but I have no real knowledge between collider.Raycast and Physics.Raycast.

(C)

 if(Input.GetMouseButtonDown(0))
 {
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             
     if (Physics.Raycast(ray, out hit, 1000))
     {
     }
 }

If I do part (C) above. How many raycast am I actually sending? One?

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 NinjaSu · Dec 27, 2012 at 10:34 AM 0
Share

This is a dilemma.

If I do part (C) above:

How many raycast am I actually sending? One? How come it feels really expensive when I do it in my game? And yes Owen Reynolds, in my case I do care how fast it runs.

avatar image Owen-Reynolds · Dec 27, 2012 at 04:43 PM 0
Share

Who's doing the shooting? Is it a mouse-controlled gun from a 1st-person camera, or 3rd person? What part of shooting is the raycast supposed to accomplish?

Once you know, you can look for people who have solved similar things here. There are many, many threads on shooting (with and without ray casts.)

3 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by TMPxyz · Oct 30, 2013 at 07:37 AM

You need to test performance with your REAL environment. But well, I had taken a simple test on Physics.Raycast for reference;

I created a 2000*2000 terrain, with dozens 100*10*100(xyz) cubes over it, then every frame I cast a lot of Raycast from [x, 1000, z] downward (x and z are among [0,2000]).

2000 rays --> 3ms per frame for raycast

10000 rays --> 15ms per frame for raycast

this test is executed on a Notebook with i5-480M and GT445M.

Comment
Add comment · Show 1 · 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 TMPxyz · Nov 01, 2013 at 03:11 AM 3
Share

And for mobile device, I tested on an android device whose performance should be somewhere near iphone4.

the result is :

 100ray,1~3ms
 200ray,3~4ms
 300ray,5ms
 500ray,11ms
 1000ray,20ms
avatar image
1

Answer by aldonaletto · Dec 27, 2012 at 12:31 AM

I suppose that collider.Raycast is faster than Physics.Raycast, since only one collider is verified, but the extra code required to make this alternative work is probably way more expensive: when the player shoots, all enemy colliders must do a raycast to see whether they are hit. The physics engine is highly optimized and compiled to machine code, thus it probably runs a single Physics.Raycast much faster than a couple collider.Raycast called via script (even compiled to CIL, our scripts run much slower than machine code). You could reduce the enemy list only to those in front of the player, but this would also require extra script code... I suppose this is one of these cases where a man can't win the machine.

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
1

Answer by Owen-Reynolds · Dec 27, 2012 at 12:31 AM

Collider Raycast probably won't do what you need (so who cares how fast it runs.) The purpose of a typical raycast is to check for unknown objects blocking the line from me to the target. Using the layer system, you can have a raycast skip certain objects -- maybe of all the thorny bushes have colliders to stop movement, but a lazer blast goes right through them.

Collider.Raycast says it skips everything except that one object, so it's no good for "is anything blocking the way?" It might be good is maybe you know you are close enough to stab someone, but want to check exactly where the put the ichor spray.

For speed, finding and checking all the other objects in the scene takes time. A shorter raycast distance speeds it up by ruling out lots of things quickly. Using layers to skip (or Collider.raycast to skip all but one thing) is going to also speed you up.

Comment
Add comment · Show 1 · 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 aldonaletto · Dec 27, 2012 at 12:37 AM 1
Share

Good point! collider.Raycast will ignore all obstacles, giving no chance to the enemy... unfair even to the worst guys!

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

13 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

Related Questions

How can i detect 2 raycasthit2D in the same function storing their Vector2 position? 0 Answers

LineRenderer doesn't go to Hit.Point 1 Answer

Third person shooter need some help 0 Answers

Making a portal inspired game - Need help! 0 Answers

(Solved!) How to Destroy tagged items using raycast? 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