Created
December 20, 2019 04:00
-
-
Save zkmark/3cd8e4ae438498912ba3a0cddc7da99b to your computer and use it in GitHub Desktop.
Unity PlayerController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class PlayerController : MonoBehaviour { | |
public float speed; | |
public float jumpForce; | |
private float moveInput; | |
private Rigidbody2D rb; | |
private bool facingRight = true; //mirada | |
//Jump | |
private bool isGrounded; | |
public Transform groundCheck; | |
public float checkRadius; | |
public LayerMask whatIsGround; | |
//More jumps | |
private int extraJumps; | |
public int extraJumpsValue; | |
// | |
public Collider2D groundCollider; | |
// Start is called before the first frame update | |
void Start() { | |
extraJumps = extraJumpsValue; | |
rb = GetComponent<Rigidbody2D>(); | |
} | |
private void FixedUpdate() { | |
//Detect Ground | |
isGrounded = Physics2D.OverlapCircle( | |
groundCheck.position, | |
checkRadius, | |
whatIsGround | |
); | |
//isGrounded = groundCollider.IsTouchingLayers(whatIsGround); | |
//Move | |
moveInput = Input.GetAxis("Horizontal"); | |
//print(moveInput); | |
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y); | |
if (facingRight == false && moveInput > 0){ | |
Flip(); | |
} | |
else if( facingRight == true && moveInput < 0 ){ | |
Flip(); | |
} | |
//other | |
//Debug.Log(groundCollider.IsTouchingLayers(whatIsGround)); | |
} | |
// Update is called once per frame | |
void Update() { | |
if (isGrounded == true) { | |
extraJumps = extraJumpsValue; | |
} | |
if (Input.GetButtonDown("Jump") && extraJumps > 0 ) { | |
rb.velocity = Vector2.up * jumpForce; | |
extraJumps--; | |
} | |
else if (Input.GetButtonDown("Jump") && extraJumps == 0 && isGrounded == true ) { | |
rb.velocity = Vector2.up * jumpForce; | |
} | |
} | |
//https://youtu.be/QGDeafTx5ug?list=PLETTKWteqrbbkTU4mAgpfaxrxJuT5iVS7 | |
void Flip(){ | |
facingRight = !facingRight; | |
Vector3 Scaler = transform.localScale; | |
Scaler.x *= -1; | |
transform.localScale = Scaler; | |
} | |
//Radius for detect ground of OverlapCircle | |
//https://youtu.be/-T-wq3JQSKc?list=PLETTKWteqrbbkTU4mAgpfaxrxJuT5iVS7 | |
private void OnDrawGizmosSelected() { | |
Gizmos.color = Color.red; | |
Gizmos.DrawWireSphere(groundCheck.position, checkRadius); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment