1琉苇、思路
這個(gè)游戲最初是國外的教程做鹰,后來被國內(nèi)的眾多機(jī)構(gòu)和個(gè)人仿寫推出眾多的版本雏门。但是內(nèi)容原理差不多柳畔,有的是通過插件(PlayMaker,CaverAI)馍管,有的通過頂級(jí)封裝簡單幾十行代碼就搞出來。特別適合初學(xué)者總結(jié)自己的學(xué)習(xí)水平薪韩。由于教學(xué)需要确沸,所以研究一下,僅供參考俘陷。
tank.gif
2罗捎、導(dǎo)入資源與素材
資源來源于網(wǎng)絡(luò),如有侵權(quán)拉盾,聯(lián)系我刪除
修改環(huán)境光桨菜,與背景色
環(huán)境設(shè)置
相機(jī)設(shè)置
3、設(shè)置Tank游戲?qū)ο?/h5>
設(shè)置碰撞器添加剛體捉偏,讓坦克能夠在地面行走倒得,旋轉(zhuǎn)
using UnityEngine;
using System.Collections;
// 控制坦克移動(dòng)
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");
// 控制前后移動(dòng)
r.velocity = transform.forward * v * speed;
// 控制左右旋轉(zhuǎn)
r.angularVelocity = transform.up * h * angularSpeed;
}
}
4、設(shè)置不同的控制模式
Paste_Image.png
設(shè)置一個(gè)標(biāo)志位告私,來判斷坦克是否為角色控制坦克
Paste_Image.png
5屎暇、子彈預(yù)制與實(shí)例化
Paste_Image.png
using UnityEngine;
using System.Collections;
// tank攻擊腳本
public class AttackOtherTank : MonoBehaviour {
// 實(shí)例化子彈的位置
public Transform shellPos;
// 子彈預(yù)制物
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、關(guān)于特效的制作:
子彈的特效驻粟,就在觸發(fā)器方法中寫根悼,可以通過觸發(fā)器方法寫,當(dāng)檢測到碰撞的時(shí)候蜀撑,拿到碰撞的點(diǎn)挤巡,實(shí)例化爆炸效果
using UnityEngine;
using System.Collections;
// 子彈預(yù)制物體掛載的腳本
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");
}
}
}
在攻擊腳本中實(shí)現(xiàn)Tank減血方法,判斷tank血量實(shí)例化tank爆炸效果
using UnityEngine;
using System.Collections;
// 子彈攻擊腳本
public class AttackOtherTank : MonoBehaviour {
// 實(shí)例化子彈的位置
public Transform shellPos;
// 子彈預(yù)制物
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)
{
// 播放死亡動(dòng)畫效果
Instantiate(TankDied, transform.position + Vector3.up, transform.rotation);
Destroy(this.gameObject);
}
}
}
7酷麦、控制視野操作
差值 = 攝像機(jī) - 兩個(gè)tank的位置平均值
攝像機(jī)位置 = 位置平均值+差值矿卑。
也就是說每次的位置平均值不一樣。
using UnityEngine;
using System.Collections;
public class FlowerTank : MonoBehaviour {
public Transform tank1;
public Transform tank2;
// 保存攝像機(jī)與坦克之間的距離
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移動(dòng)音效以及其他音效
using UnityEngine;
using System.Collections;
// 控制坦克移動(dòng)
public class Tank_Move : MonoBehaviour {
private Rigidbody r;
private float speed = 5f;
private float angularSpeed = 10f;
// 判斷當(dāng)前坦克
public bool isCurrentTank = true;
public AudioClip idle;
public AudioClip Run;
public AudioSource tankSound;
void Start () {
r = GetComponent<Rigidbody>();
}
void Update () {
if (isCurrentTank) // 如果為true表示是當(dāng)前tank
{
float h = Input.GetAxis("OtherOneHorizontal");
float v = Input.GetAxis("OtherOneVertical");
// 控制前后移動(dòng)
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表示不是當(dāng)前坦克
{
float h = Input.GetAxis("OtherTwoHorizontal");
float v = Input.GetAxis("OtherTwoVertical");
// 控制前后移動(dòng)
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母廷、設(shè)置血條(初級(jí)可以不做這一步)
其實(shí)就是通過UGUI的Slider進(jìn)行轻黑。
修改畫布為世界坐標(biāo),移動(dòng)至tank下面修改位置就可以了琴昆。
Paste_Image.png
Paste_Image.png