unity3d自学日记 第15天 - 3d炸弹人demo效果实现

目录

效果图

 简介

 脚本情况

1.玩家脚本  

 2.敌人脚本

 3.炸弹脚本


 

效果图

 

 简介

        


实现通过键盘控制人物移动以及动画状态机的切换,实现释放炸弹 炸死敌人以及爆炸效果,实现敌人简单ai,发现玩家后自动朝玩家攻击。

 脚本情况

 

 

创建了三个脚本 分别是玩家脚本(用于人物移动以及死亡判断) 敌人脚本(敌人追踪) 炸弹脚本(爆炸以及计分)

1.玩家脚本  

主要是获取 水平轴和垂直轴用于人物移动 以及获取键盘 u建实例化炸弹 设定每隔1秒才能释放

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerControl : MonoBehaviour
{
    //炸弹预制体
    public GameObject Bombpre;
    //时间
    public float CD = 1;
    //血量
    public float hp = 1;
    //计时器
    private float timer = 0;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //死亡 直接返回
        if (hp <= 0) {
            return;
        }

        //移动
        //水平
        float horizontal = Input.GetAxis("Horizontal");
        //垂直
        float vertical = Input.GetAxis("Vertical");
        //获取向量
        Vector3 dir = new Vector3(horizontal, 0, vertical);
        //向量 不为0证明在运动 没有安键的情况下 默认返回0
        if (dir != Vector3.zero)
        {
            //角色朝向向量方向
            transform.rotation = Quaternion.LookRotation(dir);
            //朝向量方向移动
            transform.position += dir * 2 * Time.deltaTime;
            //切换动画
            GetComponent<Animator>().SetBool("isrun", true);

        }
        else {
            GetComponent<Animator>().SetBool("isrun", false);
        }
        //计时器计时
        timer += Time.deltaTime;

        //获取键盘U按下 
        if (Input.GetKeyDown(KeyCode.U) && timer >=CD) {
            timer = 0;
            //生成炸弹实例  
            Instantiate(Bombpre, transform.position, transform.rotation);
        
        }
        
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.collider.tag == "Enemy") {
            hp = 0;
            GetComponent<Animator>().SetBool("die", true);
        }
    }

    public  void die() {

        GetComponent<Animator>().SetBool("die", true);

    }
}

 2.敌人脚本

检测和玩家是否距离3米之内,主要用到Vector3.Distance方法 判断2个向量之间的距离,如果小于3米则朝玩家跑去

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class enemyControl : MonoBehaviour
{

    private playerControl player;
    // Start is called before the first frame update
    void Start()
    {
        //拿到玩家
        player = GameObject.FindWithTag("Player").GetComponent<playerControl>();

    }

    // Update is called once per frame
    void Update()
    {
        //获取玩家和敌人的距离
        float dis = Vector3.Distance(player.transform.position, transform.position);

        if (dis < 3 && player.hp > 0)
        {
            //看向玩家
            transform.LookAt(player.transform);

            //朝前方走去
            transform.Translate(Vector3.forward * 2 * Time.deltaTime);

            GetComponent<Animator>().SetBool("isrun", true);
        }
        else {
            GetComponent<Animator>().SetBool("isrun", false);
        }
    }

    public void gethit() {

        Destroy(gameObject);
    }
}

 3.炸弹脚本

定时爆炸及释放爆炸效果 销毁自身 主要用到 Physics.OverlapSphere 方法来检测周围的碰撞体 只有带有碰撞体组件的物体才会被检测到

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class bombControl : MonoBehaviour
{
    public GameObject boomEffect;

    private float timer = 0;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {

        timer += Time.deltaTime;
        if (timer > 2) {
            //实例爆炸
            Instantiate(boomEffect, transform.position, transform.rotation);

            //获取周围的碰撞体 距离2米
            Collider[] collider = Physics.OverlapSphere(transform.position, 2f);

            //遍历碰撞体
            foreach (Collider coll in collider) {

               // GameObject.Find("Player").GetComponent<playerControl>().die();
                if (coll.tag == "Enemy") {
                    coll.GetComponent<enemyControl>().gethit();
                }
            
            }

            Destroy(gameObject);
        }
        
    }
}

 总结 主要学习了获取键盘事件以及 距离检测的常用方法

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值