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 ![]()
public Rigidbody2D Submarin;
public float maxSpeed = 5f;
public float acceleration = 1f;
public float waterSurfaceLimit = 10f;
Vector2 movement;
Vector2 lastMovement;
private float speed = 0f;
private bool facingRight = true;
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
if (speed < maxSpeed)
speed += acceleratie;
if (Input.GetAxisRaw("Horizontal") == 0 && Input.GetAxisRaw("Vertical") == 0)
speed = 0f;
}
void FixedUpdate()
{
FlipDirection(Input.GetAxisRaw("Horizontal"));
Submarin.constraints = RigidbodyConstraints2D.FreezeRotation;
Submarin.MovePosition(Submarin.position + movement * speed * Time.fixedDeltaTime);
//if (Submarin.position.y < waterSurfaceLimit)
//{
// Submarin.gravityScale = 1; // gravity value in water
// Submarin.MovePosition(Submarin.position + movement * speed * Time.fixedDeltaTime);
// lastMovement = movement;
//}
//else
//{
// Submarin.gravityScale = 10; // gravity value above water
// lastMovement.y -= Submarin.gravityScale * Time.fixedDeltaTime;
// lastMovement.x -= Submarin.gravityScale * Time.fixedDeltaTime;
// Submarin.MovePosition(Submarin.position + lastMovement * speed * Time.fixedDeltaTime);
// //Submarin.MovePosition(lastMovement);
//}
}
The uncommented code is my current movement script, and the commented code is my attempt to make the submarine jump out of the water.