Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by martipello · Mar 08, 2016 at 01:03 PM · playercontrolleroptimization

optimising player controller

So i have a player controller and im new to unity so maybe im just looking for an experienced person to point out a few of my more major faux pas, everything seems to work, although my animation clips dont always seem to fire and a bool i use for checking if the player is grounded seems to take longer than it should to fire meaning quite often i can smash the space bar and my player will just sit there doing nothing, i think my script could be a lot better and a lot simpler im just not experienced enough to know how, it has a lot of if statements and im not sure if i would be better off having less || and more if conditions, or if its my use of late update, or if theres a better way in handling it all together, im looking for wisdom i guess lol. I hope this doesnt breach any forum rules on asking for code i apologise if it does, here is what i have and what i know

  • my awake function grabs components i need later

     void Awake()
     {
         playerRigidbody = GetComponent<Rigidbody>();
         animator = GetComponent<Animator>();
         source = GetComponent<AudioSource>();
         Vector2 movement = Vector2.zero;
     }
    
    
  • my update function controls the buttons i use on a canvas for mobile devices the associated methods are elsewhere ill add them in a moment including the methods im using for the animations

     void Update()
     {
    
         // movement for buttons
         if (moveLeft && !moveRight)
         {
             movingLeft(1, 0.1f);
             animationClips();
         }
    
         if (moveRight && !moveLeft)
         {
             movingRight(1, 0.1f);
             animationClips();
         }
         if (myPlayerJump && isGrounded)
         {
             jump();
             animationClips();
         }
         if (!myPlayerJump)
         {
             falling();
         }
     }
    
    
  • im using lateupdate for keyboard movement

     void LateUpdate()
     {
         //movement for keyboards
         if (Input.GetKey(KeyCode.LeftArrow))
         {
             movingLeft(1, 0.1f);
             animationClips();
         }
         if (Input.GetKey(KeyCode.RightArrow))
         {
             movingRight(1, 0.1f);
             animationClips();
         }
         if (Input.GetKeyDown(KeyCode.Space))
         {
             jump();
             animationClips();
         }
     }
    
    
  • so then the main body is the methods im calling from the buttons for movement, the actual movement itself, the animations changing states and collisions so heres the methods for the buttons and the actual movement

     public void MoveMeLeft()
     {
         moveLeft = true;
     }
    
     public void StopMeLeft()
     {
         moveLeft = false;
     }
     public void MoveMeRight()
     {
         moveRight = true;
     }
    
     public void StopMeRight()
     {
         moveRight = false;
     }
     public void playerJump()
     {
         myPlayerJump = true;
     }
    
     public void playerJumped()
     {
         myPlayerJump = false;
     }
    
     //actual movement
    
     public void movingLeft(float moveInput, float changeSpeedSecs)
     {
         movement.x = Mathf.Max(-1, movement.x - moveInput * (Time.deltaTime / changeSpeedSecs));
         playerRigidbody.AddForce(movement * speed);
         if (moveInput < 0)
         {
             jumpDirection = false;
             moveDirection = false;
         }
     }
     public void movingRight(float moveInput, float changeSpeedSecs)
     {
         movement.x = Mathf.Max(1, movement.x - moveInput * (Time.deltaTime / changeSpeedSecs));
         playerRigidbody.AddForce(movement * speed);
         if (moveInput > 0)
         {
             jumpDirection = true;
             moveDirection = true;
         }
     }
     public void jump()
     {   if (isGrounded && !isJumping)
         {
             playerRigidbody.AddForce(0, jumpSpeed, 0);
             if (allGlobals.fx == true)
             {
                 source.PlayOneShot(jumpNoise, 1f);
             }
             isJumping = true;
             isGrounded = false;
         }
     }
     public void falling()
     {
         playerRigidbody.AddForce(0, 0, 0);
     }
    
     
    
    
  • my animations changing states

     public void animationClips()
     {
         if (moveDirection && isGrounded)
         {
             animator.SetFloat("rollRight", 0);
             animator.SetFloat("rollLeft", 2);
             animator.SetFloat("jumpRight", 0);
             animator.SetFloat("jumpLeft", 0);
         }
         if (!moveDirection && isGrounded)
         {
             animator.SetFloat("rollRight", 2);
             animator.SetFloat("rollLeft", 0);
             animator.SetFloat("jumpRight", 0);
             animator.SetFloat("jumpLeft", 0);
         }
    
         if (isJumping && jumpDirection)
         {
             animator.SetFloat("jumpRight", 2);
             animator.SetFloat("jumpLeft", 0);
         }
         if (isJumping && !jumpDirection)
         {
             animator.SetFloat("jumpLeft", 2);
             animator.SetFloat("jumpRight", 0);
         }
     }
    
    
  • and finally my collisions which is probably whats slowing everything down either that or my animationClips, to me this all looks great but i must be doing something crazy for the performance issues im seeing

     void OnCollisionEnter(Collision collision)
     {
         if (collision.collider.tag.ToLower() == "walls" || collision.collider.tag.ToLower() == "cylinders" ||    
         collision.collider.tag.ToLower() == "rotate" || collision.collider.tag.ToLower() == "breakgem" ||  
         collision.collider.tag.ToLower() == "block")
         {
             isGrounded = true;
             isJumping = false;
             animationClips();
         }
         if (collision.collider.tag.ToLower() == "bounce")
         {
             isJumping = true;
             animationClips();
         }
         if (collision.collider.tag.ToLower() == "newcoins")
         {
             if (allGlobals.fx == true)
             {
                 source.PlayOneShot(coinNoise, 0.3f);
             }
             Destroy(collision.gameObject);
             globals.allCoinsCollected++;
         }
     }
     void OnCollisionExit(Collision collision)
     {
         if (collision.collider.tag.ToLower() == "walls" || collision.collider.tag.ToLower() == "cylinders" ||      
         collision.collider.tag.ToLower() == "rotate" || collision.collider.tag.ToLower() == "breakgem" || 
       collision.collider.tag.ToLower() == "block")
         {
             isGrounded = false;
             isJumping = false;
             animationClips();
         }
     }
    
    
    
    
    
    
    
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 Owen-Reynolds · Mar 08, 2016 at 03:19 PM 0
Share

Standard debugging is to isolate and test everything. For example, remove the animation so you can test whether it's getting the buttons presses(?) quickly enough. Or turn green when grounded, white when not.

It's almost always something you didn't test, since you knew that couldn't possibly be the problem.

(also, moved this to HelpRoom area.)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Drakonno · Mar 08, 2016 at 01:15 PM

Since I'm not as experienced, as You might want, I'd just share with that controller:

https://github.com/prime31/CharacterController2D

It has a lot of to learn from. You will find few things which You can fix (for example how to handle Your animation clips), and do better thanks to it. I don't claim of course any rights. It's someone's job, who is better.

Oh, and about "breaking rules", it not perfectly fits Question-Answer format. You should probably ask for such help on forum. Anyway, unity-answers aren't as strict as stackoverflow.

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 martipello · Mar 08, 2016 at 01:26 PM 0
Share

thank you very much just checking this out it seems very helpful although now really what im looking for but a very cool reference all the same, and the breaking rules thing your right, im looking for an answer but havent a strict question that could possibly help people later i guess, thanks again

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

44 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

Related Questions

How to mix 3 player controller scripts for a 3D isometric game 0 Answers

how to make unlock/lock sytem for player selection? 0 Answers

Swipe Control for Android , Move left and Right ( vertical ) 1 Answer

Any ideas in relate to a cube rolling? thank you! 0 Answers

Coroutine endlessly looping? 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