码迷,mamicode.com
首页 > 编程语言 > 详细

unity demo之坦克攻击

时间:2017-05-31 20:51:00      阅读:469      评论:0      收藏:0      [点我收藏+]

标签:package   src   通过   第一个   number   实现   脚本   补充   des   

 

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

技术分享

分析:

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”,具体方法这里懒得说了。

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

unity demo之坦克攻击

标签:package   src   通过   第一个   number   实现   脚本   补充   des   

原文地址:http://www.cnblogs.com/leeplogs/p/6925789.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!