public void CreateLink(Vector3 linkCenter, Vector3 linkDirection, float upDistance, float downDistance, Transform targetParent, int agentTypeId) { //holders for found start and end from GetClosestOnNavMesh Vector3 startPoint; Vector3 endPoint; if (NavHelper.GetClosestOnNavMesh(linkCenter, linkDirection, upDistance, downDistance, FindNavMeshDistance, out startPoint) && NavHelper.GetClosestOnNavMesh(linkCenter, -linkDirection, upDistance, downDistance, FindNavMeshDistance, out endPoint)) { //if start is higher than end then swap. This keeps bidirectional falling down instead of jumping up if (startPoint.y < endPoint.y) { Vector3 startHolder = startPoint; startPoint = endPoint; endPoint = startHolder; } //if link start and end match another link's start and end or end and start then return if (!IsLinkDistantEnough(startPoint, endPoint)) { return; } //return if obstructed if (IsLinkObstructed(startPoint, endPoint)) { return; } //grounded vectors to find ground distance Vector3 startGrounded = startPoint.GroundedVector(); Vector3 endGrounded = endPoint.GroundedVector(); float groundedDistance = Vector3.Distance(startGrounded, endGrounded); float yDistance = Mathf.Abs(startPoint.y - endPoint.y); //return if yDistance is greater than fall distance// if (yDistance > MaxFallHeight) { return; } //return if distance is too far if (groundedDistance > MaxHorizontalDistance) { return; } //create NavMeshLink NavMeshLink navLink = targetParent.gameObject.AddComponent <NavMeshLink>(); navLink.startPoint = targetParent.InverseTransformPointUnscaled(startPoint); navLink.endPoint = targetParent.InverseTransformPointUnscaled(endPoint); navLink.width = NavLinkWidth; navLink.agentTypeID = agentTypeId; linkList.Add(navLink); //if height difference is greater than jump height then only allow falling if (yDistance > MaxJumpHeight) { navLink.bidirectional = false; } } }
/// <summary> /// create NavMeshLinks between two points /// </summary> /// <param name="start"></param> /// <param name="end"></param> /// <param name="refDirection"></param> /// <param name="targetParent"></param> /// <param name="agentTypeID"></param> public void CreateLinksBetweenPoints(Vector3 start, Vector3 end, Vector3 refDirection, Transform targetParent, int agentTypeID) { Vector3 calcNormal = NavHelper.FindFlatPerpVector(refDirection, start, end); Vector3 heading = end - start; float mag = heading.magnitude; Vector3 normHeading = heading.normalized; for (float i = (NavLinkWidth / 2) + .1f; i < mag; i += NavLinkWidth) { //create jump to links CreateLink(start + normHeading * i, calcNormal, MaxJumpHeight, MaxJumpHeight + MaxFallHeight + .1f, targetParent, agentTypeID); //create fall to links CreateLink(start + normHeading * i, calcNormal, .1f, MaxFallHeight + .1f, targetParent, agentTypeID); //create mid links CreateLink(start + normHeading * i, calcNormal, MaxJumpHeight / 2, MaxJumpHeight / 2, targetParent, agentTypeID); } }