mycj_demo/mycj/Assets/Script/bullet.cs
2024-12-02 23:38:32 +08:00

74 lines
1.9 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bullet : MonoBehaviour
{
[Header("飞行的目标点,不需要给,动态创建时给")]
public GameObject tagpos;
[Header("飞行的速度,不需要给,动态创建时给")]
public float speed;
[Header("子弹的伤害,可以给默认是50")]
public float changehp=100;
public Palye user;
public DamageType damageType = DamageType.noAttributeDamage;
// Start is called before the first frame update
void Update()
{
if (tagpos != null)
{
move();
}
else
{
Destroy(gameObject); // 如果目标不存在,销毁子弹
}
}
public void Init(GameObject target, float bulletSpeed,float changehp=50)
{
this.tagpos = target;
this.speed = bulletSpeed;
this.changehp = changehp;
}
void move()
{
// 子弹朝目标移动
Vector3 direction = (tagpos.transform.position - transform.position).normalized;
transform.position += direction * speed * Time.deltaTime;
// 如果子弹非常接近目标,视为命中
if (Vector3.Distance(transform.position, tagpos.transform.position) < 10f)
{
HitTarget();
}
}
// 子弹命中目标
private void HitTarget()
{
// 调用敌人的 changehp 方法
npcInfo enemy = tagpos.GetComponent<npcInfo>();
if (enemy != null)
{
enemy.bloodLoss(new object[] { enemy , changehp, damageType, user });
Debug.Log("击中敌人");
//BUff buff = new BUff();
//buff.timeLeft = 5;
//buff.executionInterval_max = 0.1f;
//buff.Funaction = enemy.poisoning;
//buff.value = new object[] { enemy,user };
//enemy.buffList.Add(buff);
}
// 销毁子弹
Destroy(gameObject);
}
}