> For the complete documentation index, see [llms.txt](https://kdoore.gitbook.io/cs2335/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kdoore.gitbook.io/cs2335/cs2335_f20/getting-started/playercontrollerstep1/playercontroller-v2-s20.md).

# PlayerController V2 S20

PlayerController V2 Feb 2020

GroundCheck GameObject must be configured as child of player Physics Layer: Ground must be configured

```java
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public enum HeroState {idle, walk, jump }    //CONSTANT:  Custom data-type - integer using words

    public HeroState currentHeroState; //instance of a variable using our custom data-type

    private Animator animator;  //object reference variable - null, null reference exception - initialization error
    public bool facingRight;   //default is that this is false, facing left
    private Rigidbody2D myRBody2D;

    public float forceX; //stores force for movement in horizontal direction

    public float jumpForce = 10f;
    public Transform groundCheck;  //transform component on the GroundCheck gameObject
    public LayerMask groundLayer; //Layer that the groundCheck is checking for contact for jumping
    public float groundCheckRadius = 0.5f; //modify as needed
    public bool grounded;  //is it grounded

    // Start is called before the first frame update
    void Start()
    {
        currentHeroState = HeroState.idle;  //we can see the current value in inspector
        animator = GetComponent<Animator>(); //initialize by connection to gameObject component
        animator.SetInteger("HeroState", (int)HeroState.idle); //misspelling will give run-time error in Unity
        facingRight = true;
        myRBody2D = GetComponent< Rigidbody2D >();
        forceX = 100.0f;
        groundCheckRadius = 0.5f;
    }

    // FixedUpdate is called with constant-time between each execution
    void FixedUpdate()
    {
        float inputX = Input.GetAxis("Horizontal"); //-1, 0, 1 values for L,R arrows, a, d
        bool isWalking = Mathf.Abs(inputX) > 0;  //left or right walking movement

        if (isWalking)
        {
            animator.SetInteger("HeroState", (int)HeroState.walk); //send signal to animator
            currentHeroState = HeroState.walk;

            if ( facingRight && inputX < 0) //facing right, moving left
            {
                Flip(); //flip to make facing left
            }
            if( !facingRight && inputX > 0) //facing left, moving right
            {
                Flip(); //flip to set facingRight
            }
            ////ADD MOVEMENT LOGIC - HORIZONTAL
            myRBody2D.velocity = new Vector2(0, myRBody2D.velocity.y);  //set velocity to 0 x, keep y velocity
            myRBody2D.AddForce(new Vector2(forceX * inputX, 0));  //
        }
        else 
        { //set back to Idle, if it was walking,jumping in last frame
            animator.SetInteger("HeroState", (int)HeroState.idle);
            currentHeroState = HeroState.idle;
        }

        bool jumpPressed = Input.GetButtonDown("Jump"); //is spacebar pressed
        grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);

        if (jumpPressed  && grounded)
        {
            animator.SetInteger("HeroState", (int)HeroState.jump);
            currentHeroState = HeroState.jump;
            ///ADD VERTICAL MOVEMENT LOGIC
            myRBody2D.velocity = new Vector2(myRBody2D.velocity.x, 0);  //set velocity to 0 y, keep x velocity
            myRBody2D.AddForce(new Vector2(0,jumpForce),ForceMode2D.Impulse);  //
        }

    } //end FixedUpdate

    private void Flip()
    {
        facingRight = !facingRight; //toggle to opposite value 
        //  transform is a variable initialized to refer to the current gameObject's transform component
        //Common pattern when working with vector values in Unity
        Vector3 theScale = transform.localScale; //initialize temp Vector3 with current transform values
        theScale.x *= -1; //multiple current Scale.x by -1
        transform.localScale = theScale;
    }

} //end PlayerController
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://kdoore.gitbook.io/cs2335/cs2335_f20/getting-started/playercontrollerstep1/playercontroller-v2-s20.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
