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 TheFrankman123 · Apr 10, 2012 at 12:56 AM · fpsupdateprofiler

Script lowering frame rate

Hello UA people.

My frame rate isn't particularly low at the moment, but i am exporting to flash so want to optimize it as much as i possibly can.

Profiler is indicating that the below script is at 39.8% not sure exactly what these figures mean, but it surely can't be good.

Can anyone recommend alterations to this script that would be more efficient? It should be pretty straight forward to you guys, but if anything is unclear i'll happily explain.

I realise there is a lot in the update, but as far as i can figure there isn't any other way, but i'm sure i'm wrong! haha.

Cheers guys!

 #pragma strict
 var playerInRange : boolean = false;
 var waypoints : Transform [];
 var currentPoint : int = 0;
 var state : String = "patrol";
 var speed : float = 3;
 var gravity : float = 15;
 private var controller : CharacterController;
 private var player : Transform;
 var shooting : boolean = false;
 var bulletFab : Rigidbody;
 var gunTip : Transform;
 var power : float = 1000;
 var shotDelay : float = 1;
 
 private var bullet : Transform;
 
 
 function Start(){
 
     player = gameObject.FindWithTag("Player").transform;
     controller = GetComponent(CharacterController);    
 }
 
 function OnTriggerEnter (hitPlayer : Collider) {
     
     
     
     if(hitPlayer.gameObject.tag == "Player") {
             
         Debug.Log("collision detected");
 
         playerInRange = true; 
         
         
     }    
     
     
 }
 
 function Update () {
     if(player) {
     
         if (playerInRange == true) {
             state = "chase";
             Debug.Log("state is chase");    
         }else{
             state = "patrol";
             Debug.Log("state is patrol");
         }
         
         if(state == "patrol"){
         
             if(currentPoint < waypoints.length) {
                 Mover(waypoints[currentPoint].position);    
             }
         
         } else if (state == "chase"){
         if(!shooting) shootStuff();
         Mover(player.position);
         }
     }
 }
 
 function Mover(target : Vector3){
 
     var diffVector: Vector3 = target - transform.position;
     
     var movement : Vector3;
     
     if(diffVector.magnitude > 1){
      movement = (diffVector.normalized * speed);//without this it will run toward something and slow down when it gets close.    
      
     }else{
         currentPoint = (currentPoint + 1) % waypoints.length;
         
     }
     
     movement.y -= gravity * Time.deltaTime;
     controller.Move(movement * Time.deltaTime);
     
     if(state == "patrol") {
         transform.LookAt(target);
     } else if (state =="chase"){
         transform.LookAt(Vector3(player.transform.position.x, transform.position.y, player.transform.position.z));
     }
     // look at waypoints, works because waypoints are the same height as the player.
     // then if it's chase look at the player but only on the y axis
     // also the other axis are frozen. 
 }
 
 function shootStuff(){
     
     shooting = true;
     
     var fwd = transform.TransformDirection(Vector3.forward);
     var bulletShot : Rigidbody = Instantiate(bulletFab, gunTip.position, gunTip.rotation);
     bulletShot.AddForce(fwd);
     
     yield WaitForSeconds(shotDelay);
     shooting = false;
     
     if(gameObject.FindWithTag("bullet")) {
         
         yield WaitForSeconds(4);
         Destroy(gameObject.FindWithTag("bullet"));
         
     
     }
     
 }
Comment
Add comment
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

3 Replies

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

Answer by simonmc · Apr 10, 2012 at 03:18 AM

you aren't doing anything in an outrageously silly way. However, a couple of tips that might help:

1) use enums instead of strings to hold your state variable. E.G.

 enum MovementState {PATROL, CHASE}
 var currentState : MovementState = MovementState.PATROL;
     
 void Update() {
    if(currentState == MovementState.PATROL) {
        //Patrol code
    } else if(currentState == MovementState.CHASE) {
        //Chase code
    }
 }



2) You should delegate the bullet destroying behaviour at the end of your shootStuff() method to the bullet itself. E.G create a new Script DestroyAfterDelay.js and attach it to your bullet prefab. In DestroyAfterDelay.js have:

 var lifeTime : float;
 
 function Start() {
     yield WaitForSeconds(lifeTime);
     Destroy(gameObject);
 }

