using UnityEngine;
public class OyuncuHareketi : MonoBehaviour
{
public float hareketHizi = 5.0f;
public float ziplamaGucu = 8.0f;
public float yuvarlanmaGucu = 2.0f;
public Transform zeminKontrolNoktasi;
public LayerMask zeminLayer;
private Rigidbody2D rb;
private float zeminCapi = 0.3f;
bool doubleJump;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
//Zıplamıyorsa double jump yapabilmeyi engeller.
if (IsGrounded() && !Input.GetButton("Jump"))
{
doubleJump = false;
}
if (Input.GetButtonDown("Jump"))
{
//yerdeyse veya double jumptaysa zıplama işlemi gerçekleşir.
if (IsGrounded() || doubleJump)
{
rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
//eğer ilk defa zıpladıysa double jump aktif olur. eğer zaten 1 kere zıpladıysa, tekrar zıplarsa double jump false olur.
doubleJump = !doubleJump;
}
}
if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
}
// Sol ve sağ hareket.
float yatayHareket = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(yatayHareket * hareketHizi, rb.velocity.y);
// dönme.
if (yatayHareket < 0)
{
transform.localScale = new Vector3(-1, 1, 1);
}
else if (yatayHareket > 0)
{
transform.localScale = new Vector3(1, 1, 1);
}
if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.D))
{
rb.drag = 0; // Sürtünmeyi sıfırla, böylece yuvarlanma etkisi oluşur.
}
if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.D))
{
rb.drag = 3; // Sürtünmeyi geri yükle.
}
}
//Zeminde olup olmadığını kontrol edip ona göre değer gönderir.
private bool zemindeMi()
{
return Physics2D.OverlapCircle(zeminKontrolNoktasi.position, 0.2f, zeminCapi);
}
}