Search Unity

Flotabilidad en agua - Buoyancy in water

Discussion in 'Scripting' started by Skarlos23, Jan 27, 2015.

  1. Skarlos23

    Skarlos23

    Joined:
    Jan 27, 2015
    Posts:
    3
    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.


    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var waterlevel : float = 4;
    4. var floatHeight : float = 2;
    5. var bounceDamp : float = 0.05;
    6. var buoyancyCentreOffset : Vector3;
    7.  
    8.  
    9. private var forceFactor : float;
    10. private var actionPoint : Vector3;
    11. private var uplift : Vector3;
    12.    
    13. function Update()
    14. {
    15.         actionPoint = transform.position + transform.TransformDirection(buoyancyCentreOffset);
    16.         forceFactor = 1f - ((actionPoint.y - waterlevel) / floatHeight);
    17.        
    18.         if (forceFactor > 0f)
    19.         {
    20.             uplift = -Physics.gravity * (forceFactor - rigidbody.velocity.y * bounceDamp);
    21.             rigidbody.AddForceAtPosition(uplift, actionPoint);
    22.         }
    23. }
    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
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    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.
     
    Skarlos23 likes this.
  3. Skarlos23

    Skarlos23

    Joined:
    Jan 27, 2015
    Posts:
    3
    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
     
    Last edited: Jan 28, 2015
  4. Skarlos23

    Skarlos23

    Joined:
    Jan 27, 2015
    Posts:
    3
    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.