using System.Collections; using System.Collections.Generic; using UnityEngine; public class Floater : MonoBehaviour { public GerstnerWave waveGenerator; public float boatOffset = 0.0f; // new variable private Rigidbody rb; private void Start() { rb = GetComponent(); } public void Update() { float waveNumber = 2.0f * Mathf.PI / waveGenerator.waveLength; float phaseConstant = waveGenerator.speed * waveNumber; Vector3 position = rb.position; float dotProduct = Vector2.Dot(new Vector2(position.x, position.z), waveGenerator.direction); float wavePhase = waveNumber * dotProduct + phaseConstant * Time.time; // Calculate the new height float newY = waveGenerator.amplitude * Mathf.Sin(wavePhase) + boatOffset; // added offset here position.y = newY; rb.MovePosition(position); // get wave's normal Mesh mesh = waveGenerator.GetComponent().mesh; Vector3 normal = mesh.normals[0]; Quaternion targetRotation = Quaternion.FromToRotation(transform.up, normal) * transform.rotation; rb.MoveRotation(Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * waveGenerator.speed)); } }