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(); } }
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.)
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.
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
Follow this Question
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