- Home /
Code optimisation techniques?
Hello,
I have a large portion of game code which involves raycasting to do things and so on. Could anyone offer any advice on optimising this code? (and/or) Best ways for optimising for the future? I've not been programming that long and curious as to whether I should avoid the update function so much... Try and make the scripts smaller.
Any help would be appreciated, thanks for your time!
#pragma strict
var rayLength = 10;
var toolboxGUI : GameObject;
var keyCollectedGUI : GameObject;
var cabindoorlockedGUI : GameObject;
var cabindoorUnlockedGUI : GameObject;
var keyPadGUI : GameObject;
var controlpanelGUI : GameObject;
var schoolpowerGUI : GameObject;
var schoolgateGUI : GameObject;
var keyCollected : boolean = false;
var isOpen : boolean = false;
var key : GameObject;
var door : GameObject;
var keyPad : GameObject;
//School Lights
var schoolLight : GameObject;
var level : String;
//Daze bridge
var bridgeDaze : GameObject;
//Story booleans
private var gateOpen : boolean = false;
var powerKey : boolean = false;
var canUse : boolean = true;
var powerOn : boolean = false;
//Shack Lights
var redlightShack : Light;
var redindLight : GameObject;
var greenlightMat : Material;
//Opening Gate
var gateSound : GameObject;
var leftGate : GameObject;
var rightGate : GameObject;
var doorSound : GameObject;
//ElectricBoxes
var outerBox : GameObject;
var handle : GameObject;
var greenLight : GameObject;
var redLight : GameObject;
function Update()
{
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, fwd, hit, rayLength))
{
if(hit.collider.gameObject.tag == "Door" && keyCollected == false)
{
cabindoorlockedGUI.SetActive(true);
if(Input.GetKeyDown("e"))
{
cabindoorlockedGUI.SetActive(false);
}
}
else if(hit.collider.gameObject.tag == "Door" && keyCollected == true)
{
cabindoorUnlockedGUI.SetActive(true);
door = (hit.collider.gameObject);
if(Input.GetKeyDown("e") && isOpen == false)
{
cabindoorUnlockedGUI.SetActive(false);
hit.collider.gameObject.tag = "Untagged";
isOpen = true;
door.animation.Play("Cabin_Door_Open");
doorSound.audio.Play();
}
}
//SEARCHING TOOLBOX
else if(hit.collider.gameObject.tag == "Toolbox" && keyCollected == false)
{
toolboxGUI.SetActive(true);
if(Input.GetKeyDown("e"))
{
keyCollected = true;
toolboxGUI.SetActive(false);
keyCollectedGUI.SetActive(true);
Fade();
}
}
//USING INPUT KEYPAD
else if(hit.collider.gameObject.tag == "InputPad" && canUse == true)
{
keyPadGUI.SetActive(true);
if(Input.GetKeyDown("e"))
{
keyPad.GetComponent(KeyPad_ScriptFinal).showMessage = true;
keyPadGUI.SetActive(false);
canUse = false;
}
}
//Switching powerstation, power on
else if(hit.collider.gameObject.tag == "ControlPower" && keyPad.GetComponent(KeyPad_ScriptFinal).codeOK == true)
{
controlpanelGUI.SetActive(true);
if(Input.GetKeyDown("e"))
{
controlpanelGUI.SetActive(false);
redlightShack.light.color = Color.green;
redindLight.renderer.material = greenlightMat;
keyPad.GetComponent(KeyPad_ScriptFinal).codeOK = false;
powerOn = true;
bridgeDaze.GetComponent(Bridge_Daze).enabled = true;
}
}
//ELECTRIC BOX COMPONENT
else if(hit.collider.gameObject.tag == "TurnOn" && powerOn == true)
{
schoolpowerGUI.SetActive(true);
if(Input.GetKeyDown("e"))
{
schoolpowerGUI.SetActive(false);
schoolLight.SetActive(true);
ElectricBox();
hit.collider.gameObject.tag = "Untagged";
}
}
//BUTTON GATE COMPONENT
else if(hit.collider.gameObject.tag == "GateButton" && gateOpen == true)
{
schoolgateGUI.SetActive(true);
if(Input.GetKeyDown("e"))
{
schoolgateGUI.SetActive(false);
gateOpen = false;
hit.collider.gameObject.tag == "Untagged";
OpenGate();
}
}
else if(hit.collider.gameObject.tag == "SchoolEnter")
{
Debug.Log("SCHOOL HERE");
if(Input.GetKeyDown("e"))
{
//GUI
Application.LoadLevel(level);
}
}
else
{
toolboxGUI.SetActive(false);
cabindoorlockedGUI.SetActive(false);
cabindoorUnlockedGUI.SetActive(false);
keyPadGUI.SetActive(false);
controlpanelGUI.SetActive(false);
schoolpowerGUI.SetActive(false);
schoolgateGUI.SetActive(false);
}
}
}
function Fade()
{
yield WaitForSeconds (5);
keyCollectedGUI.SetActive(false);
}
function ElectricBox()
{
outerBox.animation.Play("Openboxhandle");
yield WaitForSeconds(2);
handle.animation.Play("SwitchPullDown");
redLight.SetActive(false);
greenLight.SetActive(true);
gateOpen = true;
}
function OpenGate()
{
gateSound.audio.Play();
leftGate.animation.Play("OpenGate1");
rightGate.animation.Play("OpenGate2");
}
Answer by mcapousek · Mar 30, 2015 at 04:13 AM
Use 'CompareTag(string tag)' method instead comparing strings with '=='!
Your answer
![](https://koobas.hobune.stream/wayback/20220613191604im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Raycast firerate shooting 1 Answer
Placing buildings 1 Answer
Creating a teleportation gun 1 Answer
How To Detect MouseClick Using Raycast Java Script 0 Answers
What are the differences between Physics.Raycast and Physics.RaycastNonAlloc ? 1 Answer