• Unity实现简易坦克移动打炮


    功能坦克可以前后移动、左右旋转、打炮
    动态演示效果
    在这里插入图片描述
    静态展示图片
    在这里插入图片描述
    核心代码
    1、Bullet.cs挂载在Bullet预设体上

    using UnityEngine;
    
    public class Bullet : MonoBehaviour
    {
        // 移动方向
        private Vector3 moveDir;
        // 移动速度
        private float moveSpeed = 2;
    
        /// 
        /// 设置子弹移动方向
        /// 
        /// 子弹移动方向
        public void setMoveDir(Vector3 bulletMoveDir)
        {
            moveDir = bulletMoveDir;
        }
    
        private void Awake()
        {
            // 设置子弹生成后3秒销毁
            Destroy(this.gameObject, 3);
        }
    
        // Update is called once per frame
        void Update()
        {
            // 更新子弹位置
            transform.position += moveDir * Time.deltaTime * moveSpeed;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    2、Tank.cs挂载在Tank空对象上

    using UnityEngine;
    
    public class Tank : MonoBehaviour
    {
        [Header("炮弹的预设体")]
        public GameObject bulletPrefab;
        // 移动速度
        private float moveSpeed = 1;
        // 旋转速度
        private float rotateSpeed = 30;
        // 开火点
        Transform firePoint;
    
        private void Awake()
        {
            // FirePos为空对象,提供子弹的发射位置信息
            firePoint = transform.Find("FirePos");
        }
    
        // Update is called once per frame
        void Update()
        {
            // 左右旋转
            float hValue = Input.GetAxis("Horizontal");
            transform.eulerAngles += transform.up * hValue * Time.deltaTime * rotateSpeed;
            
            // 前后移动
            float vValue = Input.GetAxis("Vertical");
            transform.position += transform.forward * vValue * Time.deltaTime * moveSpeed;
           
            // 开火
            if (Input.GetKeyDown(KeyCode.J))
            {
                // 在开火点生成子弹
                GameObject bullet = Instantiate(bulletPrefab, firePoint.position, Quaternion.identity);
                // 设置子弹的移动方向为坦克的前方(在Unity场景中调整FirePos的transform与坦克的transform一致)
                bullet.GetComponent<Bullet>().setMoveDir(transform.forward);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
  • 相关阅读:
    短视频创作应该注意什么问题?
    row_number()/rank() over(PARTITION BY xxx ORDER BY的MySQL5.7实现
    看了还不懂b+tree的本质就来打我
    springboot+自行车网上商城 毕业设计-附源码130948
    STL-容器适配器详解
    git实战-多分支开发-2022新项目
    矩阵分析与应用-5.3-共轭梯度与无约束最优化
    JZ63 买卖股票的最好时机(一)
    LeetCode 2353. 设计食物评分系统(sortedcontainers)
    ATF(TF-A) RSS-AP接口威胁模型-安全检测与评估
  • 原文地址:https://blog.csdn.net/qq_40945965/article/details/133800949