public override void Fn_GetThrow() { GameData.ThrowData tmpData = new GameData.ThrowData(); Rigidbody rigidbody = prefab.GetComponent <Rigidbody>(); float cur_str = PlayerController.thePlayerData.m_fCurStr; Vector3 pos = (SceneObject.Instance.m_ObjLand.transform.position - prefab.transform.position); if (rigidbody != null) { if (cur_str >= GameData.stint) { Vector3 mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.WorldToScreenPoint(SceneObject.Instance.m_ObjLand.transform.position).z)); GameSystem.Instance.Lauch(tmpData, rigidbody, mousePosition); } else { Vector3 mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.WorldToScreenPoint(SceneObject.Instance.m_ObjLand.transform.position).z)); mousePosition.z = pos.z * cur_str; GameSystem.Instance.Lauch(tmpData, rigidbody, mousePosition); } } prefab = null; }
private Vector3 CalculateLunchVelocity(GameData.ThrowData throwData, Rigidbody rigidbody, Vector3 target) { float displacementY = target.y - rigidbody.position.y; Vector3 displacementXZ = new Vector3(target.x - rigidbody.position.x, 0, target.z - rigidbody.position.z); float time = Mathf.Sqrt(Mathf.Abs(-2 * throwData.h / throwData.gravity)) + Mathf.Sqrt(Mathf.Abs(2 * (displacementY - throwData.h) / throwData.gravity)); Vector3 velocityY = Vector3.up * Mathf.Sqrt(Mathf.Abs(-2 * throwData.gravity * throwData.h)); Vector3 velocityXZ = displacementXZ / time; return(velocityXZ + velocityY * -Mathf.Sign(throwData.gravity)); }
//拋無線的計算公式 public void Lauch(GameData.ThrowData throwData, Rigidbody rigidbody, Vector3 target) { Physics.gravity = Vector3.up * throwData.gravity; rigidbody.useGravity = true; rigidbody.velocity = CalculateLunchVelocity(throwData, rigidbody, target); }