I have a script that whenever I activate it I get a drop of framerate from 500-range to 30-45fps, but it’s only for one frame (It’s because of a for loop that runs a max of 5 iterations)
The script is only activated occasionally, a max of about once a second I’d say.
Is that something I should be worried about?
Posting your script would make it a lot easier for people to help you.
Yeah its not how big it is, its what you do with it.
It’s for checking whether or not an object is being hit by a light, and it uses linecasts to check if it’s in shadow or not.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(SphereCollider))]
[RequireComponent(typeof(MeshRenderer))]
[AddComponentMenu("Rendering/Check For Light")]
public class CheckForLight : MonoBehaviour
{
private SphereCollider col;
private Vector3[] castStartPoints = new Vector3[9];
public LayerMask layerMask;
void Start()
{
rigidbody.useGravity = false;
rigidbody.isKinematic = true;
col = GetComponent<SphereCollider>();
col.isTrigger = true;
Vector3 center = renderer.bounds.center;
float right = center.x + renderer.bounds.extents.x;
float left = center.x - renderer.bounds.extents.x;
float front = center.z + renderer.bounds.extents.z;
float back = center.z - renderer.bounds.extents.z;
castStartPoints = new Vector3[6];
castStartPoints[0] = center;
castStartPoints[1] = new Vector3(left,center.y,front);
castStartPoints[2] = new Vector3(left,center.y,back);
castStartPoints[4] = new Vector3(left,center.y,front);
castStartPoints[5] = new Vector3(left,center.y,back);
}
private bool inLight(Transform lightToCheck)
{
//-----------Update the renderer's position-------------
Vector3 center = renderer.bounds.center;
float right = center.x + renderer.bounds.extents.x;
float left = center.x - renderer.bounds.extents.x;
float front = center.z + renderer.bounds.extents.z;
float back = center.z - renderer.bounds.extents.z;
castStartPoints[0] = center;
castStartPoints[1] = new Vector3(right,center.y,front);
castStartPoints[2] = new Vector3(right,center.y,back);
castStartPoints[4] = new Vector3(left,center.y,front);
castStartPoints[5] = new Vector3(left,center.y,back);
//------------------------------------------------------
bool isInLight = false;
int noHitCount = 0;
for (int i=0;i<=5;i++)
{
RaycastHit hit;
if (!Physics.Linecast(castStartPoints[i], lightToCheck.position,out hit,layerMask.value))
{
noHitCount++;
}
else
{
Debug.Log(hit.collider.gameObject.name);
}
if (noHitCount >= 2)
{
break;
}
}
if (noHitCount >= 2)
{
isInLight = true;
}
else
{
isInLight = false;
}
return isInLight;
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Light Trigger")
{
print("hit");
if (inLight(other.transform))
{
Debug.Log("In Light");
}
else
{
Debug.Log("Not In Light");
}
}
}
}
Oh right, comment out the debuglogs. They’re murdering your framerate.
Huh… I had no idea debuglogs were that heavy. I’m still getting a framerate drop but it’s to 80-100FPS now, which should be perfectly fine.