Characters and raycasts. An acceptable amount?

So I’m working on a control system for my characters. I want to it to be lightweight enough to allow quite a few characters on-screen on mobile devices if possible. But I don’t really know anything about how heavy raycasting in the physics engine is.

It appears I will probably need an approach similar to what I’ve seen in old-school Sonic games; to use raycasts (pixel sensors in the old games) at a few spots to determine how even the ground is, surrounding the character in a cylindrical fashion. This would sense whether the coming surface was high enough to step over, even enough to stay grounded, or if the coming surface is a ledge. Currently I have only one raycast straight down, using some trig to determine grounding, and getting the ground normal.

One more raycast in the movement direction shouldn’t hurt. But the problem is that when running parallel enough to an uneven surface, one straight ahead won’t catch it since it’s on the side. But to catch that properly, I would probably need to make an arc with them; placing them at 45 degree angles from each other.

And what of any surfaces behind the character that I have to detect? In all, this would bring me up to 8 raycasts; cardinal and diagonal directions, in order to have full detection. And that doesn’t even include the one to detect pushable objects.

My question is… am I becoming unreasonable? Am I just looking at this from the wrong angle, or do people tend to use this many? Does anyone see some way I can make this smarter?

I just did this in an Update function:

        for (int i = 0; i < 1000; i++)
        {
            Physics.Raycast(transform.position, new Vector3(i, 10000-i, i*i));
        }

And my framerate didn’t break a sweat. I put it up to 10000 and it dropped to 30 FPS so… go wild.

EDIT: Intel Core 2 Duo 2.2 GHZ, so not exactly an i7 here either.

In Master Thief (link in sig) each flashlight has 16 raycasts, and it runs fine on iPad 1 and a Sony Android I have with a Tegra 2. Some of the busy levels drop below 30fps, but not due to the raycasts. PhysX seems to handle them pretty well.

Though note that it’s not just about how many raycasts you have, but also what colliders are in your scene.

Good to know, thanks for the info

All the collision detection in 2D Controller (see sig) is done with raycasts. A typical mobile character might have 12-18 casts per frame and I’ve happily had one of them plus 8 simplified enemies (5 casts each, with some optimization to avoid casts that aren’t needed) running on 3GS. The CPU cost on 3GS for this was about 2-4ms per frame, on a newer phone its of course less.

Not completely negligible, but 10-40 should be fine unless you are CPU bound.