public void AddToRoad(Road r) { float angle = 90 - r.transform.eulerAngles.y; angle = Mathf.Repeat(angle, 360); Vector3 joinPoint = r.Joint2; Vector3 offset = Polar.FromPolar(Mathf.Deg2Rad * angle, RadiusOuter, Vector3.zero); Vector3 pos = joinPoint + offset; pos.y = 0; transform.position = pos; DisableBoundary(-r.transform.forward, false); _roads.Add(r); // Spawn AIs int spawns = Random.Range(_minMaxAiSpawns.x, _minMaxAiSpawns.y) + HighwayManagement.SpawnIncrement; for (int i = 0; i < spawns; ++i) { var ai = Instantiate <Ai>(_aiPrefabs[Random.Range(0, _aiPrefabs.Length)]); float theta = Random.Range(0, Mathf.PI * 2); ai.transform.position = Polar.FromPolar(theta, RadiusOuter, transform.position); ai.transform.forward = Polar.PolarForward(theta, RadiusOuter); } }
public void AddToRoundabout(Roundabout r, float angleDeg) { angleDeg = Mathf.Repeat(angleDeg, 360); transform.eulerAngles = new Vector3(0, angleDeg, 0); Vector3 target = Polar.FromPolar(angleDeg * Mathf.Deg2Rad, r.RadiusOuter, r.transform.position); transform.position = r.transform.position; _r1 = r; }
public static void OnPlayerDie(Player.DeathType type) { if (_i._firstRoundabout) { _i.StartCoroutine(_Respawn()); } else if (_i._lives == 0) { _i._dead = true; } else { --_i._lives; _i.StartCoroutine(_Respawn()); } if (_i._dead) { _i._onDie.Invoke(); if (!Roundabout.Active.OffsetCamera) { var camEnd = Camera.main.transform.position + Vector3.right * 10; _i.StartCoroutine(MoveCamera(_i._camDeathTime, camEnd, () => { DeathsUi.OnDeath(type, true); })); } else { DeathsUi.OnDeath(type, true); } } else { _i._onCrash.Invoke(); DeathsUi.OnDeath(type, false); } IEnumerator _Respawn() { yield return(new WaitForSeconds(_i._respawnTime)); Destroy(Player.Instance.gameObject); yield return(null); var player = Instantiate <Player>(_i._playerPrefab); player.transform.position = Polar.FromPolar(0, Roundabout.Active.RadiusInner, Roundabout.Active.transform.position); player.transform.forward = Polar.PolarForward(0, Roundabout.Active.RadiusInner); } }
protected virtual void OnTriggerEnter(Collider c) { if (!_currentRoad && !_currentRoundabout) { // Just spawned if (_canStartOnRoad) { _currentRoad = c.GetComponent <Road>(); } _currentRoundabout = c.GetComponent <Roundabout>(); if (_currentRoad) { transform.position = _nearestEntry; transform.forward = _currentRoadFwd = _currentRoad.GetForward(transform.position); } else if (_currentRoundabout) { _theta = Polar.ToPolar(transform.position, _currentRoundabout.transform.position); transform.position = Polar.FromPolar(_theta, _currentRoundabout.RadiusOuter, _currentRoundabout.transform.position); transform.forward = Polar.PolarForward(_theta, _currentRoundabout.RadiusOuter); } } else if (_currentRoad) { var r = c.GetComponent <Roundabout>(); if (r) { _currentRoundabout = r; _theta = Polar.ToPolar(transform.position, _currentRoundabout.transform.position); Vector3 target = Polar.FromPolar(_theta + _entryOffsetRadians, CurrentRadius, _currentRoundabout.transform.position); Vector3 fwd = Polar.PolarForward(_theta + _entryOffsetRadians, CurrentRadius); Transfer(target, fwd, () => { _currentRoad = null; _theta = _theta + _entryOffsetRadians; }); OnEnteredRoundabout(r); } } }
void LateUpdate() { if (_transfer == null) { float distance = Speed * Time.deltaTime; if (_currentRoad) { transform.position += _currentRoadFwd * distance; } else if (_currentRoundabout) { // c = 2pr // l = theta r // theta = l/r float dTheta = distance / CurrentRadius; _theta += dTheta; _theta = Mathf.Repeat(_theta, Mathf.PI * 2); transform.position = Polar.FromPolar(_theta, CurrentRadius, _currentRoundabout.transform.position); transform.forward = Polar.PolarForward(_theta, CurrentRadius); } } }
protected void Transfer(Vector3 endPoint, Vector3 endFwd, System.Action onTransferred = null) { _transfer = StartCoroutine(_Transfer()); IEnumerator _Transfer() { Vector3 startFwd = transform.forward; Vector3 startPoint = transform.position; endPoint += endFwd * (Vector3.Distance(startPoint, endPoint) / 2); // Get circle Vector3 startRight = transform.right; Vector3 endRight = Vector3.Cross(endFwd, Vector3.up); Vector2 ctr2 = Vector2.zero; Vector3 p0 = startPoint; Vector3 p1 = startPoint + (startRight * 100); Vector3 p2 = endPoint; Vector3 p3 = endPoint - (endRight * 100); // Try every f*****g permutation if (!LineIntersection(XZ(p0), XZ(p1), XZ(p2), XZ(p3), ref ctr2)) { p3 = endPoint + (endRight * 100); if (!LineIntersection(XZ(p0), XZ(p1), XZ(p2), XZ(p3), ref ctr2)) { p1 = startPoint - (startRight * 100); if (!LineIntersection(XZ(p0), XZ(p1), XZ(p2), XZ(p3), ref ctr2)) { p3 = endPoint - (endRight * 100); LineIntersection(XZ(p0), XZ(p1), XZ(p2), XZ(p3), ref ctr2); } } } Vector3 centre = new Vector3(ctr2.x, 0, ctr2.y); float radius = Vector3.Distance(startPoint, centre); float dTheta = Vector3.SignedAngle(startFwd, endFwd, -Vector3.up) * Mathf.Deg2Rad; float arcLength = Mathf.Abs(dTheta * radius); float startTheta = Mathf.Repeat(Polar.ToPolar(transform.position, centre), Mathf.PI * 2); float endTheta = startTheta + dTheta; float time = arcLength / Speed; // Debug.DrawLine(p0, p1, Color.magenta, 10f); // Debug.DrawLine(p2, p3, Color.magenta, 10f); // Debug.DrawRay(startPoint, startFwd*100, Color.yellow, 10f); // Debug.DrawRay(endPoint, endFwd*100, Color.yellow, 10f); // Debug.DrawLine(startPoint, centre, Color.green, 10f); _tp0 = p0; _tp1 = p1; _tp2 = p2; _tp3 = p3; _tStart = startPoint; _tsFwd = startFwd; _tEnd = endPoint; _teFwd = endFwd; _tCtr = centre; _tRad = radius; float t = 0; while (t < time) { float p = t / time; float theta = Mathf.Lerp(startTheta, endTheta, p); theta = Mathf.Repeat(theta, Mathf.PI * 2); Vector3 circlePos = Polar.FromPolar(theta, radius, centre); transform.position = Vector3.Lerp(circlePos, Vector3.Lerp(startPoint, endPoint, p), Mathf.Sqrt(p)); transform.rotation = Quaternion.Slerp(Quaternion.LookRotation(startFwd, Vector3.up), Quaternion.LookRotation(endFwd, Vector3.up), p); t += Time.deltaTime; yield return(null); } transform.position = endPoint; transform.forward = endFwd; onTransferred?.Invoke(); _transfer = null; } }