Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by chronicfail · May 30, 2013 at 01:33 PM · javascriptperformanceoptimizationweapons

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;
 }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image chronicfail · May 30, 2013 at 01:50 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to fix duplication of materials with blender? 0 Answers

No me muestra el cuello de botella en Unity Profiler 1 Answer

UI-based game memory and performance issue. 1 Answer

Calling gameobject.transform vs. just calling transform directly - Performance negligible? 1 Answer

MaterialPropertyBlock (MPB) - Does it effect performace if MPB has property that shader doesn't have? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges