Search Unity

Question Unity 2D, How to make the player jump out the water and lock controls?

Discussion in 'Physics' started by AkcentXD, Nov 28, 2022.

  1. AkcentXD

    AkcentXD

    Joined:
    Mar 13, 2021
    Posts:
    1
    Hello,

    I try to make a 2D game with a submarine. I have the script for movement but I want the submarine to jump out of the water and fall back based on the speed and direction it had in the water. Something like a dolphin jumping out of the water :)

    Code (CSharp):
    1.  
    2.     public Rigidbody2D Submarin;
    3.     public float maxSpeed = 5f;
    4.     public float acceleration = 1f;
    5.     public float waterSurfaceLimit = 10f;
    6.     Vector2 movement;
    7.     Vector2 lastMovement;
    8.     private float speed = 0f;
    9.     private bool facingRight = true;
    10.      void Update()
    11.     {
    12.         movement.x = Input.GetAxisRaw("Horizontal");
    13.         movement.y = Input.GetAxisRaw("Vertical");
    14.  
    15.         if (speed < maxSpeed)
    16.             speed += acceleratie;
    17.  
    18.         if (Input.GetAxisRaw("Horizontal") == 0 && Input.GetAxisRaw("Vertical") == 0)
    19.             speed = 0f;
    20.     }
    21.     void FixedUpdate()
    22.     {
    23.         FlipDirection(Input.GetAxisRaw("Horizontal"));
    24.         Submarin.constraints = RigidbodyConstraints2D.FreezeRotation;
    25.         Submarin.MovePosition(Submarin.position + movement * speed * Time.fixedDeltaTime);
    26.  
    27.         //if (Submarin.position.y < waterSurfaceLimit)
    28.         //{
    29.         //    Submarin.gravityScale = 1; // gravity value in water
    30.         //    Submarin.MovePosition(Submarin.position + movement * speed * Time.fixedDeltaTime);
    31.         //    lastMovement = movement;
    32.         //}
    33.         //else
    34.         //{
    35.         //    Submarin.gravityScale = 10; // gravity value above water
    36.         //    lastMovement.y -= Submarin.gravityScale * Time.fixedDeltaTime;
    37.         //    lastMovement.x -= Submarin.gravityScale * Time.fixedDeltaTime;
    38.         //    Submarin.MovePosition(Submarin.position + lastMovement * speed * Time.fixedDeltaTime);
    39.         //    //Submarin.MovePosition(lastMovement);
    40.         //}
    41.     }
    The uncommented code is my current movement script, and the commented code is my attempt to make the submarine jump out of the water.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    You're using constraints so this suggests this is a Dynamic Rigidbody2D but you're using MovePosition to explicitly state where it should be positioned. This isn't velocity based movement so if you want a "jump" you'll have to explicitly set all those positions for a jump. This is Kinematic motion typically used on a Kinematic Rigidbody2D.

    I think you should go back to looking at some physics tutorials if I'm honest. Understand how to manipulate via forces or velocity.

    If you understand all this, it's not suggested by the code above.