網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了Unity實(shí)現(xiàn)坦克模型的具體代碼,供大家參考,具體內(nèi)容如下
用立方體和圓柱體搭建完坦克的外觀,接下來我們主要實(shí)現(xiàn)炮筒的上下移動(dòng)、炮彈的發(fā)射和炮彈的運(yùn)動(dòng)。
坦克模型
可調(diào)節(jié)的炮筒
功能目標(biāo):鼠標(biāo)單擊上下移動(dòng)以控制炮筒的上下移動(dòng)。
其實(shí)本質(zhì)上就是實(shí)現(xiàn)一個(gè)物體的旋轉(zhuǎn),但是問題在于,旋轉(zhuǎn)的軸心。選中炮筒,我們可以看到,它旋轉(zhuǎn)的軸心就是中心,但是炮筒的旋轉(zhuǎn)肯定是要以圓柱體的下端點(diǎn)為軸心。為了解決這個(gè)問題,增加一個(gè)空物體(Cylinder_father),位置在炮筒圓柱體的下端點(diǎn)(即炮筒目標(biāo)的旋轉(zhuǎn)軸心),并且把圓柱體炮筒放在其下面,使二者成為父子關(guān)系,這樣也就可以間接的改變物體旋轉(zhuǎn)的軸心。
在Cylinder_father上面掛載代碼控制物體的旋轉(zhuǎn),以此間接的控制炮筒的上下移動(dòng)。
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); ? ? ? ? //炮臺(tái)的旋轉(zhuǎn) ? ? ? ? if (Input.GetMouseButton(0))//鼠標(biāo)單擊 ? ? ? ? { ? ? ? ? ? ? offsetY = Input.GetAxis("Mouse Y");//鼠標(biāo)的移動(dòng)向量 ? ? ? ? ? ? //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ā)射點(diǎn)
功能目標(biāo):按下攻擊鍵,炮彈要從炮筒的上端點(diǎn)發(fā)射出去。
需要用一個(gè)物體標(biāo)記發(fā)射點(diǎn)的位置,更重要的是,炮彈發(fā)射點(diǎn)的位置一定要跟隨炮筒的移動(dòng)而移動(dòng)。因此,新增一個(gè)空物體表示發(fā)射點(diǎn)(firepoint),并且將發(fā)射點(diǎn)移到炮筒下面,形成父子關(guān)系。
在firepoint上面掛載代碼,實(shí)現(xiàn)發(fā)射炮彈的功能。按下J鍵,在發(fā)射點(diǎn)的位置實(shí)例化生成一個(gè)預(yù)制體炮彈。
public class Shoot : MonoBehaviour { ? ? private Transform firepoint;//發(fā)射地點(diǎn) ? ? 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ā)射點(diǎn)位置 ? ? ? ? shell = s.GetComponent<Rigidbody>(); ? ? } ? ? // Update is called once per frame ? ? void Update() ? ? { ? ? ? ? //點(diǎn)擊左鍵并且時(shí)間已經(jīng)大于發(fā)射時(shí)間 ? ? ? ? if (Input.GetKeyDown(KeyCode.J)&&Time.time>nextfire )//攻擊鍵——鍵盤J ? ? ? ? { ? ? ? ? ? ? nextfire = Time.time + firerate; ? ? ? ? ? ? //實(shí)例化炮彈 ? ? ? ? ? ? Rigidbody clone; ? ? ? ? ? ? clone = (Rigidbody)Instantiate(shell, firepoint.position, shell.transform.rotation); ? ? ? ? } ? ? } }
炮彈的拋物線運(yùn)動(dòng)
目標(biāo)功能:炮彈發(fā)射后要按照拋物線的軌跡運(yùn)動(dòng)。
這里其實(shí)就是一個(gè)簡單的斜拋運(yùn)動(dòng),關(guān)鍵點(diǎn)就在于將速度分解到豎直方向(Y軸)和水平方向(Z軸),然后豎直方向上模擬勻加速運(yùn)動(dòng),水平方向上是勻速運(yùn)動(dòng)。速度的分解也很簡單,利用三角函數(shù)就可以實(shí)現(xiàn)。注意:Unity中的三角函數(shù)必須要轉(zhuǎn)化成為弧度制。
在預(yù)制體炮彈上掛載代碼,實(shí)現(xiàn)拋物線運(yùn)動(dòng)。
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;//豎直方向上模擬勻加速運(yùn)動(dòng) ? ? ? ? ? ? transform.Translate(0,speedY*Time.deltaTime, speedZ * Time.deltaTime); ? ? ? ? } ? ? ? ? else ? ? ? ? { ? ? ? ? ? ? flag = false; ? ? ? ? ? ? speedY += g * Time.deltaTime;//豎直方向上模擬勻加速運(yùn)動(dòng) ? ? ? ? ? ? transform.Translate(0, -speedY * Time.deltaTime, speedZ * Time.deltaTime); ? ? ? ? } ? ? ? ? if(this.transform.position.y<0)//如果炮彈運(yùn)動(dòng)到地面以下,毀滅 ? ? ? ? { ? ? ? ? ? ? Destroy(this.gameObject); ? ? ? ? } ? ? } }
原文鏈接:https://blog.csdn.net/qq_45872901/article/details/122395445
相關(guān)推薦
- 2022-02-20 Android?WebView開發(fā)之WebView與Native交互_Android
- 2022-03-19 Nginx純配置實(shí)現(xiàn)日志實(shí)時(shí)上報(bào)的思路與方法_nginx
- 2022-04-06 .Net使用加密升級(jí)數(shù)據(jù)安全_實(shí)用技巧
- 2022-07-16 Spring MVC重定向和轉(zhuǎn)發(fā)
- 2022-07-10 Linux安裝及管理程序
- 2023-11-22 fatal: unable to access ‘https://github.com/xxxxx/
- 2022-03-16 C#中獲取二維數(shù)組的行數(shù)和列數(shù)以及多維數(shù)組各個(gè)維度的長度_C#教程
- 2022-09-06 一文詳解Python如何優(yōu)雅地對(duì)數(shù)據(jù)進(jìn)行分組_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支