2021-07-19 19:27:02 +02:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.Events;
|
|
|
|
|
|
|
|
public class CharacterController2D : MonoBehaviour
|
|
|
|
{
|
2021-07-23 09:45:12 +02:00
|
|
|
public float m_JumpVelocity = 20f;
|
2021-07-21 09:33:24 +02:00
|
|
|
public AudioSource SFX;
|
|
|
|
public AudioClip JumpSFX;
|
2021-07-23 09:45:12 +02:00
|
|
|
public LayerMask GroundLayer;
|
|
|
|
public Transform GroundCheckElement;
|
|
|
|
public float GroundCheckRadius = 0.2f;
|
|
|
|
bool OnGround;
|
|
|
|
public Rigidbody2D rb;
|
2021-07-19 19:27:02 +02:00
|
|
|
|
2021-07-23 09:45:12 +02:00
|
|
|
bool wasOnGround = false;
|
2021-07-19 19:27:02 +02:00
|
|
|
|
2021-07-23 09:45:12 +02:00
|
|
|
bool CanJump = false;
|
2021-07-19 19:27:02 +02:00
|
|
|
|
|
|
|
private void FixedUpdate()
|
|
|
|
{
|
2021-07-23 09:45:12 +02:00
|
|
|
wasOnGround = OnGround;
|
|
|
|
if (Physics2D.CircleCast(GroundCheckElement.position, GroundCheckRadius, Vector2.zero, 0f, GroundLayer))
|
2021-07-19 19:27:02 +02:00
|
|
|
{
|
2021-07-23 09:45:12 +02:00
|
|
|
OnGround = true;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
OnGround = false;
|
|
|
|
}
|
|
|
|
if (wasOnGround != OnGround && OnGround == true){
|
|
|
|
CanJump = true;
|
2021-07-19 19:27:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-20 18:52:15 +02:00
|
|
|
public void Move(float move, bool jump)
|
2021-07-19 19:27:02 +02:00
|
|
|
{
|
|
|
|
|
2021-07-23 09:45:12 +02:00
|
|
|
Vector3 targetVelocity = rb.velocity;
|
|
|
|
targetVelocity[0] = move;
|
|
|
|
rb.velocity = targetVelocity;
|
2021-07-19 19:27:02 +02:00
|
|
|
|
2021-07-23 09:45:12 +02:00
|
|
|
if (OnGround && jump && CanJump)
|
2021-07-19 19:27:02 +02:00
|
|
|
{
|
2021-07-23 09:45:12 +02:00
|
|
|
OnGround = false;
|
|
|
|
targetVelocity = rb.velocity;
|
|
|
|
targetVelocity[1] = m_JumpVelocity;
|
|
|
|
rb.velocity = targetVelocity;
|
2021-07-21 09:33:24 +02:00
|
|
|
SFX.clip = JumpSFX;
|
|
|
|
SFX.Play();
|
2021-07-19 19:27:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|