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?
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.
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.)
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.
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
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.
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.
Good point! collider.Raycast will ignore all obstacles, giving no chance to the enemy... unfair even to the worst guys!
Your answer
Follow this Question
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