Raycast collider detector lags horribly?

This script makes the game drop down from 30 fps to 3 fps. Here they seem to do it no problem(start at 1:00): Intruder Series 002 Concussion Grenades - YouTube

What is making the lag?

#pragma strict

var wake : boolean;

var power : float = 1000;
var damage : int = 1;
var radius : float = 3;

function Start () {

}

function Update () {
	if(wake == true) {
		Explode();
	}
}

function Explode() {
 
	var colliders : Collider[] = Physics.OverlapSphere (transform.position, radius);
	Debug.Log("Count of colliders = "+colliders.Length);
		for (var collider : Collider in colliders) {
			//if (collider.tag == "brick" || collider.tag == "player") {
				Debug.Log("Found brick or a player");
				var hit : RaycastHit;
				if (Physics.Linecast(transform.position, collider.transform.position, hit)) {
					if (hit.collider == collider) {
					Debug.Log("No obstructions");
						if (hit.rigidbody)
							hit.rigidbody.AddExplosionForce(power, transform.position, radius, 3.0);
				}
			}
		}
	//}
}

When do you turn "wake" true and false? My guess is, as you call it from your Update-loop, you should set wake = false after you call explode()...

–

This helps a lot, but in the game I could expect several of these going off at the same time. What could I add to the colliders(not tags or layers) that would single them out so only a few rays are drawn to the colliders?

–

you probably could add the forces of multiple exlposions for each collider before applying them, but I'm not sure if this is needed. remember you only need to add an explsion force once per collider and explosion, it's not a continuous force, more an impulse. unless you target the really old android or ios devices, you should be able to make quite a few explosions. But it's true, I'd remove the Log calls from the update for testing, rather set a boolean that you could display in a GUI or such.

–

Converted to an answer for future seekers :)

–

1 Answer

1

Debug.Log every frame will kill your framerate… That’s damn expensive as it walks the stack

I'd suggest Debug.DrawRay with a specific colour and a time to indicate what happened.

–