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
0
Question by mabit · Nov 28, 2012 at 03:36 AM · optimization

Help optimize code

I am looking for some help with this code, I am making the game for android so every CPU cycle counts. With 3 enemies the game runs at approx 45fps. With 7 enemies it drops to around 15 fps. Checking the profiler this script is the next highest activity after camera render. I am looking for any help to optimize this code to see if that helps with FPS. To increase performance the enemy does not have a rigidbody as the physics involved were hogging the CPU.

 #pragma strict
 
 var player : Transform;
 static var mummySpeed : float = 6;
 var direction: float;
 var timesHit:int;
 var touchingPlayer:boolean;
 var immune:boolean;
 var mummyTexture:Texture2D;
 var mummyHitTexture:Texture2D;
 var mummyDamage1:Texture2D;
 var mummyDamage2:Texture2D;
 var mummyRig:GameObject;
 
 
 var playerMidSection: Vector3;
 
 
 function Update () {
     if(Vector3.Distance(player.position, transform.position) > 8){
         touchingPlayer = false;
         direction = Random.value;
         
         // Cast ray to player
         var hit : RaycastHit;
         var rayDirection = (player.position) - transform.position;
         
         
         if (Physics.Raycast (transform.position, rayDirection, hit)) {
             
             if (hit.transform == player) {
                 // enemy can see the player!
                 transform.LookAt(player);
                 transform.Translate(Vector3(0,0,mummySpeed) * Time.deltaTime);
                 
                 //Debug.Log("I see You");
             }
             else {
                 // there is something obstructing the view randomly wander
                 //Debug.Log("Im blind!!");
                 transform.Translate(Vector3(0,0,mummySpeed) * Time.deltaTime);
             }
         }
         //Debug.DrawLine(transform.position, player.position, Color.green);
         var bodyForwardRay = transform.TransformDirection(Vector3(0,0,2));
         if(Physics.Raycast(transform.position, bodyForwardRay, hit, 2)){
             if (hit.collider.gameObject.tag == "Wall" ||  hit.collider.gameObject.tag == "Chest" ||  hit.collider.gameObject.tag == "Eye" ||  hit.collider.gameObject.tag == "Life" ||  hit.collider.gameObject.tag == "Sarchoph"){
                 if (direction < 0.5){
                     transform.Rotate (0,35,0);
                 }
                 else{
                     transform.Rotate (0,-35,0);
                 }
             }
         }
     }
     else
     {
         touchingPlayer = true;
         GlobalVariables.health = GlobalVariables.health - 0.2;
     }
 }
 
 
 function OnMouseDown ()
 {
     if (touchingPlayer == true && immune == false){
         MummyHit();
     }
 }
 
 function MummyHit(){
     mummyRig.renderer.material.mainTexture = mummyHitTexture;
     immune = true;
     timesHit++;
     yield WaitForSeconds(0.5);
     immune = false;
     if (timesHit == 1){
         mummyRig.renderer.material.mainTexture = mummyDamage1;
     }
     if (timesHit == 2){
         mummyRig.renderer.material.mainTexture = mummyDamage2;
     }
     if (timesHit == 3){
         Destroy(gameObject);
     }
 }
Comment
Add comment · Show 2
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 Benproductions1 · Nov 28, 2012 at 04:26 AM 0
Share

You Should probably usee a Linecast and not a raycast, as a raycast might be more expensive. It doesn't allow you to tell what your colliding with, only if you are colliding with something.

avatar image mabit · Nov 29, 2012 at 01:14 AM 0
Share

Found this, and gave it a test, Linecast was slower. Not sure why as it gives less info but it dropped to 10fps with that change.

http://answers.unity3d.com/questions/164770/raycast-and-linecast.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Avaista · Nov 28, 2012 at 04:46 AM

Also look in Collider.Raycast, Collider.Raycast

I'm not 100% sure, but I believe this is much more efficient, as it only deals with a single collider.

As for the second one, look into layer masks, and using layers instead of tags LayerMask.NameToLayer

 int layermask = 1 << LayerMask.NameToLayer("Wall");
 if(Physics.Raycast(transform.position, transform.forward, hit, 2, layermask))
 {...}


More Info On Layers, which are mad useful, can be found here: Layers

EDIT Oh didn't notice before but you are using Renderer.material as well, you probably want to use Renderer.sharedMaterial.

Renderer.material creates a clone of the material, then returns it, so each time you use it, you are cloning a material.

You should clone the material at the beginning of the class, so that all mummies have their own material,

public void Awake() { renderer.material; }

Comment
Add comment · Show 1 · 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
avatar image mabit · Nov 29, 2012 at 12:57 AM 0
Share

Thanks for the advice, I am currently using the walls on a layer as I have a $$anonymous$$inimap which when items are purchased this allows the player to see the walls. This is done by changing the layer of the walls from one the $$anonymous$$imap cam doesnt see to one it does. Would that not interfere with the collisions if I make it collide with layer?

Sorry if its a silly question, what started out as a simple project has grown and I am a designer who is coding way out of my depth

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

12 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

Related Questions

Improving code performance 1 Answer

Code Optimization Question 1 Answer

Question about procedurally generated content efficiency 2 Answers

Draw calls on PC and Console? 0 Answers

Webplayer crashes on Chrome, but not on any other browser. Any way to avoid ? 1 Answer


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