Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by darkhog · Oct 11, 2013 at 05:47 PM · waterfloating

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3

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);    
     }
 }

 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tomer-Barkan · Oct 11, 2013 at 08:08 PM 0
Share

This seems so much simpler :)

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges