void Start() { rb = GetComponentInChildren <Rigidbody> (); rb.velocity += transform.forward * speed; trackInformer = GameObject.Find("TrackInformer").GetComponent <TrackInformer>(); TrackInformer.TrackInfo trackInfo = trackInformer.GetTrackInfo(transform.position, transform.right, transform.up, 100f); height = Mathf.Max(trackInfo.distanceToGround, 1.0f); AudioSource.PlayClipAtPoint(laserSound, transform.position); if (originShip.name == "Victor") { GetComponentInChildren <Light>().color = Color.red; } if (originShip.name == "Oscar") { GetComponentInChildren <Light>().color = Color.green; } if (originShip.name == "Cristina") { GetComponentInChildren <Light>().color = Color.magenta; } if (originShip.name == "Sanic") { GetComponentInChildren <Light>().color = Color.blue; } Destroy(gameObject, 5f); }
void Update() { TrackInformer.TrackInfo trackInfo = trackInformer.GetTrackInfo(transform.position, transform.right, transform.up, 100f); if (trackInfo.overTheTrack) { transform.forward = Vector3.Cross(transform.right, trackInfo.normal); transform.position = trackInfo.groundPoint + trackInfo.normal * height; } }
void FixedUpdate() { base.Update(); if (currentState == ShipInputController.State.Moving) { info = trackInformer.GetTrackInfo(transform.position, transform.right, transform.up, 100f); if (!info.overTheTrack) { return; } List <Waypoint> waypointsAfter = trackInformer.GetNPointsAfter(transform.position, numWPForward); if (waypointsAfter.Count < numWPForward) { return; } Waypoint targetAfterWP = waypointsAfter[numWPForward - 1]; Waypoint targetAfterAfterWP = waypointsAfter[numWPForward - 2]; Vector3 targetAfter = targetAfterWP.transform.position; Vector3 targetAfterAfter = targetAfterAfterWP.transform.position; Vector3 forwardedPosition = Vector3.Lerp(targetAfter, targetAfterAfter, 0.5f); if (Vector3.Distance(targetAfter, targetAfterAfter) < 0.5f) { targetAfterAfterWP = trackInformer.GetPointAfter(targetAfterAfterWP); targetAfterAfter = targetAfterAfterWP.transform.position; } Vector3 v1 = Vector3.ProjectOnPlane(targetAfterAfter - targetAfter, Vector3.up).normalized; Vector3 v2 = Vector3.ProjectOnPlane(targetAfter - forwardedPosition, Vector3.up).normalized; float nextWPOrthogonality = Vector3.Dot(v1, v2); Vector3 target = nextWPOrthogonality < nextWPOrthogonalityTresh ? targetAfterAfter : targetAfter; Waypoint targetWP = nextWPOrthogonality < nextWPOrthogonalityTresh ? targetAfterAfterWP : targetAfterWP; target += lane * targetWP.transform.right; Vector3 direction = target - transform.position; direction.y = transform.forward.y; direction.Normalize(); //Debug.DrawLine(transform.position, transform.position + v1 * 5f, Color.red, 0.0f, false); //Debug.DrawLine(transform.position, transform.position + v2*5f, Color.green, 0.0f, false); Debug.DrawLine(transform.position, target, Color.blue, 0.0f, false); RaycastHit hitInfo; if (Physics.Raycast(transform.position, transform.right, out hitInfo, 999.9f, shipPhysicsController.trackLayer)) { rb.AddForce(hitInfo.normal * 8.0f * (1.0f / hitInfo.distance)); } if (Physics.Raycast(transform.position, -transform.right, out hitInfo, 999.9f, shipPhysicsController.trackLayer)) { rb.AddForce(hitInfo.normal * 8.0f * (1.0f / hitInfo.distance)); } float prevTurn = shipPhysicsController.GetTurn(); float s = Mathf.Sign(AngleAroundAxis(transform.forward, direction, transform.up)); float endTurn = s * (1.0f - Vector3.Dot(direction, transform.forward)) * 15.0f; float turn = Mathf.Lerp(prevTurn, endTurn, Time.fixedDeltaTime * turnSmoothing); shipPhysicsController.SetTurn(turn); shipPhysicsController.SetThrust(1f); } }
void FixedUpdate() { RaycastHit hit; if (Physics.Raycast(transform.position, Vector3.down, out hit, 999.9f, trackLayer)) { Vector3 fixedPositionUpwards = new Vector3( transform.position.x, Mathf.Max(hit.point.y + 1.0f, transform.position.y), transform.position.z); transform.position = fixedPositionUpwards; } // Hover force for (int i = 0; i < hoverPoints.Length; ++i) { GameObject hoverPoint = hoverPoints[i]; Ray ray = new Ray(hoverPoint.transform.position, -transform.up); //Debug.DrawRay (ray.origin, ray.direction, Color.red, 0f); if (Physics.Raycast(ray, out hit, hoverHeight, trackLayer)) { float proportionalHeight = (hoverHeight - hit.distance) / hoverHeight; rb.AddForceAtPosition(transform.up * hoverForce * proportionalHeight, hoverPoint.transform.position); //Debug.DrawRay (ray.origin, ray.direction, Color.green, 0f); } } // Forward if (thrust != 0) { rb.AddForce(transform.forward * thrust, ForceMode.Acceleration); } // Turn if (turn != 0) { rb.AddRelativeTorque(transform.up * turn * turnStrength); } // Correction of track boundaries with forces if (GetComponent <ShipInputController>().enabled) { TrackInformer.TrackInfo trackInfo = trackInformer.GetTrackInfo(transform.position, transform.right, transform.up, hoverHeight); if (trackInfo.overTheTrack) { Waypoint wpBefore = trackInformer.GetNPointsBefore(transform.position, 1)[0]; Waypoint wpAfter = trackInformer.GetNPointsAfter(transform.position, 1)[0]; Vector3 trackForward = (wpAfter.transform.position - wpBefore.transform.position).normalized; Vector3 trackUp = trackInfo.overTheTrack ? trackInfo.normal : transform.up; Vector3 trackRight = Vector3.Cross(trackForward, trackUp); RaycastHit hitInfo; if (Physics.Raycast(transform.position, trackRight, out hitInfo, 999.9f, trackBoundaryLayer)) { float distFactor = (1.0f / hitInfo.distance); rb.AddForce(hitInfo.normal * 400.0f * Mathf.Pow(distFactor, 5.0f)); } if (Physics.Raycast(transform.position, -trackRight, out hitInfo, 999.9f, trackBoundaryLayer)) { float distFactor = (1.0f / hitInfo.distance); rb.AddForce(hitInfo.normal * 400.0f * Mathf.Pow(distFactor, 5.0f)); } } } // if (hasFinishedTheRace) { thrust *= 0.97f; //rb.AddForce(-transform.forward * 5.0f); // Brake } }