Unity實現(xiàn)坦克模型
本文實例為大家分享了Unity實現(xiàn)坦克模型的具體代碼,供大家參考,具體內(nèi)容如下
用立方體和圓柱體搭建完坦克的外觀,接下來我們主要實現(xiàn)炮筒的上下移動、炮彈的發(fā)射和炮彈的運動。
坦克模型
可調(diào)節(jié)的炮筒
功能目標(biāo):鼠標(biāo)單擊上下移動以控制炮筒的上下移動。
其實本質(zhì)上就是實現(xiàn)一個物體的旋轉(zhuǎn),但是問題在于,旋轉(zhuǎn)的軸心。選中炮筒,我們可以看到,它旋轉(zhuǎn)的軸心就是中心,但是炮筒的旋轉(zhuǎn)肯定是要以圓柱體的下端點為軸心。為了解決這個問題,增加一個空物體(Cylinder_father),位置在炮筒圓柱體的下端點(即炮筒目標(biāo)的旋轉(zhuǎn)軸心),并且把圓柱體炮筒放在其下面,使二者成為父子關(guān)系,這樣也就可以間接的改變物體旋轉(zhuǎn)的軸心。
在Cylinder_father上面掛載代碼控制物體的旋轉(zhuǎn),以此間接的控制炮筒的上下移動。
public class Tank_rotate : MonoBehaviour { ? ? private float offsetY = 0; ? ? public float rotatespeed = 6f; ? ? //public GameObject firepoint; ? ? // Update is called once per frame ? ? void Update() ? ? { ? ? ? ? //Debug.Log(transform.rotation.eulerAngles.z); ? ? ? ? //炮臺的旋轉(zhuǎn) ? ? ? ? if (Input.GetMouseButton(0))//鼠標(biāo)單擊 ? ? ? ? { ? ? ? ? ? ? offsetY = Input.GetAxis("Mouse Y");//鼠標(biāo)的移動向量 ? ? ? ? ? ? //Debug.Log(offsetY); ? ? ? ? ? ? if (transform.rotation.eulerAngles.z > 283 && transform.rotation.eulerAngles.z < 324) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? transform.Rotate(new Vector3(0, 0, offsetY ) * rotatespeed, Space.Self); ? ? ? ? ? ? ? ? //firepoint.transform.Translate(new Vector3(0, -offsetX, offsetY) * rotatespeed, Space.World); ? ? ? ? ? ? } ? ? ? ? ? ? else if (transform.rotation.eulerAngles.z <= 283) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if(offsetY > 0) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? transform.Rotate(new Vector3(0, 0, offsetY) * rotatespeed, Space.Self); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if(offsetY < 0) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? transform.Rotate(new Vector3(0, 0, offsetY) * rotatespeed, Space.Self); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ?? ? ? } }
炮彈發(fā)射點
功能目標(biāo):按下攻擊鍵,炮彈要從炮筒的上端點發(fā)射出去。
需要用一個物體標(biāo)記發(fā)射點的位置,更重要的是,炮彈發(fā)射點的位置一定要跟隨炮筒的移動而移動。因此,新增一個空物體表示發(fā)射點(firepoint),并且將發(fā)射點移到炮筒下面,形成父子關(guān)系。
在firepoint上面掛載代碼,實現(xiàn)發(fā)射炮彈的功能。按下J鍵,在發(fā)射點的位置實例化生成一個預(yù)制體炮彈。
public class Shoot : MonoBehaviour { ? ? private Transform firepoint;//發(fā)射地點 ? ? public GameObject s; ? ? private Rigidbody shell;//炮彈 ? ? private float nextfire = 1f; ? ? public float firerate = 2f; ? ? // Start is called before the first frame update ? ? void Start() ? ? { ? ? ? ? firepoint = gameObject.GetComponent<Transform>();//發(fā)射點位置 ? ? ? ? shell = s.GetComponent<Rigidbody>(); ? ? } ? ? // Update is called once per frame ? ? void Update() ? ? { ? ? ? ? //點擊左鍵并且時間已經(jīng)大于發(fā)射時間 ? ? ? ? if (Input.GetKeyDown(KeyCode.J)&&Time.time>nextfire )//攻擊鍵——鍵盤J ? ? ? ? { ? ? ? ? ? ? nextfire = Time.time + firerate; ? ? ? ? ? ? //實例化炮彈 ? ? ? ? ? ? Rigidbody clone; ? ? ? ? ? ? clone = (Rigidbody)Instantiate(shell, firepoint.position, shell.transform.rotation); ? ? ? ? } ? ? } }
炮彈的拋物線運動
目標(biāo)功能:炮彈發(fā)射后要按照拋物線的軌跡運動。
這里其實就是一個簡單的斜拋運動,關(guān)鍵點就在于將速度分解到豎直方向(Y軸)和水平方向(Z軸),然后豎直方向上模擬勻加速運動,水平方向上是勻速運動。速度的分解也很簡單,利用三角函數(shù)就可以實現(xiàn)。注意:Unity中的三角函數(shù)必須要轉(zhuǎn)化成為弧度制。
在預(yù)制體炮彈上掛載代碼,實現(xiàn)拋物線運動。
public class SheelMove : MonoBehaviour { ? ? private Transform Cylinder_father; ? ? private float angle = 0; ? ? public float speed = 10f; ? ? private float speedY = 0; ? ? private float speedZ = 0; ? ? public float g = 1f; ? ? private bool flag = true; ? ? void Start() ? ? { ? ? ? ? Cylinder_father = GameObject.Find("Cylinder_father").transform; ? ? ? ? angle = Cylinder_father.eulerAngles.z-360;//炮筒的角度也即速度分解的角度 ? ? ? ? speedY = (-1)*speed * Mathf.Cos((-1)*angle * Mathf.PI / 180);//弧度制轉(zhuǎn)換 ? ? ? ? speedZ = speed * Mathf.Sin(angle * Mathf.PI / 180);//弧度制轉(zhuǎn)換 ? ? } ? ? // Update is called once per frame ? ? void Update() ? ? { ? ? ? ? if (speedY>0&&flag) ? ? ? ? { ? ? ? ? ? ? speedY -= g * Time.deltaTime;//豎直方向上模擬勻加速運動 ? ? ? ? ? ? transform.Translate(0,speedY*Time.deltaTime, speedZ * Time.deltaTime); ? ? ? ? } ? ? ? ? else ? ? ? ? { ? ? ? ? ? ? flag = false; ? ? ? ? ? ? speedY += g * Time.deltaTime;//豎直方向上模擬勻加速運動 ? ? ? ? ? ? transform.Translate(0, -speedY * Time.deltaTime, speedZ * Time.deltaTime); ? ? ? ? } ? ? ? ? if(this.transform.position.y<0)//如果炮彈運動到地面以下,毀滅 ? ? ? ? { ? ? ? ? ? ? Destroy(this.gameObject); ? ? ? ? } ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實現(xiàn)Windows服務(wù)測試與調(diào)試
這篇文章介紹了C#實現(xiàn)Windows服務(wù)測試與調(diào)試的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02C# 構(gòu)造函數(shù)如何調(diào)用虛方法
這篇文章主要介紹了C# 構(gòu)造函數(shù)如何調(diào)用虛方法,文中講解非常詳細(xì),示例代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07unity 如何判斷鼠標(biāo)是否在哪個UI上(兩種方法)
這篇文章主要介紹了unity 判斷鼠標(biāo)是否在哪個UI上的兩種實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04C#的靜態(tài)工廠方法與構(gòu)造函數(shù)相比有哪些優(yōu)缺點
這篇文章主要介紹了C#的靜態(tài)工廠方法與構(gòu)造函數(shù)對比的優(yōu)缺點,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07