1、思路
這個游戲最初是國外的教程,后來被國內(nèi)的眾多機構和個人仿寫推出眾多的版本。但是內(nèi)容原理差不多,有的是通過插件(PlayMaker,CaverAI),有的通過頂級封裝簡單幾十行代碼就搞出來。特別適合初學者總結自己的學習水平。由于教學需要,所以研究一下,僅供參考。
tank.gif
2、導入資源與素材
資源來源于網(wǎng)絡,如有侵權,聯(lián)系我刪除
修改環(huán)境光,與背景色
環(huán)境設置
相機設置
3、設置Tank游戲?qū)ο?/h5>
設置碰撞器添加剛體,讓坦克能夠在地面行走,旋轉(zhuǎn)
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;
// 控制左右旋轉(zhuǎn)
r.angularVelocity = transform.up * h * angularSpeed;
}
}
4、設置不同的控制模式
Paste_Image.png
設置一個標志位,來判斷坦克是否為角色控制坦克
Paste_Image.png
5、子彈預制與實例化
Paste_Image.png
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、關于特效的制作:
子彈的特效,就在觸發(fā)器方法中寫,可以通過觸發(fā)器方法寫,當檢測到碰撞的時候,拿到碰撞的點,實例化爆炸效果
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");
}
}
}
在攻擊腳本中實現(xiàn)Tank減血方法,判斷tank血量實例化tank爆炸效果
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的位置平均值
攝像機位置 = 位置平均值+差值。
也就是說每次的位置平均值不一樣。
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移動音效以及其他音效
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;
// 控制左右旋轉(zhuǎn)
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;
// 控制左右旋轉(zhuǎn)
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);
}
}
}
}
// 根據(jù)片段播放聲音
void PlayTankSound(AudioClip clip)
{
tankSound.clip = clip;
tankSound.Play();
}
}
9、設置血條(初級可以不做這一步)
其實就是通過UGUI的Slider進行。
修改畫布為世界坐標,移動至tank下面修改位置就可以了。
Paste_Image.png
Paste_Image.png