- Home /
Improving script performance
I have a script to manage the weapons my character is carrying, and allows swapping and dual wielding. It works fine, except it slows the FPS from about 60 to 30 or so. I know this script is the problem, I just don't know which part, which is why I have had to put in the whole script below. Please could someone tell me whhat I am doing wrong and how to fix it?
#pragma strict
var weaponReady : Transform;
//WEAPONS
var weaponA : Transform; //PRIMARY WEAPON
var weaponB : Transform; //SECONDARY WEAPON
var weaponC : Transform; //DUAL WIELDED WEAPON
var tempWeapon : Transform;
//PLACES TO STORE WEAPONS
var hand : Transform; //PRIMARY WEAPON
var back : Transform; //SECONDARY WEAPON
var holster : Transform; //PISTOLS, SWORDS STORED HERE
var leftHand : Transform; //USED WITH ONE HANDED WEAPONS AND DUAL WIELDING
var rightHand : Transform; //USED WITH ONE HANDED WEAPONS AND DUAL WIELDING
//SCRIPTS ASSIGNED AUTOMATICALLY
var weaponAScript : WeaponScript;
var weaponBScript : WeaponScript;
var weaponCScript : WeaponScript;
var tempScript : WeaponScript;
//BEGIN FUNCTIONS
function Start () {
//LOOK FOR WEAPONS AND ASSIGN THEM PROPERLY
if(weaponA)
{
weaponAScript=weaponA.GetComponent(WeaponScript);
weaponAScript.enabled=true;
if(weaponC)
{
weaponA.rigidbody.constraints=RigidbodyConstraints.FreezeAll;
weaponA.rigidbody.detectCollisions=false;
weaponA.collider.enabled=false;
weaponA.parent=leftHand;
weaponA.localPosition=Vector3.zero;
weaponA.rotation=leftHand.rotation;
weaponAScript.dualWielding=true;
}
else
{
weaponA.rigidbody.constraints=RigidbodyConstraints.FreezeAll;
weaponA.rigidbody.detectCollisions=false;
weaponA.collider.enabled=false;
weaponA.parent=hand;
weaponAScript.dualWielding=false;
weaponA.localPosition=Vector3.zero;
weaponA.rotation=hand.rotation;
}
}
if(weaponB)
{
weaponBScript=weaponB.GetComponent(WeaponScript);
weaponBScript.enabled=false;
weaponB.rigidbody.constraints=RigidbodyConstraints.FreezeAll;
weaponB.rigidbody.detectCollisions=false;
weaponB.collider.enabled=false;
weaponB.parent=back;
weaponB.localPosition=Vector3.zero;
weaponB.rotation=back.rotation;
weaponBScript.dualWielding=false;
}
if(weaponC)
{
weaponCScript=weaponC.GetComponent(WeaponScript);
weaponCScript.enabled=true;
weaponC.rigidbody.constraints=RigidbodyConstraints.FreezeAll;
weaponC.rigidbody.detectCollisions=false;
weaponC.collider.enabled=false;
weaponC.parent=rightHand;
weaponC.localPosition=Vector3.zero;
weaponC.rotation=rightHand.rotation;
weaponCScript.dualWielding=true;
}
}
//INITIALISATION OVER
function Update() {
if(Input.GetKeyDown(KeyCode.Q))
{
if(!weaponReady)
{
SwapWeapons();
}
if(weaponReady&&weaponC)
{
PickupWeapon("left");
}
if(weaponReady&&!weaponC)
{
PickupWeapon("left not dualing");
}
}
if(Input.GetKeyDown(KeyCode.E))
{
if(weaponReady&&weaponC)
{
PickupWeapon("right");
}
if(weaponReady&&!weaponC&&weaponB)
{
PickupWeapon("primary");
}
if(weaponReady&&!weaponC&&!weaponB)
{
PickupWeapon("primary no secondary");
}
}
}
function SwapWeapons () {
//DROP DUALWIELDING WEAPON
if(weaponC)
{
DropWeapon(weaponC);
weaponC=null;
}
//REASSIGN WEAPONS
tempWeapon=weaponA;
weaponA=weaponB;
weaponB=tempWeapon;
//REASSIGN SCRIPTS
weaponAScript=weaponA.GetComponent(WeaponScript);
weaponBScript=weaponB.GetComponent(WeaponScript);
//MOVE THEM AROUND
weaponA.parent=hand;
weaponA.localPosition=Vector3.zero;
weaponA.rotation=hand.rotation;
weaponAScript.enabled=true;
weaponB.parent=back;
weaponB.localPosition=Vector3.zero;
weaponB.rotation=back.rotation;
weaponBScript.enabled=false;
}
//STANDARD WEAPONDROP SCRIPT
function DropWeapon (weapon : Transform) {
weapon.Translate(1,0,0);
weapon.parent=null;
weapon.rigidbody.constraints=RigidbodyConstraints.None;
weapon.rigidbody.AddRelativeForce(transform.forward*100);
var weapScrip=weapon.GetComponent(WeaponScript);
weapScrip.enabled=false;
weapScrip.dualWielding=false;
yield WaitForEndOfFrame;
weapon.collider.enabled=true;
weapon.rigidbody.detectCollisions=true;
}
//WEAPON SWAPPING
function PickupWeapon (newGunHand : String) {
if(newGunHand=="left")
{
DropWeapon(weaponA);
weaponA=weaponReady;
weaponAScript=weaponA.GetComponent(WeaponScript);
weaponAScript.enabled=true;
weaponA.rigidbody.constraints=RigidbodyConstraints.FreezeAll;
weaponA.rigidbody.detectCollisions=false;
weaponA.collider.enabled=false;
weaponA.parent=leftHand;
weaponA.localPosition=Vector3.zero;
weaponA.rotation=leftHand.rotation;
weaponAScript.dualWielding=true;
weaponReady=null;
}
if(newGunHand=="right")
{
DropWeapon(weaponC);
weaponC=weaponReady;
weaponCScript=weaponC.GetComponent(WeaponScript);
weaponCScript.enabled=true;
weaponC.rigidbody.constraints=RigidbodyConstraints.FreezeAll;
weaponC.rigidbody.detectCollisions=false;
weaponC.collider.enabled=false;
weaponC.parent=rightHand;
weaponC.localPosition=Vector3.zero;
weaponC.rotation=rightHand.rotation;
weaponCScript.dualWielding=true;
weaponReady=null;
}
if(newGunHand=="primary")
{
DropWeapon(weaponA);
weaponA=weaponReady;
weaponAScript=weaponA.GetComponent(WeaponScript);
weaponAScript.enabled=true;
weaponA.rigidbody.constraints=RigidbodyConstraints.FreezeAll;
weaponA.rigidbody.detectCollisions=false;
weaponA.collider.enabled=false;
weaponA.parent=hand;
weaponA.localPosition=Vector3.zero;
weaponA.rotation=hand.rotation;
weaponAScript.dualWielding=false;
weaponReady=null;
}
if(newGunHand=="primary no secondary")
{
weaponB=weaponA;
weaponBScript=weaponB.GetComponent(WeaponScript);
weaponB.parent=back;
weaponB.localPosition=Vector3.zero;
weaponB.rotation=back.rotation;
weaponBScript.enabled=false;
weaponA=weaponReady;
weaponAScript=weaponA.GetComponent(WeaponScript);
weaponAScript.enabled=true;
weaponA.rigidbody.constraints=RigidbodyConstraints.FreezeAll;
weaponA.rigidbody.detectCollisions=false;
weaponA.collider.enabled=false;
weaponA.parent=hand;
weaponA.localPosition=Vector3.zero;
weaponA.rotation=hand.rotation;
weaponAScript.dualWielding=false;
weaponReady=null;
}
if(newGunHand=="left not dualing")
{
weaponA.parent=rightHand;
weaponA.localPosition=Vector3.zero;
weaponA.rotation=rightHand.rotation;
weaponAScript.dualWielding=true;
weaponC=weaponReady;
weaponCScript=weaponC.GetComponent(WeaponScript);
weaponCScript.enabled=true;
weaponC.rigidbody.constraints=RigidbodyConstraints.FreezeAll;
weaponC.rigidbody.detectCollisions=false;
weaponC.collider.enabled=false;
weaponC.parent=leftHand;
weaponC.localPosition=Vector3.zero;
weaponC.rotation=leftHand.rotation;
weaponCScript.dualWielding=true;
weaponReady=null;
}
}
function OnTriggerStay (col : Collider) {
print("collision");
if(col.transform.GetComponent(IncativeWeapon)){
print("weapon is ready");
weaponReady=col.transform;
}
}
Sorry, I know this kind of question has been asked a lot before, but I couldn't find anything that would help this script, as I can't really shorten the update much and I have cached what I can. Also sorry for posting such a large script, I just couldn't think of which bits to post in particular.
Answer by chronicfail · May 30, 2013 at 02:46 PM
Solved it myself, OnTriggerStay was the problem, switched to OnTriggerEnter and it is back to 60 frames per second.