void EmitWaves() { int particleDiff = waveParticles.maxParticles - waveParticles.particleCount; ParticleSystem.Particle[] particles = new ParticleSystem.Particle[waveParticles.particleCount]; waveParticles.GetParticles(particles); for (int i = 0; i < particles.Length; i++) { ParticleSystem.Particle particle = particles[i]; particles[i].position = new Vector3(particle.position.x, UTW.getHeightByPos(particle.position), particle.position.z); } waveParticles.SetParticles(particles, particles.Length); if (particleDiff > 0) { Vector3 campos = Camera.main.transform.position; for (int i = 0; i < particleDiff; i++) { Vector2 randPos = Random.insideUnitCircle.normalized * Random.Range(100f, UTW.config.size / 2f); Vector2 posC = randPos + new Vector2(campos.x, campos.z); Vector3 pos = new Vector3(posC.x, UTW.getHeightByPos(posC), posC.y); waveParticles.Emit(pos, Vector3.zero, Random.Range(5f, 15f), Random.Range(1f, 2.5f), waveParticles.startColor); } } }
// Update is called once per frame void FixedUpdate() { if (isActive == true && (UTW != null || UTWC != null)) { foreach (Floater floater in floaters) { Vector3 actionPoint = floater.buoyancePoint.position; float targetYPos = 0; if (UTW != null) { targetYPos = UTW.getHeightByPos(actionPoint); } else if (UTWC != null) { targetYPos = UTWC.getHeightByPos(actionPoint); } float forceFactor = targetYPos - actionPoint.y; forceFactor = Mathf.Min(forceFactor, 2.25f); if (forceFactor > 0f) { float buoyance = forceFactor * forceFactor * floater.buoyanceForce; Vector3 uplift = -Physics.gravity * buoyance; GetComponent <Rigidbody>().AddForceAtPosition(uplift, actionPoint); if (drawGizmos == true) { Debug.DrawLine(actionPoint, actionPoint + uplift / GetComponent <Rigidbody>().mass, Color.red); } } else if (forceFactor < 0f && forceFactor > -waterDownDragY) // Water resistance drag { float buoyance = (waterDownDragY - forceFactor) * floater.buoyanceForce; Vector3 uplift = Physics.gravity * buoyance; GetComponent <Rigidbody>().AddForceAtPosition(uplift, actionPoint); if (drawGizmos == true) { Debug.DrawLine(actionPoint, actionPoint + uplift / GetComponent <Rigidbody>().mass, Color.yellow); } } } Vector3 pseudoPos = new Vector3(origin.x, transform.localPosition.y, origin.z); if (sticky == true) { Vector3 newPos = Vector3.Lerp(transform.localPosition, pseudoPos, Time.deltaTime * 1f); transform.localPosition = newPos; } if (emitParticles == true) { if (floaterParticles != null) { StartCoroutine(particleEmit()); } } } }
// Update is called once per frame void Update() { int particleDiff = GetComponent <ParticleSystem>().maxParticles - GetComponent <ParticleSystem>().particleCount; if (snapToWater == true) { ParticleSystem.Particle[] particles = new ParticleSystem.Particle[GetComponent <ParticleSystem>().particleCount]; GetComponent <ParticleSystem>().GetParticles(particles); for (int i = 0; i < particles.Length; i++) { ParticleSystem.Particle particle = particles[i]; if (UTW != null) { particles[i].position = new Vector3(particle.position.x, UTW.getHeightByPos(particle.position), particle.position.z); } else if (UTWC != null) { particles[i].position = new Vector3(particle.position.x, UTWC.getHeightByPos(particle.position), particle.position.z); } } GetComponent <ParticleSystem>().SetParticles(particles, particles.Length); } if (particleDiff > 0) { Vector3 campos = Camera.main.transform.position; for (int i = 0; i < particleDiff; i++) { Vector2 randPos = Random.insideUnitCircle.normalized * Random.Range(100f, radius / 2f); Vector2 posC = randPos + new Vector2(campos.x, campos.z); Vector3 pos = Vector3.zero; if (UTW != null) { pos = new Vector3(posC.x, UTW.getHeightByPos(posC), posC.y); } else if (UTWC != null) { pos = new Vector3(posC.x, UTWC.getHeightByPos(posC), posC.y); } GetComponent <ParticleSystem>().Emit(pos, new Vector3(0, Random.Range(velocity.x, velocity.y), 0), Random.Range(size.x, size.y), Random.Range(filetime.x, filetime.y), GetComponent <ParticleSystem>().startColor); } } }
void LateUpdate() { // Early out if we don't have a target if (!target) { return; } // Litte camera effect to enhance feeling of speed GetComponent <Camera>().fieldOfView = Mathf.Min(70f, 50f + (20f * Mathf.Sqrt(Mathf.Max(0.001f, boatController.speed.z) / boatController.topSpeed))); // Calculate the current rotation angles float wantedRotationAngle = target.eulerAngles.y; float wantedHeight = target.position.y + height; float currentRotationAngle = transform.eulerAngles.y; float currentHeight = transform.position.y; // Damp the rotation around the y-axis currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime); // Damp the height currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime); // Convert the angle into a rotation Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0); // Set the position of the camera on the x-z plane to: // distance meters behind the target Vector3 transPos = target.position; transPos -= currentRotation * Vector3.forward * distance; // Set the height of the camera if (UTW != null) { transPos.y = Mathf.Max(currentHeight, UTW.getHeightByPos(transPos) + 3f); } else if (UTWC != null) { transPos.y = Mathf.Max(currentHeight, UTWC.getHeightByPos(transPos) + 3f); } transform.position = transPos; // Always look at the target transform.LookAt(target.TransformPoint(new Vector3(0f, 0f, offset))); }
// Update is called once per frame void Update() { particles = new ParticleSystem.Particle[GetComponent <ParticleSystem>().particleCount]; GetComponent <ParticleSystem>().GetParticles(particles); for (int i = 0; i < particles.Length; i++) { ParticleSystem.Particle particle = particles[i]; float h = 0; if (UTW != null) { h = UTW.getHeightByPos(particle.position); } else if (UTWC != null) { h = UTWC.getHeightByPos(particle.position); } if (particle.position.y - h > 2f) { particles[i].lifetime = 0f; } particles[i].position = new Vector3(particle.position.x, h, particle.position.z); } GetComponent <ParticleSystem>().SetParticles(particles, particles.Length); }
void FixedUpdate() { // Do note, top speed is estimated! topSpeed = 1.025f * forwardForce / (GetComponent <Rigidbody>().mass *(forwardDrag / GetComponent <Rigidbody>().mass + GetComponent <Rigidbody>().drag)); speed = transform.InverseTransformDirection(GetComponent <Rigidbody>().velocity); bool inWater = false; float totalForceFactor = 0f; foreach (Floater floater in floaters) { Vector3 actionPoint = floater.buoyancePoint.position; float targetYPos = 0f; if (UTW != null) { targetYPos = UTW.getHeightByPos(actionPoint); } else if (UTWC != null) { targetYPos = UTWC.getHeightByPos(actionPoint); } float forceFactor = targetYPos - actionPoint.y; forceFactor = Mathf.Min(forceFactor, maxUplift); float speedFactor = 0.5f + (speed.z / topSpeed); if (forceFactor > 0f) { totalForceFactor += forceFactor; float buoyance = forceFactor * forceFactor * floater.buoyanceForce; Vector3 uplift = -Physics.gravity * buoyance * speedFactor; uplift.y -= speed.y * forwardDrag; GetComponent <Rigidbody>().AddForceAtPosition(uplift, actionPoint); if (drawGizmos == true) { Debug.DrawLine(actionPoint, actionPoint + uplift / GetComponent <Rigidbody>().mass, Color.red); } inWater = true; } else if (forceFactor < 0f && forceFactor > -waterDownDragY) // Water resistance stickyness { float buoyance = (waterDownDragY - forceFactor) * floater.buoyanceForce; Vector3 uplift = Physics.gravity * buoyance * speedFactor; uplift.y -= speed.y * forwardDrag; GetComponent <Rigidbody>().AddForceAtPosition(uplift, actionPoint); if (drawGizmos == true) { Debug.DrawLine(actionPoint, actionPoint + uplift / GetComponent <Rigidbody>().mass, Color.yellow); } inWater = true; } } GetComponent <Rigidbody>().angularDrag = 1f + (totalForceFactor / 3.5f); if (canControl == true && inWater == true) { GetComponent <Rigidbody>().AddRelativeForce(new Vector3(-speed.x * sideDrag, 0f, 0f)); GetComponent <Rigidbody>().AddRelativeForce(new Vector3(0f, 0f, -speed.z * forwardDrag)); } GetComponent <Rigidbody>().AddRelativeForce(new Vector3(0f, -speed.z * downForce, 0f)); vertical = 0; horizontal = 0; vertical = Input.GetAxis("Vertical"); horizontal = Input.GetAxis("Horizontal") * rotationSpeed; if (Input.GetKey(KeyCode.LeftShift)) { vertical *= 2f; } if (vertical < 0f) { vertical *= 0.1f; } if (inWater == false) { vertical *= 0.125f; horizontal *= 0.125f; if (trail.isPlaying == true) { trail.Stop(); } } else { if (trail.isPlaying == false) { trail.Play(); } } GetComponent <Rigidbody>().AddRelativeForce(vertical * forwardForce * Vector3.forward); transform.Rotate(horizontal * Vector3.up); // Little boost for if we go slow if (vertical > 0) { vertical += 1f - Mathf.Max(0, speed.z) / topSpeed; } // Particle handling for the boat particles = new ParticleSystem.Particle[trail.particleCount]; trail.GetParticles(particles); for (int i = 0; i < particles.Length; i++) { ParticleSystem.Particle particle = particles[i]; float h = 0f; if (UTW != null) { h = UTW.getHeightByPos(particle.position); } else if (UTWC != null) { h = UTWC.getHeightByPos(particle.position); } if (particle.position.y - h > 1f) { particles[i].lifetime = 0f; } particles[i].position = new Vector3(particle.position.x, h, particle.position.z); } trail.SetParticles(particles, particles.Length); }