public static NearestPoint ( Vector3 lineStart, Vector3 lineEnd, Vector3 point ) : Vector3 | ||
lineStart | Vector3 | |
lineEnd | Vector3 | |
point | Vector3 | |
return | Vector3 |
public Vector3 Raycast(Vector3 origin, out RaycastHit hit, out bool walkable) { walkable = true; if (!this.heightCheck || this.use2D) { hit = default(RaycastHit); return(origin - this.up * this.fromHeight); } if (this.thickRaycast) { Ray ray = new Ray(origin, -this.up); if (Physics.SphereCast(ray, this.finalRaycastRadius, out hit, this.fromHeight + 0.005f, this.heightMask)) { return(AstarMath.NearestPoint(ray.origin, ray.origin + ray.direction, hit.point)); } walkable &= !this.unwalkableWhenNoGround; } else { if (Physics.Raycast(origin, -this.up, out hit, this.fromHeight + 0.005f, this.heightMask)) { return(hit.point); } walkable &= !this.unwalkableWhenNoGround; } return(origin - this.up * this.fromHeight); }
/** Same as #CheckHeight, except that the raycast will always start exactly at \a origin. * \a walkable will be set to false if nothing was hit. * The ray will check a tiny bit further than to the grids base to avoid floating point errors when the ground is exactly at the base of the grid */ public Vector3 Raycast(Vector3 origin, out RaycastHit hit, out bool walkable) { walkable = true; if (!heightCheck || use2D) { hit = new RaycastHit(); return(origin - up * fromHeight); } if (thickRaycast) { var ray = new Ray(origin, -up); if (Physics.SphereCast(ray, finalRaycastRadius, out hit, fromHeight + 0.005F, heightMask)) { return(AstarMath.NearestPoint(ray.origin, ray.origin + ray.direction, hit.point)); } walkable &= unwalkableWhenNoGround; } else { if (Physics.Raycast(origin, -up, out hit, fromHeight + 0.005F, heightMask)) { return(hit.point); } walkable &= unwalkableWhenNoGround; } return(origin - up * fromHeight); }
/** Returns the position with the correct height. * If #heightCheck is false, this will return \a position.\n * \a walkable will be set to false if nothing was hit. * The ray will check a tiny bit further than to the grids base to avoid floating point errors when the ground is exactly at the base of the grid */ public Vector3 CheckHeight(Vector3 position, out RaycastHit hit, out bool walkable) { walkable = true; if (!heightCheck || use2D) { hit = new RaycastHit(); return(position); } if (thickRaycast) { var ray = new Ray(position + up * fromHeight, -up); if (Physics.SphereCast(ray, finalRaycastRadius, out hit, fromHeight + 0.005F, heightMask)) { return(AstarMath.NearestPoint(ray.origin, ray.origin + ray.direction, hit.point)); } walkable &= unwalkableWhenNoGround; } else { // Cast a ray from above downwards to try to find the ground if (Physics.Raycast(position + up * fromHeight, -up, out hit, fromHeight + 0.005F, heightMask)) { return(hit.point); } walkable &= unwalkableWhenNoGround; } return(position); }
public Vector3 CheckHeight(Vector3 position, out RaycastHit hit, out bool walkable) { walkable = true; if (!this.heightCheck || this.use2D) { hit = default(RaycastHit); return(position); } if (this.thickRaycast) { Ray ray; ray..ctor(position + this.up * this.fromHeight, -this.up); if (Physics.SphereCast(ray, this.finalRaycastRadius, ref hit, this.fromHeight + 0.005f, this.heightMask)) { return(AstarMath.NearestPoint(ray.origin, ray.origin + ray.direction, hit.point)); } if (this.unwalkableWhenNoGround) { walkable = false; } } else { if (Physics.Raycast(position + this.up * this.fromHeight, -this.up, ref hit, this.fromHeight + 0.005f, this.heightMask)) { return(hit.point); } if (this.unwalkableWhenNoGround) { walkable = false; } } return(position); }
// Update is called once per frame void LateUpdate() { if (prevNode == null) { NNInfo nninfo = AstarPath.active.GetNearest(transform.position); prevNode = nninfo.node; prevPos = transform.position; } if (prevNode == null) { return; } if (prevNode != null) { IRaycastableGraph graph = AstarData.GetGraph(prevNode) as IRaycastableGraph; if (graph != null) { GraphHitInfo hit; if (graph.Linecast(prevPos, transform.position, prevNode, out hit)) { hit.point.y = transform.position.y; Vector3 closest = AstarMath.NearestPoint(hit.tangentOrigin, hit.tangentOrigin + hit.tangent, transform.position); Vector3 ohit = hit.point; ohit = ohit + Vector3.ClampMagnitude((Vector3)hit.node.position - ohit, 0.008f); if (graph.Linecast(ohit, closest, hit.node, out hit)) { hit.point.y = transform.position.y; transform.position = hit.point; } else { closest.y = transform.position.y; transform.position = closest; } } prevNode = hit.node; } } prevPos = transform.position; }
/** Returns the position with the correct height. If #heightCheck is false, this will return \a position.\n * \a walkable will be set to false if nothing was hit. The ray will check a tiny bit further than to the grids base to avoid floating point errors when the ground is exactly at the base of the grid */ public Vector3 CheckHeight(Vector3 position, out RaycastHit hit, out bool walkable) { walkable = true; if (!heightCheck || use2D) { hit = new RaycastHit(); return(position); } if (thickRaycast) { var ray = new Ray(position + up * fromHeight, -up); if (Physics.SphereCast(ray, finalRaycastRadius, out hit, fromHeight + 0.005F, heightMask)) { return(AstarMath.NearestPoint(ray.origin, ray.origin + ray.direction, hit.point)); //position+up*(fromHeight-hit.distance); } else { if (unwalkableWhenNoGround) { walkable = false; } } } else { if (Physics.Raycast(position + up * fromHeight, -up, out hit, fromHeight + 0.005F, heightMask)) { return(hit.point); } else { if (unwalkableWhenNoGround) { walkable = false; } } } return(position); }
private void LateUpdate() { if (this.prevNode == null) { this.prevNode = AstarPath.active.GetNearest(base.transform.position).node; this.prevPos = base.transform.position; } if (this.prevNode == null) { return; } if (this.prevNode != null) { IRaycastableGraph raycastableGraph = AstarData.GetGraph(this.prevNode) as IRaycastableGraph; if (raycastableGraph != null) { GraphHitInfo graphHitInfo; if (raycastableGraph.Linecast(this.prevPos, base.transform.position, this.prevNode, out graphHitInfo)) { graphHitInfo.point.y = base.transform.position.y; Vector3 vector = AstarMath.NearestPoint(graphHitInfo.tangentOrigin, graphHitInfo.tangentOrigin + graphHitInfo.tangent, base.transform.position); Vector3 vector2 = graphHitInfo.point; vector2 += Vector3.ClampMagnitude((Vector3)graphHitInfo.node.position - vector2, 0.008f); if (raycastableGraph.Linecast(vector2, vector, graphHitInfo.node, out graphHitInfo)) { graphHitInfo.point.y = base.transform.position.y; base.transform.position = graphHitInfo.point; } else { vector.y = base.transform.position.y; base.transform.position = vector; } } this.prevNode = graphHitInfo.node; } } this.prevPos = base.transform.position; }