Well, I have an extremely simple game, with blocks and a character that hits them and destroys the block. I use Raycast to find the blocks, like this (raycastOrigin is a transform right in front of the character):
if (Physics.Raycast(raycastOrigin.position, raycastOrigin.forward, out hit, range, blocksLayer)) {...}
While I'm hitting blocks the fps drops a lot, and on the profiler window Active Rigidbodies and Number of Contacts were really high, so I supposed the problems were with Raycasts, since I just learned they are really expensive.
I tried to do some things (read below) and it got just a little better, but fps is still dropping too much, as you can see here: [http://imgur.com/7Ew3vGy][1].
What I did: I put all the blocks that can be hit in a single layer and I start coroutines to make the raycasts and deal with collisions.
I tried other things like changing Update to FixedUpdate, but no changes.
I don't know what to do now, I started to learn about Unity recently and don't have the necessary knowledge to handle this properly. Is there a way to fix my problem or to use a replacement for Raycast that doesn't make fps drop?
My actual code where I am Raycasting:
void Update () {
time = time + Time.deltaTime;
if (Input.GetButton("Fire1") && time > nextFire)
{
nextFire = time + fireDelta;
StartCoroutine(FindBlockRaycasting());
nextFire = nextFire - time;
time = 0.0F;
}
}
private IEnumerator FindBlockRaycasting()
{
if (Physics.Raycast(raycastOrigin.position, raycastOrigin.forward, out hit, range, blocksLayer))
{
target = hit.collider.gameObject;
warn = target.tag;
if (target.tag == "Destroyable")
{
animator.Play("HitWithArm", -1, 0F);
target.GetComponent().GetHit();
}
}
yield return new WaitForSeconds(0);
}
[1]: +http://imgur.com/7Ew3vGy
↧