- Home /
Problem with water floating script
I have this nice script to make water float up&down to simulate waves:
public class waterfloat : MonoBehaviour {
public float upoffset;
public float speed;
private float defaulty;
private int dir = 0;
// Use this for initialization
void Start () {
defaulty = this.transform.position.y;
}
// Update is called once per frame
void Update () {
Debug.Log (this.transform.position.y.ToString());
Vector3 currentpos = this.transform.position;
switch (dir) {
case 0: {
Vector3 targetPos = currentpos;
targetPos.y = defaulty+upoffset;
currentpos=Vector3.Lerp(currentpos,targetPos,speed*Time.deltaTime);
this.transform.position = currentpos;
if (currentpos.y>=targetPos.y) {dir =1;}
break;
}
case 1: {
Vector3 targetPos = currentpos;
targetPos.y = defaulty;
currentpos=Vector3.Lerp(currentpos,targetPos,speed*Time.deltaTime);
this.transform.position = currentpos;
if (currentpos.y<=targetPos.y) {dir =0;}
break;
}
}
}
}
The problem is that water floats up but then fails to float back down. Can you help me with that issue?
Answer by robertbu · Oct 11, 2013 at 07:42 PM
Your issue is the way you are using Lerp(). Lerp() is designed to have the last parameter (usually referred to as 't') go from 0 to 1. A lot of code you see posted on this list use it instead as you do where 't' is a small value, and each frame the starting position is updated. The result is that each frame the the object is moved the same percentage (give or take depending on deltaTime) towards that goal. But since the distance is shrinking, the same percentage produces ever smaller moves. The result is an eased movement towards the end. An artifact if this code is that never really reaches the destination. Think about old problem. If I cover half the distance to my goal each day, how long will it take me to reach the goal? Never. Your code depends on the object reaching the goal for you to change 'dir' and reverse direction.
You can patch your current code by adding a threshold. So line 21 might become:
if (currentpos.y>=targetPos.y-0.1) {dir =1;}
and line 29 becomes:
if (currentpos.y<=targetPos.y+0.1) {dir =0;}
But with wave motion, I think you really want something that is sinusoidal. Here is a bit of code to demonstrate. It assumes that the water starts in the mid position and that offset is the the offset up and down (i.e. total travel is 2 * offset):
using UnityEngine;
using System.Collections;
public class UpDown : MonoBehaviour {
public float offset = 1.5f;
public float speed = 0.8f;
private Vector3 startPos;
void Start() {
startPos = transform.position;
}
void Update() {
transform.position = startPos + Vector3.up * Mathf.Sin (Time.time * speed);
}
}
Answer by Tomer-Barkan · Oct 11, 2013 at 07:45 PM
Your problem is here:
currentpos=Vector3.Lerp(currentpos,targetPos,speed*Time.deltaTime);
You will never reach your target position, because speed * Time.deltaTime is always smaller than 1, and henve you will always choose some point between the currentpos and targetPos, you will never get all the way to targetPos (like if you advance half the distance to your target every second, you will never get to the target because you always move just half the remaining distance).
You should try this instead:
currentpos += (targetPos - currentpos).normalized * speed * Time.deltaTime;
Here I calculate the vector that will bring the current position to the target position (targetPos - currentPos), and I normalize it to make it magnitude one, and then I multiply it by the speed and time. This will make a small vector pointing from the current position to the target position. If I add this to the current position, it will move the current position a bit towards the target, but this bit is only dependent on the speed, not on the distance remaining to the target.
Your answer
Follow this Question
Related Questions
How do you get an object to level out when floating? 0 Answers
Floating object on the water and stand on it. 2 Answers
Making an object float in water 2 Answers
How do i make floating ship ? 0 Answers
Simulating Water not working.. 1 Answer