两军对垒第一版(附带玩家控制坦克射击)

军队

效果展示

克隆

克隆脚本1——克隆士兵

首先要建立士兵的预制体,该脚本用于预制体的克隆

using System.Collections;
using UnityEngine;

public class CloneTest1 : MonoBehaviour
{
    public GameObject cloneObj;//被克隆的物体
    private float xPos = 0f;//x坐标
    private float zPos = 0f;//z坐标
    /*    public int rows = 10; // 行数
        public int columns = 10; // 列数
        public float spacing = 1.5f; // 士兵之间的间隔*/

    private void Start()
    {
        if(cloneObj==null)
        {
            Debug.LogError("请将制作好的预制体拖入参数栏");
            
        }
        //启动协程
        StartCoroutine(TodoSomeThingByTime());
        //克隆代码
        /*Instantiate(cloneObj,new(0f,0f,0f),Quaternion.identity);
        Instantiate(cloneObj,new(1.5f,0f,0f),Quaternion.identity);
        Instantiate(cloneObj, new(0f, 0f, 1.5f), Quaternion.identity);*/
        /* for (int row = 0; row < rows; row++)
         {
             for (int col = 0; col < columns; col++)
             {
                 // 计算每个士兵的位置
                 Vector3 position = new Vector3(col * spacing, 0, row * spacing) + transform.position;
                 // 实例化预制体
                 Instantiate(cloneObj, position, Quaternion.identity, transform);
             }
         }*/
        IEnumerator TodoSomeThingByTime()
        {
            for (int i = 1; i <= 40; i++)
            {
                //克隆代码
                Instantiate(cloneObj, new Vector3(xPos, 0f, zPos), Quaternion.identity);
                xPos += 1.5f;
                if (i % 5 == 0)
                {
                    zPos += 1.5f;
                    xPos = 0;
                }
               //等待0.2秒,继续运行(携程)
                yield return new WaitForSeconds(0.2f);
            }
        }

    }

}


克隆脚本2——克隆老虎

建立老虎的预制体,该脚本用于预制体的克隆

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

public class CloneTest2 : MonoBehaviour
{
    public GameObject cloneObj2;//被克隆的物体
    [SerializeField] private int rows; // 行数
    [SerializeField] private int columns ; // 列数
    [SerializeField] private float spacing = 1.5f; // 老虎之间的间隔

    private void Start()
    {
        if(cloneObj2==null)
        {
            Debug.LogError("请将制作好的预制体拖入参数栏");
            
        }
        //启动协程
        StartCoroutine(TodoSomeThingByTime());
        //克隆代码
  /*      Instantiate(cloneObj,new(0f,0f,0f),Quaternion.identity);
        Instantiate(cloneObj,new(1.5f,0f,0f),Quaternion.identity);
        Instantiate(cloneObj, new(0f, 0f, 1.5f), Quaternion.identity);*/

        IEnumerator TodoSomeThingByTime()
        {
            for (int row = 1; row < rows; row++)
            {
                for (int col = 0; col < columns; col++)
                {
                   
                    // 计算每个老虎的位置
                    Vector3 position = new Vector3(col * spacing, 0, row * spacing) + cloneObj2.transform.position;
                    // 实例化预制体
                    Instantiate(cloneObj2, position, Quaternion.Euler(0, 180, 0), transform);
                    // transform.Rotate(0, 180, 0);
                    //等待两秒,继续运行(携程)
                    yield return new WaitForSeconds(0.2f);
                }
            }

        }

    }
    private void Update()
    {
        
    }
}


用到了协程,实现让克隆体一个一个克隆出来的效果,间隔为0.2秒

 在yield return new WaitForSeconds(0.2f);中设定

克隆脚本2同上


using UnityEngine;

public class Move : MonoBehaviour
{
    float min = 0.5f;
    float max = 5.0f;
    float randomMoveSpeed;
    void Start()
    {
        randomMoveSpeed = Random.Range(min, max);
    }
    void Update()
    {
       transform.Translate(Vector3.forward * randomMoveSpeed * Time.deltaTime);
    }
    //只在游戏开始前运行一次

}

随机速度

每个克隆体的速度不一样,初始随机分配一个随机速度


using UnityEngine;

public class Move : MonoBehaviour
{
    float min = 0.5f;
    float max = 5.0f;
    float randomMoveSpeed;
    void Start()
    {
        randomMoveSpeed = Random.Range(min, max);
    }
    void Update()
    {
       transform.Translate(Vector3.forward * randomMoveSpeed * Time.deltaTime);
    }
    //只在游戏开始前运行一次

}

碰撞脚本

给老虎设置标签,分类一下

把碰撞脚本拖入士兵的预制体里

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

