PlayerController.cs V2-Jump

This version of PlayerController includes code for Animation, and Movement of the player game object and includes logic for physics of constrained jumping

Required GameObjects:

  • Player with Components: Animator, Rigidbody2D, Collider2D, PlayerController.cs

  • GroundCheck: Empty GameObject placed as a child of the Player, transform positioned at the feet of the Player.

  • Physics Layer: Ground

  • Floor Empty GameObject with BoxCollider2D, Layer: Ground

CODE: PlayerController.cs V2 Includes Jump Logic

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    //create custom data-type
    public enum HeroState { idle = 0, walk = 1, jump = 2 } //custom data-type 
    public HeroState currentHeroState;  //variable to keep track of current animation state

    private bool facingRight = true; // initialize when declare (first way),  false by default

    private Animator animator; //obj-reference variable to access the animator component on this gameObject

    private Rigidbody2D rb2d;  //rigidbody component
    public float forceX;  //may need adjusted 
    public float jumpForce = 8.0f;  //may need adjusted 
    public bool grounded;
    public Transform groundCheck;
    public LayerMask groundLayer;
    public float groundCheckRadius = 0.2f; //needs changed


    // Start is called before the first frame update
    private void Start()
    {
        facingRight = true; //final way to initialize, overrides all prior settings
        animator = GetComponent<Animator>(); //<T>  //call a method - make connection to component on gameObject in Unity scene
        animator.SetInteger("HeroState", (int)HeroState.idle);
        rb2d = GetComponent<Rigidbody2D>();  //connect with component
        forceX = 100.0f;
    }

    //Fixed update used for physics, to give smooth motion, called at consistent time increments
    void FixedUpdate()
    {
        float inputX = Input.GetAxis("Horizontal"); // key input:  -1, 0, 1
        bool isWalking = Mathf.Abs(inputX) > 0;

        //check for spacebar or w, up keys to indicate vertical jump
        bool jumpPressed = Input.GetButtonDown("Jump") || Input.GetAxis("Vertical") > 0;
        grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
        if (isWalking)
        {
            animator.SetInteger("HeroState", (int)HeroState.walk);

            if( facingRight && inputX < 0)  //currently facing right, but moving left so flip
            {
                Flip();
            }
            if( !facingRight && inputX > 0) //facing left, moving right so Flip()
            {
                Flip();
            }
            rb2d.velocity = new Vector2(0,rb2d.velocity.y);   //zero out velocity in x, y
            rb2d.AddForce(new Vector2(forceX * inputX ,0 ));
        }
        else
        {
            animator.SetInteger("HeroState", (int)HeroState.idle);
        }

        //in all cases, jump logic overrides idle and walk states
        if (jumpPressed && grounded)
        {
            animator.SetInteger("HeroState", (int)HeroState.jump);
            rb2d.velocity = new Vector2(rb2d.velocity.x, 0);
            rb2d.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
        }

    }

private void Flip(   )
    {
        facingRight = !facingRight; //toggle the value true / false
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;  //mulitply by negative 1 to flip the sprite
        transform.localScale = theScale;
    }  //end Flip()


}  //end class PlayerController

Last updated