- Home /
Performance issue Raycast each frame
I'm creating a game for mobile devices where I use a Raycast to get the world position on an object where the player touches the screen. For example, I have a ground plane, and if I touch the screen, I want to know where exactly in the 3D world my finger is touching the plane.
Here's my code for the raycast:
Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
_Floor.collider.Raycast(r, out hit, 50);
_TargetPos = hit.point;
Ideally, I want to do this every frame, but it seems to be quite a demanding piece of code.
_Floor is the object where I want to get the world position on, so I only raycast on this collider. I really thought this would be a decent way of doing the calculation, but my framerate drops under 15 on my Android, so I'm actually looking for alternatives/optimizations to get the same calculation, but with a better framerate.
Perhaps I'm missing something simpler, because I only need to now the Vector3 where my mousePosition intersects with a specific 3D mesh.
Any help/tips are greatly appreciated, thanks in advance ;)
ps: I also tried a system where my Raycast is done every 0.x seconds, but it doesn't stop the framedrop and it gives less good results.
Ask yourself this question: When do you need to cast the ray? Each frame? Only when the player touch the screen?
I only cast the ray when the player touches the screen, and I've tried both ways: 1: cast a ray each frame the player touches, and 2: cast a ray every 0.2 seconds when player is touching.
$$anonymous$$y main issue here is to make the calculation itsself simpler.
Every 0.2 seconds isn't that much different from every frame, try something more like every 1, 2 or 5 seconds and see if there is a difference.
If not, maybe it's something else that slows down your project. Is there something more to your calculation code than you have posted above?
seconds is a lot different from every frame (5 x per second), and also it's not good enough for what I want. The calculation is intended to drag an object over another object, so casting every 5 seconds would really suck...
The raycast is basically all I'm doing right now, so I really need to find a better way of calculating the world position.
In case the calculation of my Raycast is allready "the most optimized way" of doing that calculation, it would also be nice to know...
I just want to make sure there isn't a simpler way of calculating a world point on a mesh where my mouseposition is.
A single raycast each frame should not drop the performance.
Ask yourself this other question: do you move a static collider, a kinematic rigidbody or a non-kinematic rigidbody?
$$anonymous$$oving a static collider or a non-kinematic rigidbody is pretty expensive, especially on mobile device.
Answer by Xtro · Jul 31, 2013 at 04:24 PM
Kryptos is right. Single raycast each frame is not a big performance deal. Moving static colliders is!
If you still want a solution for your original question,
Try to do it in FixedUpdate event rathar than Update event.
Notice that, this only helps when your actual framerate is bigger than the fixed time rate defined in Time settings. If your framerate is lower than fixed time rate, there is no difference between FixedUpdate and Update events. They both are called in the same rate which is your framerate.
It's ok I managed to find the problem, and it may be useful for some people: it didn't come from the ray casting, but from OnGUI which had too heavy stuffs to compute!