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 henrywoodaqp · Oct 23, 2020 at 01:36 PM · profilerthird person controllercpu

Third Person Controller is very CPU intensive

I've been testing my game out and it started getting very laggy, I checked the unity profiler and it states that the Third Person Controller is very intensive, I need the TPC because without it their is no game, someone please help me out here. Heres the code if you need to look at it:

      void Update()
             {
                 float horizontal = joystick.Horizontal;
                 float vertical = joystick.Vertical;
                 Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
         
                 killCounter.GetComponent<Text>().text = kill.ToString();
         
                 if (direction.magnitude >= 1f)
                 {
                     float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
                     float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
                     transform.rotation = Quaternion.Euler(0f, angle, 0f);
         
                     controller.Move(direction * speed * Time.deltaTime);
                     animator.SetFloat("MovementSpeed", 1);
                 }
         
                 if(direction.magnitude <= 0f)
                 {
                     animator.SetFloat("MovementSpeed", 0);
                     dustTrail.Play();
                     FindObjectOfType<Audio>().Walk();
                 }
         
                 if(kill >= desiredKills)
                 {
                     light.color = (Color.cyan);
                     skull.SetTrigger("Extinguished");
                 }
             }






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
0

Answer by unity_ek98vnTRplGj8Q · Oct 23, 2020 at 05:47 PM

Calls like FindObjectOfType and GetComponent can cause serious performance issues if used every frame. Assuming that you the objects you are finding are always the same, you should find the component once and save the result.

 private Text killCounterText;
 private Audio audio;
 
 void Start(){
     killCounterText = killCounter.GetComponent<Text>();
     audio = FindObjectOfType<Audio>();
 }
 
 void Update(){
     killCounterText.text = kill.ToString();
 
     audio.Walk();
 }
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 rh_galaxy · Oct 23, 2020 at 05:54 PM

In that piece of code the only thing that I see you can optimize is

 killCounter.GetComponent<Text>().text = kill.ToString();
 FindObjectOfType<Audio>().Walk();

Find and GetComponent should be avoided in Update().

 dustTrail.Play();

Sometimes particle systems can cause frameskips when they are started, but it is possible to fix it, not sure if it's that, but you do Play every frame, and Audio Walk every frame.

Otherwise it's down to what code that runs when you do this, you have to follow the code further

 controller.Move(direction * speed * Time.deltaTime);
 FindObjectOfType<Audio>().Walk();

You can also put in timing code to meassure certain parts of the code.

 float t1 = Time.realtimeSinceStartup;
 //code to meassure
 Debug.Log("Time taken: " + (Time.realtimeSinceStartup - t1) * 1000.0f);
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

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

Related Questions

CPU Usage Question (iOS) 0 Answers

iPhone Performance Question relating to CPU-WAITS-GPU 1 Answer

Profiler Windows - What is "Others" 1 Answer

Low FPS with no shown reason 2 Answers

Terrain.Heightmap.RenderStep3 taking 90 % of CPU. Why ? 2 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