First Attempt : Target Matching
實作 Target Matching 有幾個要點
- 於 transition 狀態中不能啟動 Target Matching 會被忽視然後出現警告.
- 如果已經處於 Target Matching 中途出現 transition, 原有的 Target Matching 的處理會被中止.
- Target Matching 無視物理, 只是單純的於起始點 lerp 到終點. rotation 亦是一樣.
這次嘗試中使利用 Trigger box 來決定起跳及著地點, 利用起跳的瞬間的 Transfrom position/rotation 來決定坐標及面向.
依該坐標向向前順延 2 個 Collider 的 Bound box 距離, 即可知道在路踁上接觸的點.
再以該最近的點(白星) & 最遠的點(黃星) 為指標取得可調整的著陸點(淺藍色)
public bool TryGetLandingPoint(int index, Vector3 pos, Vector3 facing, out Vector3 landingPoint) { landingPoint = Vector3.zero; Collider landingZone = index == -1 ? m_DefaultJump.landingZone : m_ExtraConfig[index].landingZone; Bounds both = landingZone.bounds; both.Encapsulate(m_TriggerArea.bounds); float maxDistance = (both.min - both.max).magnitude; // Calculate facing adjustment. Vector3 virtualPlaneNormal = Vector3.up; Vector3 vertor = landingZone.bounds.center - m_TriggerArea.bounds.center; Vector3 dir = vertor; Vector3.OrthoNormalize(ref dir, ref virtualPlaneNormal); Vector3 biasVertor = Vector3.Lerp(dir, facing, 0.5f); Vector3 fixedFacing = Vector3.ProjectOnPlane(biasVertor, virtualPlaneNormal).normalized * maxDistance; if (m_Debug.gizmos) { DebugExtend.DrawRay(m_TriggerArea.bounds.center, vertor, Color.blue); DebugExtend.DrawRay(m_TriggerArea.bounds.center, virtualPlaneNormal, Color.green); DebugExtend.DrawRay(pos, facing * maxDistance, Color.gray); DebugExtend.DrawRay(pos, fixedFacing, Color.white); } Ray ray = new Ray(pos, fixedFacing); if (landingZone.bounds.IntersectRay(ray)) { Vector3 near = landingZone.Raycast(ray, out RaycastHit hit, maxDistance) ? hit.point : Vector3Extend.NearestPointOnLine(ray.origin, ray.direction, landingZone.ClosestPoint(pos)); Vector3 far = landingZone.ClosestPoint(ray.GetPoint(maxDistance)); // Project on current direction. far = Vector3Extend.NearestPointOnLine(ray.origin, ray.direction, far); float weight = index == -1 ? m_DefaultJump.landingWeight : m_ExtraConfig[index].landingWeight; landingPoint = Vector3.Lerp(near, far, weight); if (m_Debug.gizmos) { DebugExtend.DrawPoint(near, Color.white, 0.3f); DebugExtend.DrawPoint(landingPoint, Color.cyan, 0.3f); DebugExtend.DrawPoint(far, Color.yellow, 0.3f); } return true; } else { DebugExtend.DrawRay(pos, fixedFacing, Color.red, 5f); } return false; }
簡單來說這次實驗是失敗的, 只能算是功能上完成實際根本到不了 AAA 的水平.
也在 Unity3d Forum 上詢問過好像也沒有更好的辦法.
https://forum.unity.com/threads/how-to-implement-match-target-jump-best-practice.1027060/#post-6659785
所以決定另外找一個辦法來完成.
(待續)
Pingback: 用 TimeLine 來實作跳越障礙物 – Clonefactor