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 floater script miscalculates sea height

Discussion in 'Prefabs' started by santilapro13, Mar 2, 2024.

  1. santilapro13

    santilapro13

    Joined:
    May 8, 2021
    Posts:
    1
    In this script the height of the sea is calculated from another script that generates this wave, the problem is that the script works on prefab objects, but when it is an instantiated prefab that is created inside an empty, it is not calculated well. I need a way to solve this...

    script:
    Code (CSharp):
    1. public class Floater : MonoBehaviour
    2. {
    3.     public Rigidbody rb;
    4.     public float depthBeforeSubmerged = 1f;
    5.     public float displacementAmount = 3f;
    6.     public int floaterCount = 1;
    7.     public float waterDrag = 0.99f;
    8.     public float waterAngularDrag = 0.5f;
    9.  
    10.     private WaveManager waveManager; // Referencia al script que maneja la onda del mar
    11.     private Vector3 oceanScale; // Escala del prefab del océano
    12.  
    13.     void Start()
    14.     {
    15.         waveManager = FindObjectOfType<WaveManager>(); // Busca un objeto con el script WaveManager en la escena
    16.  
    17.         // Obtén la escala del prefab del océano
    18.         oceanScale = waveManager.gameObject.transform.localScale;
    19.     }
    20.  
    21.     private void FixedUpdate()
    22.     {
    23.         // Escala la posición del floater para que coincida con la escala del océano
    24.         Vector3 scaledPosition = new Vector3(transform.position.x / oceanScale.x, transform.position.y, transform.position.z / oceanScale.z);
    25.  
    26.         // Obtener la altura de la onda del mar en la posición actual del floater (solo en el eje X)
    27.         float waveHeight = waveManager.GetWaveHeight(scaledPosition.x);
    28.  
    29.         // Aplicar fuerza de gravedad
    30.         rb.AddForceAtPosition(Physics.gravity / floaterCount, transform.position, ForceMode.Acceleration);
    31.  
    32.         // Verificar si el floater está sumergido en el agua
    33.         if (transform.position.y < waveHeight)
    34.         {
    35.             // Calcular la fuerza de flotación
    36.             float displacementMultiplier = Mathf.Clamp01((waveHeight - transform.position.y) / depthBeforeSubmerged) * displacementAmount;
    37.  
    38.             // Aplicar fuerza de flotación hacia arriba
    39.             rb.AddForceAtPosition(new Vector3(0f, Mathf.Abs(Physics.gravity.y) * displacementMultiplier, 0f), transform.position, ForceMode.Acceleration);
    40.  
    41.             // Aplicar arrastre al objeto
    42.             rb.AddForce(displacementMultiplier * -rb.velocity * waterDrag * Time.fixedDeltaTime, ForceMode.VelocityChange);
    43.             rb.AddTorque(displacementMultiplier * -rb.angularVelocity * waterAngularDrag * Time.fixedDeltaTime, ForceMode.VelocityChange);
    44.         }
    45.     }