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;publicclassPlayerController : MonoBehaviour{//create custom data-typepublicenumHeroState { idle = 0, walk = 1, jump = 2 } //custom data-type publicHeroState currentHeroState; //variable to keep track of current animation stateprivate bool facingRight =true; // initialize when declare (first way), false by defaultprivateAnimator animator; //obj-reference variable to access the animator component on this gameObjectprivateRigidbody2D rb2d; //rigidbody componentpublicfloat forceX; //may need adjusted publicfloat jumpForce =8.0f; //may need adjusted public bool grounded;publicTransform groundCheck;publicLayerMask groundLayer;publicfloat groundCheckRadius =0.2f; //needs changed// Start is called before the first frame updateprivatevoidStart() { 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 sceneanimator.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 incrementsvoidFixedUpdate() {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=newVector2(0,rb2d.velocity.y); //zero out velocity in x, yrb2d.AddForce(newVector2(forceX * inputX ,0 )); }else {animator.SetInteger("HeroState", (int)HeroState.idle); }//in all cases, jump logic overrides idle and walk statesif (jumpPressed && grounded) {animator.SetInteger("HeroState", (int)HeroState.jump);rb2d.velocity=newVector2(rb2d.velocity.x,0);rb2d.AddForce(newVector2(0, jumpForce),ForceMode2D.Impulse); } }privatevoidFlip( ) { facingRight =!facingRight; //toggle the value true / falseVector3 theScale =transform.localScale;theScale.x*=-1; //mulitply by negative 1 to flip the spritetransform.localScale= theScale; } //end Flip()} //end class PlayerController