public class Soldier : MonoBehaviour
{
    private void OnTriggerEnter(Collider other)
    {
        // 检查碰撞对象
        if (other.gameObject.CompareTag("Tiger"))
        {
            // 隐藏当前士兵
            gameObject.SetActive(false);
            // 销毁当前士兵
            Destroy(gameObject);

            // 隐藏碰撞的老虎
            other.gameObject.SetActive(false);
            // 销毁碰撞的老虎
            Destroy(other.gameObject);
        }
    }
    private void OnTriggerStay(Collider other)
    {
        // 检查碰撞对象
        if (other.gameObject.CompareTag("Tiger"))
        {
            // 隐藏当前士兵
            gameObject.SetActive(false);
            // 销毁当前士兵
            Destroy(gameObject);

            // 隐藏碰撞的老虎
            other.gameObject.SetActive(false);
            // 销毁碰撞的老虎
            Destroy(other.gameObject);
        }
    }
}

游戏管理

时间管理+胜负管理

时间管理用协程,30秒时游戏暂停

胜负管理用到了标签查找,最后进行数值判断,进行相应的UI界面弹出

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using static UnityEditor.Experimental.AssetDatabaseExperimental.AssetDatabaseCounters;

public class GameManager : MonoBehaviour
{
    private string tagToFindT = "Tiger";
    private string tagToFindS = "Soldier";
    [SerializeField]private GameObject gameOverPanelTigerWin;
    [SerializeField]private GameObject gameOverPanelSoldierWin;

    private void Start()
    {
        // 游戏开始时启动定时器
        StartCoroutine(GameTimer(30));
    }

    IEnumerator GameTimer(float time)
    {
        // 等待游戏时间结束
        yield return new WaitForSeconds(time);
        // 暂停游戏时间
        Time.timeScale = 0;
        // 时间到达,判定胜负
        EndGame();
    
    }

    void EndGame()
    {
        //GameObject myObject = GameObject.FindWithTag("Tiger");
        GameObject[] objectsWithTigerTag = GameObject.FindGameObjectsWithTag(tagToFindT);
        GameObject[] objectsWithSoldierTag = GameObject.FindGameObjectsWithTag(tagToFindS);
        if (objectsWithTigerTag != null)
        {
            // 找到了对象
            // objectsWithTag数组的长度就是带有指定标签的对象的个数
            int countT = objectsWithTigerTag.Length;
            int countS = objectsWithSoldierTag.Length;

            // 打印出找到的对象个数
            Debug.Log("Found " + countT + " objects with Tigertag " + tagToFindT);
            Debug.Log("Found " + countS + " objectsWithSoldierTag " + tagToFindS);
            // 根据剩余数显示游戏结束面板
            if (countT > countS)
                gameOverPanelTigerWin.SetActive(true);
            if (countT < countS)
                gameOverPanelSoldierWin.SetActive(true);
            

        }
        else
        {
            // 没有找到带有指定标签的对象
            Debug.Log(0);
        }

        // 在这里添加更多游戏结束的逻辑,比如显示胜负界面、重置游戏状态、返回主菜单等。
    }
}

 相机入场动画

添加旧的动画组件

然后把动画掉调出来

注意一定要选中要编辑的摄像机

添加动画,添加属性,并设置达到想要的效果

UI界面

开始界面(不同场景跳转)

添加场景,加入跳转代码

把它绑到空物体中执行

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

public class UI : MonoBehaviour
{
    public void Jump()
    {
        SceneManager.LoadScene(1);
    }
}

一个丑丑的界面be like

建立一个按钮,把脚本挂到鼠标点击事件里

结算界面

要把他们都隐藏,谁胜利了显示谁

两个很丑的界面be like

坦克

玩家移动脚本

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

public class NewBehaviourScript : MonoBehaviour
{
    
    void Update()
    {
        //水平轴
        float horizontal = Input.GetAxis("Horizontal");
        //垂直轴
        float vertical = Input.GetAxis("Vertical");
        //向量
        Vector3 dir = new Vector3(horizontal, 0, vertical);
        //朝向量方向移动
        transform.Translate(dir * 8 * Time.deltaTime);
       
    }
}

子弹

子弹生成

每单击鼠标左键一下,生成一个子弹

需要找到炮口的位置(fire.position)

把炮口挂进去(跟挂物体一样的)

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

public class TankMove : MonoBehaviour
{
    [SerializeField] GameObject bullet;
    [SerializeField] Transform fire;


    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Instantiate(bullet, fire.position, transform.rotation);
        }
    }
}

拖尾组件

挂在子弹预制体上,会产生一个拖尾效果

子弹速度设置

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

public class BulletMove : MonoBehaviour
{
    void Update()
    {
        transform.position += transform.forward * Time.deltaTime * 6;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值