Flotabilidad en agua - Buoyancy in water

Hola soy un estudiante y estoy aprendiendo un poco más sobre scripting en unity y quisiera saber si alguien me podría explicar este script y como lo puedo aplicar al First Person Controller de unity.

Hello I am a student and I’m learning a little more about scripting in unity and I wonder if someone could explain me this script and as I apply to the First Person Controller of unity.

#pragma strict

var waterlevel : float = 4;
var floatHeight : float = 2;
var bounceDamp : float = 0.05;
var buoyancyCentreOffset : Vector3;


private var forceFactor : float;
private var actionPoint : Vector3;
private var uplift : Vector3;
   
function Update()
{
        actionPoint = transform.position + transform.TransformDirection(buoyancyCentreOffset);
        forceFactor = 1f - ((actionPoint.y - waterlevel) / floatHeight);
       
        if (forceFactor > 0f)
        {
            uplift = -Physics.gravity * (forceFactor - rigidbody.velocity.y * bounceDamp);
            rigidbody.AddForceAtPosition(uplift, actionPoint);
        }
}

He sacado este código de un vídeo tutorial en youtube de PSD “creating games”.
Quiero aplicarlo para que el FPC pueda flotar cuando pulse espacio y no salte si no que flote, gracias por la atención prestada.

I taken this code from a tutorial video on youtube of PSD “creating games”.
I want to apply for the FPC to float when I press space and do not jump but it afloat, thanks for your attention and excuse me if my english isn’t good

line 15: get the “action point” (center of buoyancy) of your character

line 16: calculate the amount of up force depending on how deeply you are submerged.

line 20: calculate the uplift

line 21: apply the uplift to the rigid body

To use this effectively, there may be considerations with interoperating it with the FPS script.

You may find that you need to either:

1 - disable the FPS when you are floating and put another control in

2 - limit the jump functionality

3 - force the FPS script to detect you are landed.

This is dependent on what FPS script you are using.

I have not looked at the video that you linked.

1 Like

Tanks
The FPS is the First Person Controller default of the Character Controllers assets of Unity

Sorry the link is of the channel, This is the video

The biggest problem is that the First Person Controller Unity uses a Character Controller physics and the buoyancy script uses RigidBody, I don’t know how to do a First Person Controller that use RigidBody.