/// Gets normal vector on path based on distance travelled. public Vector3 GetNormalAtDistance(float dst, EndOfPathInstruction endOfPathInstruction = EndOfPathInstruction.Loop) { float t = dst / length; return(GetNormal(t, endOfPathInstruction)); }
public void follow(bool lookAt = false, Transform lookAtTarget = null, EndOfPathInstruction instruction = EndOfPathInstruction.Stop) { for (int i = 0; i < gameobj.Count; i++) { if (this.gameobj[i] != null) { this.distanceTravelled[i] += this.gameobj[i].GetComponent <BehaviourStrategy>().getSpeed() * Time.deltaTime; this.gameobj[i].transform.position = pathCreator[i].path.GetPointAtDistance(distanceTravelled[i], instruction); if (!lookAt) { this.gameobj[i].transform.rotation = Quaternion.Lerp(this.gameobj[i].transform.rotation, pathCreator[i].path.GetRotationAtDistance(distanceTravelled[i], instruction), 2f * Time.deltaTime); } else { this.gameobj[i].transform.LookAt(lookAtTarget); } if (this.gameobj[i].transform.position != this.prevPos[i]) { this.prevPos[i] = this.gameobj[i].transform.position; } else { this.gameobj[i] = null; } } } }
/// Gets a rotation that will orient an object in the direction of the path at this point, with local up point along the path's normal public Quaternion GetRotationAtDistance(float dst, EndOfPathInstruction endOfPathInstruction = EndOfPathInstruction.Loop) { float t = dst / length; return(GetRotation(t, endOfPathInstruction)); }
private float ClampT(float t, EndOfPathInstruction instruction) { switch (instruction) { case EndOfPathInstruction.Loop: // If t is negative, make it the equivalent value between 0 and 1 if (t < 0) { t += Mathf.CeilToInt(Mathf.Abs(t)); } t %= 1; break; case EndOfPathInstruction.PingPong: t = Mathf.PingPong(t, 1); break; case EndOfPathInstruction.Stop: t = Mathf.Clamp01(t); break; case EndOfPathInstruction.SmoothStep: t = Mathf.SmoothStep(0, 1, Mathf.Clamp01(t)); break; } return(t); }
private void OnMouseDown() { bc.isTrigger = !bc.isTrigger; transform.Rotate(0, 90, 0); transform.position = pathCreator.path.GetPointAtDistance(distanceTravelled, endOfPathInstruction = EndOfPathInstruction.Stop); transform.position = pathCreator2.path.GetPointAtDistance(distanceTravelled) + new Vector3(0, 0.5f, 0); }
/// Gets forward direction on path based on 'progress' (where 0 is start, and 1 is end of path). public Vector3 GetDirection(float progress, EndOfPathInstruction endOfPathInstruction = EndOfPathInstruction.Loop) { var data = CalculatePercentOnPathData(progress, endOfPathInstruction); Vector3 dir = Vector3.Lerp(localTangents[data.previousIndex], localTangents[data.nextIndex], data.percentBetweenIndices); return(MathUtility.TransformDirection(dir, transform, space)); }
/// Gets normal vector on path based on 'time' (where 0 is start, and 1 is end of path). public Vector3 GetNormal(float t, EndOfPathInstruction endOfPathInstruction = EndOfPathInstruction.Loop) { var data = CalculatePercentOnPathData(t, endOfPathInstruction); Vector3 normal = Vector3.Lerp(localNormals[data.previousIndex], localNormals[data.nextIndex], data.percentBetweenIndices); return(MathUtility.TransformDirection(normal, transform, space)); }
public EnemyModeOptions(EnemyMode mode, GameObject path, EndOfPathInstruction endOfPathMode) { Mode = mode; Path = path != null?path.GetComponent <PathCreator>() : null; EndOfPathMode = endOfPathMode; }
/// Gets normal vector on path based on distance travelled. public Vector3 GetNormalAtDistance( float dst, EndOfPathInstruction endOfPathInstruction = EndOfPathInstruction.Loop) { var t = dst / this.Length; return(this.GetNormal(t, endOfPathInstruction)); }
/// Gets a rotation that will orient an object in the direction of the path at this point, with local up point along the path's normal public Quaternion GetRotation(float progress, EndOfPathInstruction endOfPathInstruction = EndOfPathInstruction.Loop) { var data = CalculatePercentOnPathData(progress, endOfPathInstruction); Vector3 direction = Vector3.Lerp(localTangents[data.previousIndex], localTangents[data.nextIndex], data.percentBetweenIndices); Vector3 normal = Vector3.Lerp(localNormals[data.previousIndex], localNormals[data.nextIndex], data.percentBetweenIndices); return(Quaternion.LookRotation(MathUtility.TransformDirection(direction, transform, space), MathUtility.TransformDirection(normal, transform, space))); }
public Vector3 GetDirectionWithPositions(float t, EndOfPathInstruction endOfPathInstruction = EndOfPathInstruction.Loop) { var data = CalculatePercentOnPathData(t, endOfPathInstruction); Vector3 p1 = MathUtility.TransformPoint(localPoints[data.previousIndex], transform, space); Vector3 p2 = MathUtility.TransformPoint(localPoints[data.nextIndex], transform, space); return(p2 - p1); }
/// Gets a rotation that will orient an object in the direction of the path at this point, with local up point along the path's normal public Quaternion GetRotationAtDistance( float dst, EndOfPathInstruction endOfPathInstruction = EndOfPathInstruction.Loop) { var t = dst / this.Length; return(this.GetRotation(t, endOfPathInstruction)); }
/// Gets a rotation that will orient an object in the direction of the path at this point, with local up point along the path's normal public Quaternion GetRotation(float t, EndOfPathInstruction endOfPathInstruction = EndOfPathInstruction.Loop) { var data = CalculatePercentOnPathData(t, endOfPathInstruction); Vector3 direction = Vector3.Lerp(tangents[data.previousIndex], tangents[data.nextIndex], data.percentBetweenIndices); Vector3 normal = Vector3.Lerp(normals[data.previousIndex], normals[data.nextIndex], data.percentBetweenIndices); return(Quaternion.LookRotation(direction, normal)); }
void UpdateChildCart() { pathCreator = transform.parent.GetComponentInParent <PathFollower>().pathCreator; speed = transform.parent.GetComponentInParent <PathFollower>().speed; endOfPathInstruction = transform.parent.GetComponentInParent <PathFollower>().endOfPathInstruction; previousEndOfPathInstruction = endOfPathInstruction; trainSize = transform.parent.GetComponentInParent <PathFollower>().trainSize; extraSpace = transform.parent.GetComponentInParent <PathFollower>().extraSpace; }
/// For a given value 't' between 0 and 1, calculate the indices of the two vertices before and after t. /// Also calculate how far t is between those two vertices as a percentage between 0 and 1. TimeOnPathData CalculatePercentOnPathData(float t, EndOfPathInstruction endOfPathInstruction) { // Constrain t based on the end of path instruction switch (endOfPathInstruction) { case EndOfPathInstruction.Loop: //If t is negative, make it the equivalent value between 0 and 1 if (t < 0) { t += Mathf.CeilToInt(Mathf.Abs(t)); } t %= 1; break; case EndOfPathInstruction.Reverse: t = Mathf.PingPong(t, 1); break; case EndOfPathInstruction.Stop: t = Mathf.Clamp01(t); break; case EndOfPathInstruction.Continue: t = Mathf.Clamp01(t); break; } int prevIndex = 0; int nextIndex = NumPoints - 1; int i = Mathf.RoundToInt(t * (NumPoints - 1)); // starting guess // Starts by looking at middle vertex and determines if t lies to the left or to the right of that vertex. // Continues dividing in half until closest surrounding vertices have been found. while (true) { // t lies to left if (t <= times[i]) { nextIndex = i; } // t lies to right else { prevIndex = i; } i = (nextIndex + prevIndex) / 2; if (nextIndex - prevIndex <= 1) { break; } } float abPercent = Mathf.InverseLerp(times[prevIndex], times[nextIndex], t); return(new TimeOnPathData(prevIndex, nextIndex, abPercent)); }
public float GetMyRotation(float t, EndOfPathInstruction endOfPathInstruction = EndOfPathInstruction.Loop) { var data = CalculatePercentOnPathData(t, endOfPathInstruction); Vector3 direction = Vector3.Lerp(localTangents[data.previousIndex], localTangents[data.nextIndex], data.percentBetweenIndices); Vector3 normal = Vector3.Lerp(localNormals[data.previousIndex], localNormals[data.nextIndex], data.percentBetweenIndices); float angle = Vector3.Angle(direction, normal); Debug.Log(angle); return(angle);//Quaternion.LookRotation (MathUtility.TransformDirection (direction, transform, space), MathUtility.TransformDirection (normal, transform, space)); }
public Quaternion GetRotation2D(float t, EndOfPathInstruction endOfPathInstruction = EndOfPathInstruction.Loop) { var data = CalculatePercentOnPathData(t, endOfPathInstruction); Vector3 direction = Vector3.Lerp(tangents[data.previousIndex], tangents[data.nextIndex], data.percentBetweenIndices); //direction = new Vector3(0, 0, direction.z); Vector3 normal = Vector3.Lerp(normals[data.previousIndex], normals[data.nextIndex], data.percentBetweenIndices); //normal = new Vector3(0, 0, normal.z); //return Quaternion.LookRotation(direction, normal); Quaternion rotation = Quaternion.LookRotation (direction, normal); return(new Quaternion(0, 0, rotation.z, rotation.w)); }
private void Awake() { placer = GetComponent <Placer>(); placer.objectPlaced += onObjectPlacedOnPath; pathCreator = placer.pathCreator; ignoredAxis = placer.ignoredAxis; if (endInstruction == EndOfPathInstruction.Loop && pathCreator != null && !pathCreator.path.isClosedLoop) { endInstruction = EndOfPathInstruction.Reverse; backwardSpeed = forwardSpeed; } }
public void GetAtPercent(float t, out Vector2 pos, out Vector2 direction, EndOfPathInstruction instruction = EndOfPathInstruction.Loop) { if (!isInit) { pos = Vector2.zero; direction = Vector2.zero; return; } t = ClampT(t, instruction); int prevIndex = 0; int nextIndex = Points.Length - 1; int i = Mathf.RoundToInt(t * (Points.Length - 1)); // starting guess // Starts by looking at middle vertex and determines if t lies to the left or to the right of that vertex. // Continues dividing in half until closest surrounding vertices have been found. while (true) { // t lies to left if (t <= Ts[i]) { nextIndex = i; } // t lies to right else { prevIndex = i; } i = (nextIndex + prevIndex) / 2; if (nextIndex - prevIndex <= 1) { break; } } float abPercent = Mathf.InverseLerp(Ts[prevIndex], Ts[nextIndex], t); if (nextIndex == prevIndex) { nextIndex = (nextIndex + 1) % Points.Length; } direction = Points[nextIndex] - Points[prevIndex]; if (direction == Vector2.zero) { direction = Points[(nextIndex + 1) % Points.Length] - Points[prevIndex]; } pos = Vector2.Lerp(Points[prevIndex], Points[nextIndex], abPercent); }
/// Gets a rotation that will orient an object in the direction of the path at this point, with local up point along the path's normal public Quaternion GetRotation(float t, EndOfPathInstruction endOfPathInstruction = EndOfPathInstruction.Loop) { var data = this.CalculatePercentOnPathData(t, endOfPathInstruction); var direction = Vector3.Lerp( this.Tangents[data.PreviousIndex], this.Tangents[data.NextIndex], data.PercentBetweenIndices); var normal = Vector3.Lerp( this.Normals[data.PreviousIndex], this.Normals[data.NextIndex], data.PercentBetweenIndices); return(Quaternion.LookRotation(direction, normal)); }
private void Awake() { curStation = null; if (transform.tag == Constants.fullTrain) { UnityEditor.AI.NavMeshBuilder.ClearAllNavMeshes(); UnityEditor.AI.NavMeshBuilder.BuildNavMesh(); } previousEndOfPathInstruction = endOfPathInstruction; foreach (Transform child in gameObject.transform) { if (child.gameObject.CompareTag(Constants.trainCart)) { child.gameObject.AddComponent <PathFollower>(); trainSize++; } } extraSpace = trainSize * sizeOfCart + (trainSize - 1) * sizeOfSpaceBetweenCarts; }
public void FrameMove(EndOfPathInstruction end_of_path_instruction) { if (is_anim_running) { InitPathAnimator(); float delta_fraction = CalcSpeed(curr_fraction, speed_coeff) * Time.fixedDeltaTime; if (MoveInWorldCoordinates) { // In order to make point move equally in all paths we need to adjust it by path length delta_fraction /= vertex_path.length; } curr_fraction += delta_fraction; // Debug.Log(curr_fraction + " " + gameObject.name); if (curr_fraction >= stop_fraction) { if (end_of_path_instruction == EndOfPathInstruction.Stop) { curr_fraction = stop_fraction; // SetOnCurrFraction(); StopAnimation(); } else if (end_of_path_instruction == EndOfPathInstruction.Loop) { curr_fraction = 0.0f; } OnPathEnd.Invoke(); } calc_position = vertex_path.GetPoint(curr_fraction, end_of_path_instruction); } }
private static ClosestPoint CalculateClosestPoint(PathCreator pathCreator, EndOfPathInstruction endOfPath, Vector3 targetPosition) { var stepScale = pathCreator.path.length % 10; stepScale = Mathf.Clamp(stepScale, 1, 500f); var pathStep = pathCreator.path.length / stepScale; ClosestPoint closestPoint = new ClosestPoint(Vector3.zero, -1, 0); for (int i = 0; i < stepScale; i++) { var travelledDistance = pathStep * i; var pathPoint = pathCreator.path.GetPointAtDistance(travelledDistance, endOfPath); var vectorToTarget = targetPosition - pathPoint; vectorToTarget.y = 0; var distanceToTarget = vectorToTarget.magnitude; if (distanceToTarget < closestPoint.DistanceToTarget || closestPoint.DistanceToTarget == -1) { closestPoint = new ClosestPoint(vectorToTarget, distanceToTarget, travelledDistance); } } return(closestPoint); }
/// Gets point on path based on 'time' (where 0 is start, and 1 is end of path). public Vector3 GetPointAtTime(float t, EndOfPathInstruction endOfPathInstruction = EndOfPathInstruction.Loop) { var data = CalculatePercentOnPathData(t, endOfPathInstruction); return(Vector3.Lerp(GetPoint(data.previousIndex), GetPoint(data.nextIndex), data.percentBetweenIndices)); }
/// Gets normal vector on path based on 'time' (where 0 is start, and 1 is end of path). public Vector3 GetNormal(float t, EndOfPathInstruction endOfPathInstruction = EndOfPathInstruction.Loop) { var data = CalculatePercentOnPathData(t, endOfPathInstruction); return(Vector3.Lerp(normals[data.previousIndex], normals[data.nextIndex], data.percentBetweenIndices)); }
/// Gets forward direction on path based on 'time' (where 0 is start, and 1 is end of path). public Vector3 GetDirection(float t, EndOfPathInstruction endOfPathInstruction = EndOfPathInstruction.Loop) { var data = this.CalculatePercentOnPathData(t, endOfPathInstruction); return(Vector3.Lerp(this.Tangents[data.PreviousIndex], this.Tangents[data.NextIndex], data.PercentBetweenIndices)); }
// Start is called before the first frame update void Start() { end = EndOfPathInstruction.Stop; Destroy(this.gameObject, 42.0f); }
void FixedUpdate() { if (pathIdx == 0) { distanceTravelled += speed * Time.deltaTime; transform.position = pathCreator.path.GetPointAtDistance(distanceTravelled, endOfPathInstruction = EndOfPathInstruction.Stop); distance = Vector3.Distance(transform.position, pathCreator2.path.GetPointAtDistance(0)); distance10 = Vector3.Distance(transform.position, pathCreator3.path.GetPointAtDistance(0)); distance9 = Vector3.Distance(transform.position, pathCreator11.path.GetPointAtDistance(0)); if (A.Apx(pathCreator2.transform.parent.eulerAngles.y, 0, 0.1f) && distance < 0.4f) { pathIdx = 1; distanceTravelled2 = 0; } else if (A.Apx(pathCreator2.transform.parent.eulerAngles.y, 90, 0.1f) && distance10 < 0.4f) { pathIdx = 10; distanceTravelled10 = 0; } else if (A.Apx(pathCreator2.transform.parent.eulerAngles.y, 270, 0.1f) && distance9 < 0.4f) { pathIdx = 14; distanceTravelled15 = 0; } } else if (pathIdx == 14) { distanceTravelled15 += speed * Time.deltaTime; transform.position = pathCreator11.path.GetPointAtDistance(distanceTravelled15); distance14 = Vector3.Distance(transform.position, pathCreator.path.GetPointAtDistance(0)); if (distance14 < 0.4) { pathIdx = 0; distanceTravelled = 0; } } else if (pathIdx == 10) { distanceTravelled10 += speed * Time.deltaTime; transform.position = pathCreator3.path.GetPointAtDistance(distanceTravelled10); distance11 = Vector3.Distance(transform.position, pathCreator.path.GetPointAtDistance(0)); if (distance11 < 0.4) { pathIdx = 0; distanceTravelled = 0; } } else if (pathIdx == 1) { distanceTravelled2 += speed * Time.deltaTime; transform.position = pathCreator2.path.GetPointAtDistance(distanceTravelled2); distance2 = Vector3.Distance(transform.position, pathCreator4.path.GetPointAtDistance(0)); if (distance2 < 0.5f) { pathIdx = 2; } } else if (pathIdx == 2) { distanceTravelled3 += speed * Time.deltaTime; transform.position = pathCreator4.path.GetPointAtDistance(distanceTravelled3); distance3 = Vector3.Distance(transform.position, pathCreator5.path.GetPointAtDistance(0)); if (distance3 < 0.5f) { pathIdx = 3; } } else if (pathIdx == 3) { distanceTravelled4 += speed * Time.deltaTime; transform.position = pathCreator5.path.GetPointAtDistance(distanceTravelled4, endOfPathInstruction = EndOfPathInstruction.Stop); distance4 = Vector3.Distance(transform.position, pathCreator6.path.GetPointAtDistance(0)); distance15 = Vector3.Distance(transform.position, pathCreator7.path.GetPointAtDistance(0)); distance16 = Vector3.Distance(transform.position, pathCreator12.path.GetPointAtDistance(0)); if (A.Apx(pathCreator6.transform.parent.eulerAngles.y, 90, 0.1f) && distance4 < 0.5f) { pathIdx = 4; } else if (A.Apx(pathCreator7.transform.parent.eulerAngles.y, 0, 0.1f) && distance15 < 0.5f) { pathIdx = 12; } else if (A.Apx(pathCreator7.transform.parent.eulerAngles.y, 180, 0.1f) && distance16 < 0.5f) { pathIdx = 13; } } else if (pathIdx == 4) { distanceTravelled5 += speed * Time.deltaTime; transform.position = pathCreator6.path.GetPointAtDistance(distanceTravelled5); distance5 = Vector3.Distance(transform.position, pathCreator8.path.GetPointAtDistance(0)); if (distance5 < 0.5) { pathIdx = 5; } } else if (pathIdx == 5) { distanceTravelled6 += speed * Time.deltaTime; transform.position = pathCreator8.path.GetPointAtDistance(distanceTravelled6); distance6 = Vector3.Distance(transform.position, pathCreator9.path.GetPointAtDistance(0)); if (distance6 < 0.5) { pathIdx = 6; } } else if (pathIdx == 6) { distanceTravelled13 += speed * Time.deltaTime; transform.position = pathCreator9.path.GetPointAtDistance(distanceTravelled13); } else if (pathIdx == 12) { distanceTravelled12 += speed * Time.deltaTime; transform.position = pathCreator7.path.GetPointAtDistance(distanceTravelled12); distance7 = Vector3.Distance(transform.position, pathCreator10.path.GetPointAtDistance(0)); if (distance7 < 0.5) { pathIdx = 7; } } else if (pathIdx == 13) { distanceTravelled16 += speed * Time.deltaTime; transform.position = pathCreator12.path.GetPointAtDistance(distanceTravelled16); distance12 = Vector3.Distance(transform.position, pathCreator12.path.GetPointAtDistance(0)); if (distance12 < 0.5) { pathIdx = 7; } } else if (pathIdx == 7) { distanceTravelled14 += speed * Time.deltaTime; transform.position = pathCreator10.path.GetPointAtDistance(distanceTravelled14); distance8 = Vector3.Distance(transform.position, pathCreator5.path.GetPointAtDistance(0)); if (distance8 < 0.5) { pathIdx = 3; distanceTravelled4 = 0; } } }
void FixedUpdate() { if (pathIdx == 0) { distanceTravelled = distanceTravelled + speed * Time.deltaTime; transform.position = pathCreator.path.GetPointAtDistance(distanceTravelled, endOfPathInstruction = EndOfPathInstruction.Stop); distance = Vector3.Distance(transform.position, pathCreator2.path.GetPointAtDistance(0)); distance10 = Vector3.Distance(transform.position, pathCreator3.path.GetPointAtDistance(0)); distance9 = Vector3.Distance(transform.position, pathCreator11.path.GetPointAtDistance(0)); if (A.Apx(pathCreator2.transform.parent.eulerAngles.y, 0, 0.1f) && distance < 0.4f) { pathIdx = 1; distanceTravelled2 = 0; } else if (A.Apx(pathCreator2.transform.parent.eulerAngles.y, 90, 0.1f) && distance10 < 0.4f) { pathIdx = 10; distanceTravelled10 = 0; } else if (A.Apx(pathCreator2.transform.parent.eulerAngles.y, 270, 0.1f) && distance9 < 0.4f) { pathIdx = 14; distanceTravelled15 = 0; } } else if (pathIdx == 14) { distanceTravelled15 += speed * Time.deltaTime; transform.position = pathCreator11.path.GetPointAtDistance(distanceTravelled15); distance14 = Vector3.Distance(transform.position, pathCreator.path.GetPointAtDistance(0)); if (distance14 < 0.4) { pathIdx = 0; distanceTravelled = 0; } } else if (pathIdx == 10) { distanceTravelled10 += speed * Time.deltaTime; transform.position = pathCreator3.path.GetPointAtDistance(distanceTravelled10); distance11 = Vector3.Distance(transform.position, pathCreator.path.GetPointAtDistance(0)); if (distance11 < 0.4) { pathIdx = 0; distanceTravelled = 0; } } else if (pathIdx == 1) { distanceTravelled2 += speed * Time.deltaTime; transform.position = pathCreator2.path.GetPointAtDistance(distanceTravelled2); distance2 = Vector3.Distance(transform.position, pathCreator4.path.GetPointAtDistance(0)); if (distance2 < 0.5f) { pathIdx = 2; } } else if (pathIdx == 2) { distanceTravelled3 += speed * Time.deltaTime; transform.position = pathCreator4.path.GetPointAtDistance(distanceTravelled3); distance3 = Vector3.Distance(transform.position, pathCreator5.path.GetPointAtDistance(0)); if (distance3 < 0.5f) { pathIdx = 3; } } else if (pathIdx == 3) { distanceTravelled4 += speed * Time.deltaTime; transform.position = pathCreator5.path.GetPointAtDistance(distanceTravelled4); } }
/// Gets forward direction on path based on distance travelled. public Vector3 GetDirectionAtDistance(float dst, EndOfPathInstruction endOfPathInstruction = EndOfPathInstruction.Loop) { float progress = dst / length; return(GetDirection(progress, endOfPathInstruction)); }