Ahmet Efe11
Femtopat
- Katılım
- 18 Ağustos 2023
- Mesajlar
- 29
Daha fazla
- Cinsiyet
- Erkek
Unity ile bir oun geliştirirken karakterin hasar almasını nasıl sağlayacağımı bulamadım. Kodlar şu şekilde.
Kod:
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class characterhealth : MonoBehaviour
{
// health
public int Maxhealth = 1;
public int currenthealth;
public Healthbar healtbar;
//Enemy AttackTime
public bool enemyattack;
public float enemytimer;
void Start()
{
currenthealth = Maxhealth;
enemytimer = 1.5f;
}
// düşmanın vurma arlığı
void EnemyAttackTimer()
{
if (enemyattack == false)
{
enemytimer -= Time.deltaTime;
}
if (enemytimer < 0)
{
enemytimer = 0f;
}
if (enemytimer == 0f)
{
enemyattack = true;
enemytimer = 1.5f;
}
}
// düşmana vurduğumuzda kitlenmesi
public void characterdamage()
{
if (Input.GetKeyDown(KeyCode.E))
{
enemyattack = false;
}
}
//karakterin hasar alması
public void Takedamage(int damage)
{
if (enemyattack == true)
{
currenthealth -= 20;
enemyattack = false;
}
healtbar.SetHealth(currenthealth);
}
// Update is called once per frame
void Update()
{
EnemyAttackTimer();
characterdamage();
if (Input.GetKeyDown(KeyCode.Z))
{
Takedamage(20);
}
}
}
bu ve
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Healthbar : MonoBehaviour
{
public Slider slider;
public void SetMaxHealth(int health)
{
slider.maxValue = health;
slider.value = health;
}
public void SetHealth(int health)
{
slider.value = health;
}
}
Son düzenleyen: Moderatör: