zoukankan      html  css  js  c++  java
  • unity demo之坦克攻击

    先展示一下成果吧,节后第一天上班简直困爆了,所以一定要动下脑子搞点事情。

    分析:

    1.涉及到的游戏对象有:坦克,摄像机,场景素材(包含灯光),子弹

    2.坦克具有的功能:移动,旋转,发射子弹,记录生命值或血量

    3.摄像机具有功能:跟随目标拍摄

    4.子弹具有的功能:移动,并且是在常见出来的时候就要移动,碰撞后要销毁

    OK,先分析到这里,其他就边做边看吧。

    1.先把素材导进来,素材.unitypackage下载地址链接: https://pan.baidu.com/s/1qXH4EXu 密码: h6gt

    2.添加坦克,找到模型拖到场景里面就行了。

     

    坦克的两个轮跑起来会有特效,找到拖到坦克下面,调整好位置,坦克的轮后面。

    指定坦克开火的位置,创建一个空GameObject,改名FirePosition,初学者还是要养成好的习惯,认真对待每一个取名,规范起来比较好。将FirePosition移动到发射子弹的炮眼,微调一下旋转。

    下面开始挂脚本了,第一个脚本是控制坦克的前后移动,TankMovement.cs

    using UnityEngine;
    
    public class TankMovement : MonoBehaviour {
    
    	public float speed = 5f;
    
    	public float angularSpeed = 30;
    
    	private Rigidbody rigidbody;
    
    	public int number = 2;
    
    	// Use this for initialization
    	void Start () {
    
    		rigidbody = GetComponent<Rigidbody> ();
    		
    	}
    	
    	// Update is called once per frame
    	void FixedUpdate () {
    		//前后移动
    		float v = Input.GetAxis("Verticalplay" + number );
    		rigidbody.velocity = transform.forward * speed * v;
    		//控制旋转
    		float h = Input.GetAxis("Horizontalplay" + number);
    		rigidbody.angularVelocity = transform.up * angularSpeed * h;
    	}
    }
    

      通过刚体组件让坦克移动旋转,number是为了后面添加第二个坦克,

    用什么键控制可以自己设定。

     第二个脚本是控制坦克发射子弹,TankAttack.cs

    using UnityEngine;
    
    public class TankAttack : MonoBehaviour
    {
    
    	private Transform firePosition;
    
    	public GameObject shellPrefab;
    
    	public KeyCode fireKey = KeyCode.Space;
    
    	public float shellSpeed = 20;
    
    	public AudioClip shotAudio;
    
    	// Use this for initialization
    	void Start ()
    	{
    		firePosition = transform.Find("FirePosition");
    	}
    	
    	// Update is called once per frame
    	void Update () {
    		if (Input.GetKeyDown(fireKey))
    		{
    			//音效
    			AudioSource.PlayClipAtPoint(shotAudio,firePosition.position);
    			//生成子弹对象
    			GameObject go = GameObject.Instantiate(shellPrefab, firePosition.position, firePosition.rotation);
    			//让子弹移动
    			go.GetComponent<Rigidbody>().velocity = go.transform.forward * shellSpeed;
    		}
    	}
    }
    

      音效那一行先注释掉吧,音效放后面搞。接下来做shellPrefab

    找到子弹预制体,检查该有的组件是否有缺失,

    挂脚本Shell.cs,控制子弹碰撞后的爆炸特效,

    using UnityEngine;
    
    public class Shell : MonoBehaviour
    {
    
        public GameObject shellExplosionPrefab;
    
        // Use this for initialization
        void Start () {
            
        }
        
        // Update is called once per frame
        void Update () {
            
        }
    
        void OnTriggerEnter(Collider other)
        {
            GameObject.Instantiate(shellExplosionPrefab, transform.position, transform.rotation);
            GameObject.Destroy(gameObject);
    
            if (other.tag == "Tank")
            {
                other.SendMessage("TakeDamage");
            }
            
        }
    }

    这个tag要为坦克设置“Tank”,具体方法这里懒得说了。

    到这里为止游戏基本功能已经实现了,实在有点疲乏,后面以后再补充。

  • 相关阅读:
    【转】Oracle 查询每天执行慢的SQL
    【转】StringBuilder与String的区别
    【转】C#单例模式实现
    【转】设计模式
    【转】十大排序算法
    ASP.NET jQuery 随笔 从DropDownList获取选择的text和value值
    ASP.NET jQuery 随笔 显示CheckBoxList成员选中的内容
    ASP.NET jQuery 随笔 在TextBox里面阻止复制、剪切和粘贴事件
    ASP.NET JQuery 随笔-搜索框默认提示
    JS 某一区域内所有CheckBox全选和取消全选(.net)
  • 原文地址:https://www.cnblogs.com/leeplogs/p/6925789.html
Copyright © 2011-2022 走看看