Unity怎么实现已知落点和速度自动计算发射角度-成都快上网建站

Unity怎么实现已知落点和速度自动计算发射角度

这期内容当中小编将会给大家带来有关Unity怎么实现已知落点和速度自动计算发射角度,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

我们拥有10余年网页设计和网站建设经验,从网站策划到网站制作,我们的网页设计师为您提供的解决方案。为企业提供成都网站制作、成都网站建设、微信开发、微信小程序开发、手机网站开发、HTML5、等业务。无论您有什么样的网站设计或者设计方案要求,我们都将富于创造性的提供专业设计服务并满足您的需求。

在发射物已知落点和速度的情况下如果刚体应用重力,不容易算出发射角度,以下为计算过程

///

/// 挂载到发射器上即可/// 

public class Rotate : MonoBehaviour{ public GameObject prefab; //发射物 public float speed;   

//发射物速度 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);  } } 

///

/// 执行整体旋转 /// 

/// 旋转的物体(自身) 

/// 目标点(鼠标指向) 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 (抛射) //字段    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怎么实现已知落点和速度自动计算发射角度了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


分享标题:Unity怎么实现已知落点和速度自动计算发射角度
浏览路径:http://kswjz.com/article/ipcgcj.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流