Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Question On Floating Objects In New Water System HDRP

Discussion in 'Scripting' started by barbraz, Jan 13, 2024.

  1. barbraz

    barbraz

    Joined:
    Jun 3, 2018
    Posts:
    6
    so in the 2023 unity update they changed the water systems api and its alot more confusing now and now my code is broken and i tried converting it and it doesnt work nearly as good, can someone either tell me whats wrong with the script or help me understand the differences and how i can change it, ive been working on this for hours and ive gotten nowhere

    here is the Original Buoyancy Script:
    '
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering.HighDefinition;
    3.  
    4. public class Buoyancy : MonoBehaviour
    5. {
    6.     public Rigidbody rb;
    7.     public float depthBefSub;
    8.     public float displacementAmt;
    9.     public int floaters;
    10.  
    11.     public float waterDrag;
    12.     public float waterAngularDrag;
    13.     public WaterSurface water;
    14.     WaterSearchParameters Search;
    15.     WaterSearchResult SearchResult;
    16.  
    17.     private void FixedUpdate(){
    18.        
    19.         rb.AddForceAtPosition(Physics.gravity / floaters, transform.position, ForceMode.Acceleration);
    20.  
    21.         Search.startPosition = transform.position;
    22.  
    23.         water.surf(Search, out SearchResult);
    24.  
    25.         if (transform.position.y < SearchResult.height){
    26.  
    27.             float displacementMulti = Mathf.Clamp01(SearchResult.height - transform.position.y / depthBefSub) * displacementAmt;
    28.             rb.AddForceAtPosition(new Vector3(0f, Mathf.Abs(Physics.gravity.y) * displacementMulti, 0f), transform.position, ForceMode.Acceleration);
    29.             rb.AddForce(displacementMulti * -rb.velocity * waterDrag * Time.fixedDeltaTime, ForceMode.VelocityChange);
    30.             rb.AddTorque(displacementMulti * Time.fixedDeltaTime * waterAngularDrag * -rb.angularVelocity, ForceMode.VelocityChange);
    31.         }
    32.     }
    33. }

    And heres the attempted new Script:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering.HighDefinition;
    3.  
    4. public class Buoyancy : MonoBehaviour
    5. {
    6.     public Rigidbody rb;
    7.     public float depthBefSub;
    8.     public float displacementAmt;
    9.     public int floaters;
    10.  
    11.     public float waterDrag;
    12.     public float waterAngularDrag;
    13.     public WaterSurface water;
    14.     WaterSearchParameters Search;
    15.     WaterSearchResult SearchResult;
    16.  
    17.     private void FixedUpdate(){
    18.        
    19.         rb.AddForceAtPosition(Physics.gravity / floaters, transform.position, ForceMode.Acceleration);
    20.  
    21.         Search.startPositionWS = transform.position;
    22.  
    23.         water.ProjectPointOnWaterSurface(Search, out SearchResult);
    24.  
    25.         if (transform.position.y < SearchResult.projectedPositionWS.y){
    26.  
    27.             float displacementMulti = Mathf.Clamp01(SearchResult.projectedPositionWS.y - transform.position.y / depthBefSub) * displacementAmt;
    28.             rb.AddForceAtPosition(new Vector3(0f, Mathf.Abs(Physics.gravity.y) * displacementMulti, 0f), transform.position, ForceMode.Acceleration);
    29.             rb.AddForce(displacementMulti * -rb.velocity * waterDrag * Time.fixedDeltaTime, ForceMode.VelocityChange);
    30.             rb.AddTorque(displacementMulti * Time.fixedDeltaTime * waterAngularDrag * -rb.angularVelocity, ForceMode.VelocityChange);
    31.         }
    32.     }
    33. }
    i dont see how such a small change could be so drastic unless im missing something any help would be appreciated thanks
     
  2. barbraz

    barbraz

    Joined:
    Jun 3, 2018
    Posts:
    6
    Smaller objects work with the new attempted one but the bigger the object gets the crazier it acts.
    upload_2024-1-12_18-24-19.png \
    the boat is floating but the pier and spinning like crazy
     
  3. jaridmitchell

    jaridmitchell

    Joined:
    Sep 17, 2018
    Posts:
    1
    I am having the same issue! I will be sure to comment here if I figure anything out