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();
animator = GetComponent();
source = GetComponent();
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();
}
}