示例#1
0
    void Awake()
    {
        // Get wave properties from water
        steepness  = water.GetWaveSteepness();
        wavelength = water.GetWaveLength();
        speed      = water.GetWaveSpeed();
        directions = water.GetWaveDirections();

        // Get rigidbody
        rb            = GetComponent <Rigidbody>();
        rb.useGravity = false;

        effectorProjections = new Vector3[effectors.Length];
        for (int i = 0; i < effectors.Length; i++)
        {
            effectorProjections[i] = effectors[i].position;
        }
    }
示例#2
0
    void FixedUpdate()
    {
        int effectorAmount = effectors.Length;

        for (int i = 0; i < effectorAmount; i++)
        {
            Vector3 effectorPosition = effectors[i].position;

            effectorProjections[i]   = effectorPosition;
            effectorProjections[i].y = water.transform.position.y + GerstnerWaveDisplacement.GetWaveDisplacement(effectorPosition, water.GetWaveSteepness(), water.GetWaveLength(), water.GetWaveSpeed(), water.GetWaveDirections()).y;

            // gravity
            rb.AddForceAtPosition(m_Gravity / effectorAmount, effectorPosition, ForceMode.Acceleration);

            float waveHeight     = effectorProjections[i].y;
            float effectorHeight = effectorPosition.y;

            if (effectorHeight < waveHeight) // submerged
            {
                float submersion = Mathf.Clamp01(waveHeight - effectorHeight) / objectDepth;
                float buoyancy   = Mathf.Abs(m_Gravity.y) * submersion * strength;

                // buoyancy
                rb.AddForceAtPosition(Vector3.up * buoyancy, effectorPosition, ForceMode.Acceleration);

                // drag
                rb.AddForce(-rb.velocity * velocityDrag * Time.fixedDeltaTime, ForceMode.VelocityChange);

                // torque
                rb.AddTorque(-rb.angularVelocity * angularDrag * Time.fixedDeltaTime, ForceMode.Impulse);
            }
        }
    }