Unity實(shí)現(xiàn)已知落點(diǎn)和速度自動(dòng)計(jì)算發(fā)射角度
本文實(shí)例為大家分享了Unity已知落點(diǎn)和速度自動(dòng)計(jì)算發(fā)射角度的具體代碼,供大家參考,具體內(nèi)容如下
在發(fā)射物已知落點(diǎn)和速度的情況下如果剛體應(yīng)用重力,不容易算出發(fā)射角度,以下為計(jì)算過(guò)程
/// <summary> /// 掛載到發(fā)射器上即可 /// </summary> public class Rotate : MonoBehaviour { public GameObject prefab; //發(fā)射物 public float speed; //發(fā)射物速度 public bool 拋射 = false; //拋射:仰角 > 45°,否:仰角 < 45° Ray RayMouse; Vector3 direction; Quaternion rotation; void Update() { if (Input.GetMouseButtonDown(0)) { GameObject go = Instantiate(prefab, transform.position, transform.rotation); go.AddComponent<Rigidbody>().velocity = go.transform.forward * speed; } RaycastHit hit; RayMouse = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(RayMouse.origin, RayMouse.direction, out hit, Mathf.Infinity)) { RotateToMouseDirection(gameObject, hit.point); } } /// <summary> /// 執(zhí)行整體旋轉(zhuǎn) /// </summary> /// <param name="obj">旋轉(zhuǎn)的物體(自身)</param> /// <param name="destination">目標(biāo)點(diǎn)(鼠標(biāo)指向)</param> void RotateToMouseDirection(GameObject obj, Vector3 destination) { direction = destination - obj.transform.position; rotation = Quaternion.LookRotation(direction); Vector3 finalAngle = rotation.eulerAngles; float targetAng = Angle(destination); finalAngle = new Vector3(-targetAng, finalAngle.y, finalAngle.z);//注意正負(fù) obj.transform.localRotation = Quaternion.Euler(finalAngle); } /// <summary> /// 自動(dòng)計(jì)算x歐拉角,即仰角 /// </summary> /// <param name="target">目標(biāo)點(diǎn)坐標(biāo)</param> /// <returns></returns> float Angle(Vector3 target) { float angleX; float distX = Vector2.Distance(new Vector2(target.x, target.z), new Vector2(transform.position.x, transform.position.z)); float distY = target.y - transform.position.y; float posBase = (Physics.gravity.y * Mathf.Pow(distX, 2.0f)) / (2.0f * Mathf.Pow(speed, 2.0f)); float posX = distX / posBase; float posY = (Mathf.Pow(posX, 2.0f) / 4.0f) - ((posBase - distY) / posBase); if (posY >= 0.0f) { if (拋射) //字段 angleX = Mathf.Rad2Deg * Mathf.Atan(-posX / 2.0f + Mathf.Pow(posY, 0.5f)); else angleX = Mathf.Rad2Deg * Mathf.Atan(-posX / 2.0f - Mathf.Pow(posY, 0.5f)); } else { angleX = 45.0f; } return angleX; } }
實(shí)際效果
拋射效果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#中的靜態(tài)成員、靜態(tài)方法、靜態(tài)類(lèi)介紹
本文主要介紹了C#中的靜態(tài)成員、靜態(tài)方法、靜態(tài)類(lèi)的基礎(chǔ)的使用,并做了相關(guān)的代碼演示,供初學(xué)者參考。2016-03-03winform c#中子窗體關(guān)閉刷新父窗體的實(shí)例
下面小編就為大家?guī)?lái)一篇winform c#中子窗體關(guān)閉刷新父窗體的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02c#利用Excel直接讀取數(shù)據(jù)到DataGridView
這個(gè)例子的功能是c#讀取excel文件,大家可以參考使用2013-11-11C# 開(kāi)發(fā)圓角控件(窗體)的具體實(shí)現(xiàn)
這篇文章主要介紹了C# 開(kāi)發(fā)圓角控件的具體實(shí)現(xiàn),需要的朋友可以參考下2014-02-02C#使用SQL DataAdapter數(shù)據(jù)適配代碼實(shí)例
今天小編就為大家分享一篇關(guān)于C#使用SQL DataAdapter數(shù)據(jù)適配代碼實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10C#之HttpClient設(shè)置cookies的兩種方式
這篇文章主要介紹了C#之HttpClient設(shè)置cookies的兩種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11