63 lines
2 KiB
C#
63 lines
2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class PlayerMovement : MonoBehaviour
|
|
{
|
|
public float speed, jumpForce;
|
|
Rigidbody2D rb;
|
|
public Transform feet;
|
|
public float range;
|
|
public LayerMask whatIsGround;
|
|
public float coyoteTime;
|
|
Vector2 length;
|
|
private float lastTimeTouchingGround;
|
|
private float lastJumpTime;
|
|
public ParticleSystem jumpParticles;
|
|
[HideInInspector]
|
|
public bool canMove = true;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
length = transform.GetChild(0).GetComponent<SpriteRenderer>().bounds.size / 2;
|
|
rb = GetComponent<Rigidbody2D>();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if(Physics2D.OverlapCircle(feet.position, range, whatIsGround)) {
|
|
lastTimeTouchingGround = Time.time;
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.Space) && Time.time-coyoteTime <= lastTimeTouchingGround & Time.time>=lastJumpTime+coyoteTime)
|
|
{
|
|
AudioManager.instance.Play("Jump");
|
|
lastJumpTime = Time.time;
|
|
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
|
|
}
|
|
if (Input.GetButton("Horizontal") && canMove)
|
|
{
|
|
transform.rotation = Quaternion.Euler(0, Input.GetAxisRaw("Horizontal") == 1 ? 0 : 180, 0);
|
|
}
|
|
if (!Physics2D.Raycast(transform.position, Input.GetAxisRaw("Horizontal") * Vector2.right, length.x + 0.01f, whatIsGround)) {
|
|
rb.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * speed, rb.velocity.y);
|
|
}
|
|
|
|
}
|
|
|
|
void OnCollisionEnter2D(Collision2D col){
|
|
if(col.gameObject.tag == "MovingPlatform"){
|
|
transform.parent = col.transform;
|
|
}
|
|
if(col.gameObject.tag == "InsideTileKill"){
|
|
GetComponent<PlayerStats>().Die();
|
|
}
|
|
}
|
|
|
|
void OnCollisionExit2D(Collision2D col){
|
|
if (col.gameObject.tag == "MovingPlatform"){
|
|
transform.parent = null;
|
|
}
|
|
}
|
|
}
|