using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float speed;
public float jumpForce;
public float moveInput;
public Rigidbody2D rb;
private bool facingRight = true;
private bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
private bool canJump;
private bool canWallJump;
public float wallJumpForce;
public bool isOnWall;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (isGrounded)
{
canJump = true;
canWallJump = true;
} else
{
canJump = false;
}
if (Input.GetKeyDown(KeyCode.Space) && canJump)
{
rb.velocity = Vector2.up * jumpForce;
canJump = false;
} else if (Input.GetKeyDown(KeyCode.Space) && canWallJump && isOnWall)
{
rb.velocity = Vector2.up * jumpForce;
canWallJump = false;
}
}
private void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
moveInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (facingRight == false && moveInput > 0)
{
Flip();
} else if (facingRight && moveInput < 0)
{
Flip();
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.layer == LayerMask.NameToLayer("Wall"))
{
canWallJump = true;
isOnWall = true;
print("Can Wall Jump");
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.layer == LayerMask.NameToLayer("Wall"))
{
print("Wall jumped/Cant wall jump");
canWallJump = false;
isOnWall = false;
}
}
}