前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Unity3D基础项目(四):3D坦克大战

Unity3D基础项目(四):3D坦克大战

作者头像
孙寅
发布2020-06-02 17:04:50
1.6K0
发布2020-06-02 17:04:50
举报
文章被收录于专栏:宜达数字
1、思路

这个游戏最初是国外的教程,后来被国内的众多机构和个人仿写推出众多的版本。但是内容原理差不多,有的是通过插件(PlayMaker,CaverAI),有的通过顶级封装简单几十行代码就搞出来。特别适合初学者总结自己的学习水平。由于教学需要,所以研究一下,仅供参考。

tank.gif

2、导入资源与素材

资源来源于网络,如有侵权,联系我删除

修改环境光,与背景色

环境设置

相机设置

3、设置Tank游戏对象

设置碰撞器添加刚体,让坦克能够在地面行走,旋转

代码语言:javascript
复制
using UnityEngine;
using System.Collections;
// 控制坦克移动

public class Tank_Move : MonoBehaviour {

    private Rigidbody r;
    private float speed = 5f;
    void Start () {
        r = GetComponent<Rigidbody>();
    }
    
    
    void Update () {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        // 控制前后移动
        r.velocity = transform.forward * v * speed;
        // 控制左右旋转
        r.angularVelocity = transform.up * h * angularSpeed;
    }
}
4、设置不同的控制模式

Paste_Image.png

设置一个标志位,来判断坦克是否为角色控制坦克

Paste_Image.png

5、子弹预制与实例化

Paste_Image.png

代码语言:javascript
复制
using UnityEngine;
using System.Collections;
// tank攻击脚本

public class AttackOtherTank : MonoBehaviour {
    // 实例化子弹的位置
    public Transform shellPos;
    // 子弹预制物
    public GameObject shellPrefabs;
    // 子弹身上的刚体
    public Rigidbody shell_rigidbody;
    // 子弹的速度
    public float shellSpeed = 10f;

    void Update () {
        if (Input.GetMouseButtonDown(0))
        {
           GameObject shell = Instantiate(shellPrefabs,shellPos.position,shellPos.rotation) as GameObject;
           shell_rigidbody = shell.GetComponent<Rigidbody>();
           shell_rigidbody.velocity = shell.transform.forward * shellSpeed;
        }
    }
}
6、关于特效的制作:

子弹的特效,就在触发器方法中写,可以通过触发器方法写,当检测到碰撞的时候,拿到碰撞的点,实例化爆炸效果

代码语言:javascript
复制
using UnityEngine;
using System.Collections;
// 子弹预制物体挂载的脚本

public class ShellFly : MonoBehaviour {
    // 子弹特效
    public GameObject shellEffc;

    void OnTriggerEnter(Collider other)
    {
        GameObject go = Instantiate(shellEffc, transform.position, transform.rotation) as GameObject;
        Destroy(go,1.5f);
        Destroy(this.gameObject,1.5f);
        if (other.tag == "Tank")
        {
            other.SendMessage("TankDamege");
        }
    }
}

在攻击脚本中实现Tank减血方法,判断tank血量实例化tank爆炸效果

代码语言:javascript
复制
using UnityEngine;
using System.Collections;
// 子弹攻击脚本

public class AttackOtherTank : MonoBehaviour {
    // 实例化子弹的位置
    public Transform shellPos;
    // 子弹预制物
    public GameObject shellPrefabs;
    // 子弹身上的刚体
    public Rigidbody shell_rigidbody;
    // 子弹的速度
    public float shellSpeed = 10f;

    public int hp = 100;
    public GameObject TankDied;

    
    void Update () {
        if (Input.GetMouseButtonDown(0))
        {
           GameObject shell = Instantiate(shellPrefabs,shellPos.position,shellPos.rotation) as GameObject;
           shell_rigidbody = shell.GetComponent<Rigidbody>();
           shell_rigidbody.velocity = shell.transform.forward * shellSpeed;
        }
    }

