I'm new to coding and to Unity2D, so please bear with me.
I've created a Character Controller, which is called via EventTriggers set against UI button objects. The controller works fine (though is probably overkill), but below are the challenges around the jump function I'm having. Any help on this is really appreciated.
Thanks so much!
Challenges
- Player jumps in the right direction even from a standstill.
- Player can keep jumping even when in the air.
Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour
{
Rigidbody2D rb2d;
SpriteRenderer spriteRenderer;
Animator animator;
public Transform groundCheck;
private LayerMask groundLayer = 1 << LayerMask.NameToLayer ("groundLayer");
private bool moveLeft;
private bool moveRight;
private bool moveJump;
private bool isGrounded;
private float horizontalMove;
private float verticalMove;
public float playerSpeed = 0f;
public float jumpForce = 0f;
public float groundCheckRadius;
public void Start()
{
rb2d = GetComponent<Rigidbody2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
animator = GetComponent<Animator>();
moveLeft = false;
moveRight = false;
moveJump = false;
}
//Function - Left button pressed
public void MoveLeft()
{
moveLeft = true;
Debug.Log("Function - Move Left");
}
//Function - Right button pressed
public void MoveRight()
{
moveRight = true;
Debug.Log("Function - Move Right");
}
//Function - Jump button pressed
public void MoveJump()
{
moveJump = true;
Debug.Log("Function - Move Jump");
}
//Function - Stop player movement
public void MoveStop()
{
rb2d.velocity = Vector3.zero;
animator.Play("PlayerIdle");
moveLeft = false;
moveRight = false;
moveJump = false;
}
//Function - Check if player 'GroundCheck' is touching the ground layer
private void PlayerGrounded()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
if (isGrounded == true)
{
Debug.Log("Player is touching the ground!");
}
if (isGrounded == false)
{
Debug.Log("Player is not touching the ground!");
}
}
//Function - Plays once every frame
private void Update()
{
PlayerMovement();
PlayerGrounded();
}
//Function - Move the player GameObject
public void PlayerMovement()
{
//Set the player horizontal right axis
if (moveRight)
{
horizontalMove = playerSpeed;
Debug.Log("Function - PlayerMovement - Move Right!");
}
//Set the player horizontal left axis
if (moveLeft)
{
horizontalMove = -playerSpeed;
Debug.Log("Function - PlayerMovement - Move Left!");
}
//Set the player vertical axis
if (moveJump)
{
verticalMove = jumpForce;
Debug.Log("Function - PlayerMovement - Move Jump!");
}
}
//Function - Plays once every loaded frame
private void FixedUpdate()
{
//Move the player right, don't flip, play animation
if (moveRight)
{
rb2d.velocity = new Vector2(horizontalMove, rb2d.velocity.y * playerSpeed * Time.deltaTime);
spriteRenderer.flipX = false;
animator.Play("PlayerRun");
Debug.Log("Function - Player Moving Right!");
}
//Move the player left, don't flip, play animation
if (moveLeft)
{
rb2d.velocity = new Vector2(horizontalMove, rb2d.velocity.y * playerSpeed * Time.deltaTime);
spriteRenderer.flipX = true;
animator.Play("PlayerRun");
Debug.Log("Function - Player Moving Left!");
}
//Move the player into the air via a jump
if (moveJump && (rb2d.velocity.y == 0))
{
rb2d.velocity = new Vector2(verticalMove, rb2d.velocity.x * jumpForce);
animator.Play("PlayerJump");
Debug.Log("Function - Player Moving Up!");
}
}
}