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 Posly · Feb 25, 2012 at 12:47 AM · optimizationlagslowframe ratepreformance

Why does this script lag so much?

When I run this script the whole game lags a lot. I don't have any graphics just a bunch of cubes, 3 to be exact. Here it is:

//this is the player we are targeting

var target : Transform;

//this is the speed that the enemy walks

var walkingSpeed : float;

 function Update () {

 

 //variables that are going to be used throughout the script

 var fwd : Vector3 = transform.TransformDirection(Vector3.forward);

 var collider : CharacterController = GetComponent(CharacterController);

 var ourX : int = transform.position.x;

 var ourZ : int = transform.position.z;

 var theirX : int = target.position.x;

 var theirZ : int = target.position.z;

 

     //this makes the enemy move towards the player

     

         //this makes the enemy move forward

         collider.SimpleMove(walkingSpeed * fwd * Time.deltaTime);

         

         //makes the enemy look at the player

         //first quadrant

         if(theirZ > ourZ && theirX < ourX) {

         

             print(1);

         }

         

         if(theirZ > ourZ && theirX > ourX) {

         

             print(2);

         }

         

         if(theirZ < ourZ && theirX < ourX) {

         

             print(3);

         }

         

         if(theirZ < ourZ && theirX > ourX) {

         

             print(4);

         

         }

             

         

     

         

         

         

         

}

@script RequireComponent(CharacterController)

Thanks! I don't really want a fix to this script, I just want to know whats lagging so I don't do this again.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by rabbitfang · Feb 25, 2012 at 01:31 AM

It lags because you are printing to console every frame (times 3). Printing something to console takes a lot of time (relatively) and so when you do this every frame, things start to slow down.

Try to limit the amount you are printing.

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 Posly · Feb 25, 2012 at 03:08 PM 0
Share

Thank you!

avatar image
1

Answer by jaised · Feb 25, 2012 at 01:25 AM

I may not be correct in this, but try debugging and see what collider variable you actually get at line: collider.SimpleMove(walkingSpeed * fwd * Time.deltaTime); collider is a variable used within GameObject itself so it may be referencing that instead which may cause some weird things. Or does the program act the way you think it should?

EDIT: Also, in your Update() you are getting the CharacterController every update call which means every frame, so that eats a lot of time. Therefore, what you should do is remove that line from Update() and put it in a new function Start(); like so: (also rename collider to controller, or something)

private var controller : CharacterController;
function Start() {
    controller = GetComponent(CharacterController);
    if(!controller)
        Debug.Log("Cannot find player controller.");
}

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 Posly · Feb 25, 2012 at 03:08 PM 0
Share

Thank you!

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

8 People are following this question.

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

Related Questions

Android lag 1 Answer

WebGl Using too much memory 1 Answer

Very Bad Lag from ~20 Large Particles 0 Answers

OnTriggerEnter causing lag spike? 0 Answers

Why does my program run very slow on MAC OS when built from version 5.3.4up when it runs normal speed like it should when built using 5.2.4f1? 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