or something along those lines

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
avatar image
0

Answer by Aydan · Apr 10, 2012 at 01:08 AM

Use this just change the frameRate variable to whatever you want

 var frameRate : int = 300;
 
 function Awake () {
     // Make the game run as fast as possible in the web player
     Application.targetFrameRate = frameRate;
 }

Source: http://unity3d.com/support/documentation/ScriptReference/Application-targetFrameRate.html

EDIT

if you go Edit > Project Settings > Time

it will give you three options:

Fixed Timestep

Maximum Allowed Timestep

Time Scale

the fixed timestep will allow you to control the framerate and the maximum allowed timestep will allow you to set the maximum framerate.

Hope it helps

Comment
Add comment · Show 6 · 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 TheFrankman123 · Apr 10, 2012 at 01:16 AM 0
Share

Surely Unity should just do this automatically? why wouldn't it want to supply the best possible frame rate?

avatar image TheFrankman123 · Apr 10, 2012 at 01:40 AM 0
Share

Besides, i'm more looking at how i can re-write the code itself more efficiently, since i'm sure this is a bit of a dirty way to do it.

avatar image Aydan · Apr 10, 2012 at 01:48 AM 0
Share

oh I see what you mean

avatar image Aydan · Apr 10, 2012 at 01:53 AM 0
Share

read through this see if you can find anything useful http://answers.unity3d.com/questions/37293/general-ways-to-optimize-frame-rate.html

avatar image Eric5h5 · Apr 10, 2012 at 02:50 AM 0
Share

The default value for Application.targetFrameRate is -1, which means "as fast as possible". Setting it to 300 is basically useless...it doesn't mean it somehow forces Unity to run at 300 fps, which is of course impossible.

Show more comments
avatar image
0

Answer by rutter · Apr 10, 2012 at 02:16 AM

You might try deep profiling. It'll slow down your game while it's active, but it also provides a lot more information, specifically including nested function calls, which makes it much more useful for identifying hotspots.

Beyond that, I notice two things:

Calling Debug.Log() on a regular basis can slow down your game. Each log can cost several milliseconds, which adds up pretty quickly in performance-critical functions like Update().

Looks like you're calling WaitForSeconds() from shootStuff(), which in turn is called from Update(). Calling any blocking operation from your update loop seems like an extremely bad idea, if it'll work at all. Perhaps instead you might compare the last shot time against Time.time, or start a coroutines to manage some of those flags.

Otherwise, I didn't notice anything obviously amiss, but it's easy to overlook problems. My main advice is still to check out deep profiling, and look for functions which seem to take more time than they ought to.

Comment
Add comment · Show 3 · 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 TheFrankman123 · Apr 10, 2012 at 02:45 AM 0
Share

Thank you Rutter. This was the kind of answer i was looking for. I wont accept the answer just yet, see if anyone else notices anything.

Thanks for point out the debug, i kind of assumed that when you built the game it would ignore the debug since it doesn't actually show up anyway.

Also, thinking about it, not sure i even need the wait for seconds anymore...

Cheers!

avatar image simonmc · Apr 10, 2012 at 03:23 AM 2
Share

using WaitForSeconds() with a yield statement does not block execution of the update loop. Unity JS automatically turns any functions with yield in them into coroutines, and wraps the function calls in StartCoroutine(), so the shootSutff() call is actually equivalent to StartCoroutine(shootStuff()).

avatar image rutter · Apr 10, 2012 at 06:01 PM 0
Share

I see. Thanks for the explanation. I'm far more used to C# coding, and sometimes it shows a bit painfully. ;)

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

7 People are following this question.

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

Related Questions

Android update math at higher hz? 2 Answers

Updating at frameRate some fixedUpdated gameObject 1 Answer

Improving custom shader performance? 0 Answers

CPU Usage Not Constant on iOS 11.2 (Metal) in Profiler and Xcode FPS CPU time != Profiler 1 Answer

Profiler.CollectGlobalStats high fps problem 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