噩梦系列篇之Playe攻击敌人与敌人声音播放完成

本博客详细介绍了在游戏开发中如何通过脚本实现敌人受到攻击后的伤害计算、死亡声音播放、粒子效果显示及敌人状态变化的过程,具体包括使用enemyhealth脚本进行伤害处理和敌人死亡逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

现在敌人的collider和rigidbody都有了,说明可以发生碰撞了,下面就用player攻击敌人。

首先给敌人加一个脚本;enemyhealth();

<pre name="code" class="csharp">using UnityEngine;
using System.Collections;

public class enemyhealth : MonoBehaviour {

    private float hp = 100;

    private Animator ani;

    private NavMeshAgent nav;

    private enemymove enemymo;

    public  AudioClip deathclip;//定义死亡声音

    public ParticleSystem particlesystem;

    private CapsuleCollider cclolider;
    
	void Start () {

        ani = this.GetComponent<Animator>();
        nav = this.GetComponent<NavMeshAgent>();
        enemymo = this.GetComponent<enemymove>();
        cclolider = this.GetComponent<CapsuleCollider>();
        particlesystem=this.GetComponentInChildren<ParticleSystem>();

	}
	
	
	void Update () {

        //if (this.hp <= 0)//实现死亡之后下移然后消失
        //{
          
        //    transform.Translate(Vector3.down*Time.deltaTime*0.5f);//死后下移
        //    if(transform.position .y<=-10)//当下移动到3时就销毁敌人
        //    {
           // Destroy(this.gameObject, 2f);
               
        //    }
        //}

	}

    public void enemydamage(float damage,Vector3 hitposition)//hitposition 是指射击效果的位置
    {
       
        hp -= damage;
        audio.Play();

        particlesystem.transform.position = hitposition;//指射击粒子效果的位置
        particlesystem.Play();

        if (hp <= 0)
        {
            print("siwang");
            Death();
        }
    }

 
        
    void Death()
    {
        enemymo.enabled = false;

        nav.enabled = false;

        ani.SetBool("Death",true);

        AudioSource.PlayClipAtPoint(deathclip,transform.position,0.5f);//死亡声音播放(使用的静态)

        Destroy(this.gameObject, 2f);
    }
}


上面代码里的 public void enemydamage(float damage,Vector3 hitposition)是由player的gunshoot()脚本传递过来的(如下图)。只要在激光的位置加入判断碰撞的代码就可以(attack,hitinfo.point)分别传递了敌人enemyhealth()脚本里的(float
 damage,Vector3 hitposition)这两个值。。。这样就可以实现攻击敌人到死亡的功能了。



敌人受伤和死亡都需要播放声音,只能定义一个死亡声音(脚本里的 public  AudioClip deathclip;//定义死亡声音),另个用unity本来的audio source,如下图;


攻击敌人会有受伤的粒子效果:

如enemyhealth()代码的  particlesystem.transform.position = hitposition;//指射击粒子效果的位置。。。。。 particlesystem.Play();这个是播放粒子。。。。




                 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值