Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to automatically calculate the launch Angle of known Landing Point and Velocity by Unity

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, the editor will bring you about how Unity can automatically calculate the launch angle of the known landing point and speed. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

If gravity is applied to the rigid body, it is not easy to calculate the launch angle when the impact point and velocity of the projectile are known. The following is the calculation process.

/ / just mount it to the transmitter /

Public class Rotate: MonoBehaviour {public GameObject prefab; / / emitter public float speed

/ / projectile velocity public bool ejection = false

/ / projection: elevation > 45 °, No: elevation

< 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); } } /// /// 执行整体旋转 /// /// 旋转的物体(自身) /// 目标点(鼠标指向) 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);//注意正负 obj.transform.localRotation = Quaternion.Euler(finalAngle); } /// /// 自动计算x欧拉角,即仰角 /// /// 目标点坐标 /// 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 (ejection) / / field 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;}}

The above is the Unity shared by the editor how to automatically calculate the launch angle of the known landing point and speed. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report