public void MoveToCircleIntersection2D(Vector3 circleCenter3D, float radius, IMovementPlane transform) { if (path == null) { return; } // Move forwards as long as we are getting closer to circleCenter3D while (segmentIndex < path.Count - 2 && VectorMath.ClosestPointOnLineFactor(path[segmentIndex], path[segmentIndex + 1], circleCenter3D) > 1) { NextSegment(); } var circleCenter = transform.ToPlane(circleCenter3D); // Move forwards as long as the current segment endpoint is within the circle while (segmentIndex < path.Count - 2 && (transform.ToPlane(path[segmentIndex + 1]) - circleCenter).sqrMagnitude <= radius * radius) { NextSegment(); } // Calculate the intersection with the circle. This involves some math. var factor = VectorMath.LineCircleIntersectionFactor(circleCenter, transform.ToPlane(path[segmentIndex]), transform.ToPlane(path[segmentIndex + 1]), radius); // Move to the intersection point MoveToSegment(segmentIndex, factor); }
private void OnPathComplete(Path path) { path.Claim(this); _isSearchingPath = false; _isAtDestination = false; if (path.error) { path.Release(this); return; } if (_path != null) { _path.Release(this); } _path = path as ABPath; var graph = AstarData.GetGraph(path.path[0]) as ITransformedGraph; _movementPlane = graph != null ? graph.transform : GraphTransform.identityTransform; if (_path.vectorPath.Count == 1) { _path.vectorPath.Add(_path.vectorPath[0]); } _interpolator.SetPath(_path.vectorPath); var position = transform.position; _interpolator.MoveToLocallyClosestPoint((position + _path.originalStartPoint) * 0.5f); _interpolator.MoveToLocallyClosestPoint(position); }
protected virtual void UpdateMovementPlane() { if (path.path == null || path.path.Count == 0) { return; } var graph = AstarData.GetGraph(path.path[0]) as ITransformedGraph; IMovementPlane graphTransform = graph != null ? graph.transform : (orientation == OrientationMode.YAxisForward ? new GraphTransform(Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, 270, 90), Vector3.one)) : GraphTransform.identityTransform); movementPlane = graphTransform.ToSimpleMovementPlane(); }
public void MoveToCircleIntersection2D(Vector3 circleCenter3D, float radius, IMovementPlane transform) { Vector2 vector = transform.ToPlane(circleCenter3D); while (this.segmentIndex < this.path.Count - 2 && (transform.ToPlane(this.path[this.segmentIndex + 1]) - vector).sqrMagnitude <= radius * radius) { this.NextSegment(); } float fractionAlongSegment = VectorMath.LineCircleIntersectionFactor(vector, transform.ToPlane(this.path[this.segmentIndex]), transform.ToPlane(this.path[this.segmentIndex + 1]), radius); this.MoveToSegment(this.segmentIndex, fractionAlongSegment); }
public void MoveToCircleIntersection2D(Vector3 circleCenter3D, float radius, IMovementPlane transform) { var circleCenter = transform.ToPlane(circleCenter3D); while (segmentIndex < path.Count - 2 && (transform.ToPlane(path[segmentIndex + 1]) - circleCenter).sqrMagnitude <= radius * radius) { NextSegment(); } // Calculate the intersection with the circle. This involves some math. var factor = VectorMath.LineCircleIntersectionFactor(circleCenter, transform.ToPlane(path[segmentIndex]), transform.ToPlane(path[segmentIndex + 1]), radius); // Move to the intersection point MoveToSegment(segmentIndex, factor); }
public void OnPathRequestComplete(Path newPath) { var p = newPath as ABPath; if (p == null) { throw new Exception("This function only handles ABPaths, do not use special path types"); } _waitingForPathCalculation = false; // Increase the reference count on the new path. // This is used for object pooling to reduce allocations. p.Claim(this); // Path couldn't be calculated of some reason. // More info in p.errorLog (debug string) if (p.error) { //Debug.LogErrorFormat("Path error {0} {1} is RandomPath {2} isWander {3} end {4}",Actor.Tr.position, p.errorLog, p is RandomPath, _wandering, p.originalEndPoint); p.Release(this); _errorTimer.StartNewTime(0.5f); Stop(); return; } if (_path != null) { _path.Release(this); } _path = p; // Make sure the path contains at least 2 points if (_path.vectorPath.Count == 1) { _path.vectorPath.Add(_path.vectorPath[0]); } _interpolator.SetPath(_path); var graph = AstarData.GetGraph(_path.path[0]) as ITransformedGraph; _movementPlane = graph != null ? graph.transform : GraphTransform.identityTransform; TargetReached = false; // Simulate movement from the point where the path was requested // to where we are right now. This reduces the risk that the agent // gets confused because the first point in the path is far away // from the current position (possibly behind it which could cause // the agent to turn around, and that looks pretty bad). _interpolator.MoveToLocallyClosestPoint((Tr.position + p.originalStartPoint) * 0.5f); _interpolator.MoveToLocallyClosestPoint(Tr.position); if (Wandering) { MoveTarget = (Vector3)_path.path.LastElement().position; } if (_debugRenderer != null) { _debugRenderer.positionCount = _path.vectorPath.Count; _debugRenderer.SetPositions(_path.vectorPath.ToArray()); } var distanceToEnd = _movementPlane.ToPlane(SteeringTargetV3 - Tr.position).magnitude + _interpolator.remainingDistance; if (distanceToEnd <= _endReachedDistance) { MovementCompleted(); } }