這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)Unity怎么實(shí)現(xiàn)已知落點(diǎn)和速度自動(dòng)計(jì)算發(fā)射角度,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)主營(yíng)秦淮網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,
app軟件定制開發(fā),秦淮h5小程序開發(fā)搭建,秦淮網(wǎng)站營(yíng)銷推廣歡迎秦淮等地區(qū)企業(yè)咨詢
在發(fā)射物已知落點(diǎn)和速度的情況下如果剛體應(yīng)用重力,不容易算出發(fā)射角度,以下為計(jì)算過(guò)程
/// /// 掛載到發(fā)射器上即可///
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().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); } }
/// /// 執(zhí)行整體旋轉(zhuǎn) ///
/// 旋轉(zhuǎn)的物體(自身)
/// 目標(biāo)點(diǎn)(鼠標(biāo)指向) 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); }
/// /// 自動(dòng)計(jì)算x歐拉角,即仰角 ///
/// 目標(biāo)點(diǎn)坐標(biāo) ///
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; }}
上述就是小編為大家分享的Unity怎么實(shí)現(xiàn)已知落點(diǎn)和速度自動(dòng)計(jì)算發(fā)射角度了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁(yè)名稱:Unity怎么實(shí)現(xiàn)已知落點(diǎn)和速度自動(dòng)計(jì)算發(fā)射角度-創(chuàng)新互聯(lián)
當(dāng)前鏈接:
http://weahome.cn/article/hoece.html