先展示一下成果吧,節后第一天上班簡直困爆了,所以一定要動下腦子搞點事情。

分析:
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”,具體方法這里懶得說了。
到這里為止游戲基本功能已經實現了,實在有點疲乏,后面以后再補充。
