2023-08-02 17:10:40 +00:00
|
|
|
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;
|
|
|
|
|
2023-08-03 01:03:08 +00:00
|
|
|
private void Start()
|
2023-08-02 17:10:40 +00:00
|
|
|
{
|
|
|
|
rb = GetComponent<Rigidbody>();
|
|
|
|
}
|
|
|
|
|
2023-08-03 01:03:08 +00:00
|
|
|
public void Update()
|
2023-08-02 17:10:40 +00:00
|
|
|
{
|
|
|
|
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<MeshFilter>().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));
|
|
|
|
}
|
2023-08-03 01:03:08 +00:00
|
|
|
}
|