    void TankDamege()
    {
        if (hp <= 0) 
        {
            return;
        }
        else
        {
            hp -= Random.Range(10, 20);
        }

        if (hp <= 0)
        {
            // 播放死亡动画效果
            Instantiate(TankDied, transform.position + Vector3.up, transform.rotation);
            Destroy(this.gameObject);
        }
    }
}
7、控制视野操作

差值 = 摄像机 - 两个tank的位置平均值 摄像机位置 = 位置平均值+差值。 也就是说每次的位置平均值不一样。

代码语言:javascript
复制
using UnityEngine;
using System.Collections;

public class FlowerTank : MonoBehaviour {

    public Transform tank1;
    public Transform tank2;
    // 保存摄像机与坦克之间的距离
    private Vector3 offset;
    private Camera CurrentCarema;
    void Start() {
        offset = transform.position - (tank1.position + tank2.position) / 2;
        CurrentCarema = this.GetComponent<Camera>();
    }

    void Update() {
        if (tank1 != null || tank2 != null)
        {
            transform.position = (tank1.position + tank2.position) / 2 + offset;
            float distance = Vector3.Distance(tank1.position, tank2.position);
            float size = distance * 0.8f;
            CurrentCarema.orthographicSize = size;
        }
        else
        {
            return;
        }
    }
}
8、播放tank移动音效以及其他音效
代码语言:javascript
复制
using UnityEngine;
using System.Collections;
// 控制坦克移动

public class Tank_Move : MonoBehaviour {

    private Rigidbody r;
    private float speed = 5f;
    private float angularSpeed = 10f;
    // 判断当前坦克
    public bool isCurrentTank = true;
    public AudioClip idle;
    public AudioClip Run;
    public AudioSource tankSound;
    void Start () {
        r = GetComponent<Rigidbody>();
    }
    
    void Update () {
        if (isCurrentTank) // 如果为true表示是当前tank
        {
            float h = Input.GetAxis("OtherOneHorizontal");
            float v = Input.GetAxis("OtherOneVertical");
            // 控制前后移动
            r.velocity = transform.forward * v * speed;
            // 控制左右旋转
            r.angularVelocity = transform.up * h * angularSpeed;
            if (Mathf.Abs(h) > 0.1 || Mathf.Abs(v) > 0.1)
            {
                if (tankSound.isPlaying == false)
                {
                    PlayTankSound(Run);
                }
            }
            else
            {
                if (tankSound.isPlaying == false)
                {
                    PlayTankSound(idle);
                }
            }
        }
        else  // 如果为false表示不是当前坦克
        {
            float h = Input.GetAxis("OtherTwoHorizontal");
            float v = Input.GetAxis("OtherTwoVertical");
            // 控制前后移动
            r.velocity = transform.forward * v * speed;
            // 控制左右旋转
            r.angularVelocity = transform.up * h * angularSpeed;
            if (Mathf.Abs(h) > 0.1 || Mathf.Abs(v) > 0.1)
            {
                if (tankSound.isPlaying == false)
                {
                    PlayTankSound(Run);
                }
            }
            else
            {
                if (tankSound.isPlaying == false)
                {
                    PlayTankSound(idle);
                }
            }
        }
    }
    // 根据片段播放声音
    void PlayTankSound(AudioClip clip)
    {
        tankSound.clip = clip;
        tankSound.Play();
    }
}
9、设置血条(初级可以不做这一步)

其实就是通过UGUI的Slider进行。 修改画布为世界坐标,移动至tank下面修改位置就可以了。

Paste_Image.png

Paste_Image.png

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、思路
  • 2、导入资源与素材
    • 修改环境光,与背景色
    • 3、设置Tank游戏对象
    • 4、设置不同的控制模式
    • 5、子弹预制与实例化
    • 6、关于特效的制作:
    • 7、控制视野操作
    • 8、播放tank移动音效以及其他音效
    • 9、设置血条(初级可以不做这一步)
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档