private IEnumerator MoveTowards(int nextPitStop, bool isLastPitStop, bool direction) { isMoving = true; float t = 0.0f, initialTime = currentTimeOnPath; float finalTime = pitStops[nextPitStop]; if (isLastPitStop) { if (direction == FORWARD) { initialTime -= 1.0f; } else { finalTime -= 1.0f; } } while (t < 1.0f) { currentTimeOnPath = Mathf.SmoothStep(initialTime, finalTime, t); transform.position = path.GetPointAtTime(currentTimeOnPath); t += Time.deltaTime / travelDuration; yield return(new WaitForEndOfFrame()); } currentTimeOnPath = pitStops[nextPitStop]; currentPitStop = nextPitStop; isMoving = false; }
private void Update() { if (path != null) { timeSincePathSet += Time.deltaTime / time; transform.position = path.GetPointAtTime(curve.Evaluate(Mathf.Clamp01(timeSincePathSet))); if (timeSincePathSet >= 1f) { Destroy(gameObject); OnPathCompleted?.Invoke(); } } }
// Start is called before the first frame update void Start() { path = robotPath.path; if (pitStops.Length > 0) { transform.position = path.GetPointAtTime(pitStops[0]); currentTimeOnPath = pitStops[0]; currentPitStop = 0; } else { Debug.LogError("No pit stops set!"); } }
IEnumerator Following(VertexPath path) { Vector3 lastPoint = path.GetPointAtTime(1f, EndOfPathInstruction.Stop); float befDist = 0.5f; if (stopped == false) { while (DistanceXZ(transform.position, lastPoint) > 0.5f) { float distance = Vector3.Distance(this.transform.position, lastPoint); if (distance < stopDistance) { stopped = true; body.velocity = Vector3.zero; } body.velocity = Vector3.ClampMagnitude(body.velocity, maxSpeed); body.AddForce(transform.forward * followSpeed, ForceMode.VelocityChange); float dist = path.GetClosestDistanceAlongPath(transform.position); if (dist < befDist) { dist = befDist; } else { befDist = dist; } Vector3 nextPoint = path.GetPointAtDistance(dist + followSpeed, EndOfPathInstruction.Stop); Vector3 diff = nextPoint - transform.position; diff.y = 0; diff.Normalize(); Debug.DrawLine(transform.position, nextPoint, Color.red); Debug.DrawRay(transform.position, diff, Color.gray); float angle = Mathf.Atan2(diff.x, diff.z) * Mathf.Rad2Deg; transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.AngleAxis(angle, Vector3.up), rotateSpeed * Time.fixedDeltaTime); yield return(new WaitForFixedUpdate()); } body.velocity = Vector3.zero; } }
// Update is called once per frame void Update() { // if the ball or the trail hits the end point, destory them if (trail != null && trail.transform.position == path.GetPointAtTime(1, EndOfPathInstruction.Stop)) { Destroy(trail, 3); } if (ball == null || ball.transform.position == path.GetPointAtTime(1, EndOfPathInstruction.Stop)) { if (ball != null && survival) { if (ball.tag != "Bomb" && scorekeeper.caught) { scorekeeper.caught = false; } else { scorekeeper.lives--; } } // the ball can be null if it is caught and destoryed if (ball != null) { Destroy(ball); } Destroy(startHole); Destroy(endHole); Destroy(this.gameObject); } travelDst += Time.deltaTime * speed; if (trail != null) { trail.transform.position = path.GetPointAtDistance(travelDst, EndOfPathInstruction.Stop); } float prevDelay = delay; delay -= Time.deltaTime; if (delay < 0) { // if this is the first time delay became less than 0, then instantiate the ball if (prevDelay >= 0) { ball = Instantiate(ball, path.GetPointAtDistance(0), path.GetRotationAtDistance(0)); spawned = true; // mark end of path since ball.transform.position is not accurate ball.GetComponent <Explosion>().end = this.end; if (random == 0) { Instantiate(GameObject.Find("AudioManager").GetComponent <AudioManager>().bombSpawn, start, Quaternion.identity); } else { Instantiate(GameObject.Find("AudioManager").GetComponent <AudioManager>().spawn, start, Quaternion.identity); } } if (ball != null) { ball.transform.position = path.GetPointAtDistance(-delay * speed, EndOfPathInstruction.Stop); ball.transform.rotation = path.GetRotationAtDistance(-delay * speed, EndOfPathInstruction.Stop); } } }