Code

This section will cover the code that is present in this chapter:

PlayerController.cs

Here's the code:

using UnityEngine;
using System.Collections;

publicclass PlayerController : MonoBehaviour {

  publicfloat jumpForce = 6f;
  publicfloat runningSpeed = 1.5f;
  privateRigidbody2D rigidBody;
  public Animator animator;

  void Awake() {
    rigidBody = GetComponent<Rigidbody2D>();
  }

  void Start() {
    animator.SetBool("isAlive", true);
  }


  // Update is called once per frame
  void Update () {

    if (Input.GetMouseButtonDown(0)) {
      Jump();
    }

    animator.SetBool("isGrounded", IsGrounded());
  }


  void FixedUpdate() {

    if (rigidBody.velocity.x < runningSpeed) {
      rigidBody.velocity = newVector2(runningSpeed, rigidBody.velocity.y);
    }
  }



  void Jump() {
    if (IsGrounded()) {
      rigidBody.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
    }
  }

  publicLayerMask groundLayer;

  bool IsGrounded() {

    if (Physics2D.Raycast(this.transform.position, Vector2.down, 0.2f, groundLayer.value)) {
      returntrue;
    }
    else {
      returnfalse;
    }
  }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset