B站 unity轻松塔防2

那个1之后好像改了点小东西,具体是啥在B站看看,我忘了(

打开文件,发现怪物数量波数还得重新设置...

四.导入炮塔资源,C#炮塔锁定目标

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

public class Turret : MonoBehaviour
{
    public float range = 3;
    string enemyTag = "Enemy";
    Transform target;//攻击目标
    public float rotSpeed = 10;
    void Start()
    {
        InvokeRepeating("UpdateTarget", 0, 0.5f);
    }

    // Update is called once per frame
    void Update()
    {
        if (target == null) return;
        LockTarget();
    }
    private  void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.yellow;
        Gizmos.DrawWireSphere(transform.position , range); 
    }

    private void UpdateTarget()//锁定目标
    {
        GameObject[] enemies = GameObject.FindGameObjectsWithTag(enemyTag);
        float minDistance = Mathf.Infinity;//无穷大
        Transform nearestEnemy = null;
        foreach (var enemy in enemies)
        {
            float distance = Vector3.Distance(enemy.transform.position, transform.position);
            if (distance < minDistance)
            {
                minDistance = distance;
                nearestEnemy = enemy.transform;//最近的敌人
            }//更新
        }
        if (minDistance < range)//在攻击范围 
        {
            target = nearestEnemy;
        }
        else
        {
            target = null;
        }
    }

    private void LockTarget ()//y轴不想动
    {
        Vector3 dir = new Vector3 (target.position.x - transform.position.x,0, target.position.z - transform.position.z);
        transform.rotation = Quaternion.LookRotation(dir);
    }

}

教程开始搁那绕,直接把脚本物体换成炮管不就好了,直接旋转自身就行,而且后面那一堆四元数函数用的我头疼,简化了下需求(何必那么复杂,做差之后把y赋0也可)

五.发射子弹c#(想换成激光的想了想算了)

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

public class Turret : MonoBehaviour
{
    public float range = 3;
    string enemyTag = "Enemy";
    Transform target;//攻击目标
    public float rotSpeed = 10;
    private Transform bulletPoint;//生成子弹位置
    public GameObject bulletPrefab;//子弹预制体

    public float bulletRate= 1f;//子弹的速率
    private float countDown = 0;
    void Start()
    {
        InvokeRepeating("UpdateTarget", 0, 0.5f);
        countDown = 1 / bulletRate ;
    }

    // Update is called once per frame
    void Update()
    {
        if (target == null) return;
        LockTarget();//瞄准
        //倒计时发射子弹
        countDown -= Time.deltaTime;
        if (countDown <= 0) 
        {
            Debug.Log("发射子弹");
            Instantiate(bulletPrefab, transform.position, transform.rotation );
            countDown = 1 / bulletRate;
        }


    }
    private  void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.yellow;
        Gizmos.DrawWireSphere(transform.position , range); 
    }

    private void UpdateTarget()//锁定目标
    {
        GameObject[] enemies = GameObject.FindGameObjectsWithTag(enemyTag);
        float minDistance = Mathf.Infinity;//无穷大
        Transform nearestEnemy = null;
        foreach (var enemy in enemies)
        {
            float distance = Vector3.Distance(enemy.transform.position, transform.position);
            if (distance < minDistance)
            {
                minDistance = distance;
                nearestEnemy = enemy.transform;//最近的敌人
            }//更新
        }
        if (minDistance < range)//在攻击范围 
        {
            target = nearestEnemy;
        }
        else
        {
            target = null;
        }
    }

    private void LockTarget ()//y轴不想动
    {
        Vector3 dir = new Vector3 (target.position.x - transform.position.x,0, target.position.z - transform.position.z);
        transform.rotation = Quaternion.LookRotation(dir);
    }

}

不太想改子弹生成位置...(潜台词,就没改到炮口)

六.子弹移动c#

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

public class Bullet : MonoBehaviour
{
    private Transform m_Target;//目标
    public float speed = 1000;//子弹速度
    public void SetTarget(Transform target)
    {
        m_Target = target;
    }
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (m_Target != null) return;//没目标不反应
        Vector3 dir = m_Target.position - transform.position;
        if (Vector3.Distance(m_Target.position,transform.position) < speed * Time.deltaTime) 
        {
            HitTarget();//击中目标
            return;
        }
        transform.Translate (dir.normalized * speed * Time.deltaTime, Space.World);
        transform.LookAt(m_Target);
    }

    void HitTarget()
    {
        Destroy(gameObject);//销毁自己
        //敌人掉血
    }


}

寄...子弹不动

无大语,写成了有目标不反应...("//没目标不反应"前)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值