Im working on a game 2d game that has some platforming. I followed this video as guidance and produced the following code:
public class PlayerControler : MonoBehaviour
public float maxWalkingSpeed = 10.0f;
public float jumpForce = 5.0f;
public float jumpInputSensitivityThreshold = 0.3f;
public float verticalAirControl = 0.5f;
public float horizontalAirControl = 2.0f;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool facingRight = false;
private bool isControlable = true;
private bool canMove = true;
private bool isGrounded = false;
private Animator playerAnimator;
// Use this for initialization
void Start ()
{
playerAnimator = GetComponent<Animator> ();
}
// Update is called once per physics calculation
void FixedUpdate ()
{
CheckIfGrounded ();
CheckInput ();
CheckHorizontalMovement ();
CheckVerticalMovement ();
}
private void CheckInput ()
{
bool isBlockHeld = Input.GetButtonDown ("Block");
bool isBlockReleased = Input.GetButtonUp("Block");
bool isPerformingAttack1 = Input.GetButtonDown ("Attack1");
bool isPerformingAttack2 = Input.GetButtonDown ("Attack2");
float horizontalInput = Input.GetAxis ("Horizontal");
float verticalInput = Input.GetAxis ("Vertical");
if (isControlable) {
CheckFacing (horizontalInput);
if (isBlockHeld) {
if (verticalInput < 0) {
playerAnimator.SetTrigger ("LowBlockPushed");
} else {
playerAnimator.SetTrigger ("HighBlockPushed");
}
}
if(isBlockReleased)
{
playerAnimator.SetTrigger ("BlockReleased");
}
if(isPerformingAttack1)
{
playerAnimator.SetTrigger ("Attack1");
}
if(isPerformingAttack2)
{
playerAnimator.SetTrigger ("Attack2");
}
if (isGrounded && canMove && horizontalInput != 0) {
Walk (horizontalInput);
}
if (isGrounded && canMove && verticalInput > jumpInputSensitivityThreshold) {
Jump (verticalInput);
}
if (!isGrounded && canMove) {
AirControl (horizontalInput, verticalInput);
}
}
}
private void CheckFacing (float horizontalInput)
{
if (horizontalInput > 0 && !facingRight) {
Flip ();
} else
if (horizontalInput < 0 && facingRight) {
Flip ();
}
}
private void CheckIfGrounded ()
{
isGrounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
playerAnimator.SetBool ("isGrounded", isGrounded);
}
private void CheckHorizontalMovement ()
{
playerAnimator.SetFloat ("HorizontalVelocity", Mathf.Abs (rigidbody2D.velocity.x));
}
private void CheckVerticalMovement ()
{
playerAnimator.SetFloat ("VerticalVelocity", rigidbody2D.velocity.y);
}
private void AirControl (float horizontalInput, float verticalInput)
{
rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x + horizontalInput * horizontalAirControl, rigidbody2D.velocity.y + verticalInput * verticalAirControl);
}
private void Jump (float verticalInput)
{
rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x, verticalInput * jumpForce);
}
private void Walk (float input)
{
rigidbody2D.velocity = new Vector2 (input * maxWalkingSpeed, rigidbody2D.velocity.y);
}
private void Flip ()
{
facingRight = !facingRight;
Vector3 newScale = transform.localScale;
newScale.x *= -1;
transform.localScale = newScale;
}
public void setPlayerNotControllable ()
{
isControlable = false;
}
public void setPlayerControllable ()
{
isControlable = true;
}
public void setPlayerCanMove ()
{
canMove = true;
}
public void setPlayerCanNotMove ()
{
canMove = false;
}
In the editor I set the jumpForce to 50 and the characters rigidbody properties gravity scale to 10 and mass to 80 for a snappy jump.
The walking and jumping seems okay enough, with one issue. The first jump made by the character is good, of proper height. But if I keep the jump button pressed or hit it immediately after the character falls to the ground after previous jump, it jumps way too high, basically shoots upward like a rocket. If I let it land and wait a bit and then jump again, the jump is properly high. Any ideas what might be causing this? How can I fix this?
If any further information is needed, please leave me a comment.