blob: 37297e4686e610ad015c8583fe7d330f4ab1b8ec (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
extends Spatial
onready var inv_gravity = -ProjectSettings.get_setting("physics/3d/default_gravity_vector") * ProjectSettings.get_setting("physics/3d/default_gravity")
func _physics_process(_delta):
var parent = get_parent()
var water = get_node("/root/Main/Water/MeshInstance")
var height = self.global_transform.origin.y
var wave_height = water.height(self.global_transform.origin)
if height < wave_height:
var pos = self.global_transform.origin-parent.global_transform.origin
#buoyancy force is weight of the displaced water
#we can estimate it by how deep we are in the water
var buoyancy = inv_gravity * clamp(wave_height-height,0.0,1.0)
parent.add_force(buoyancy, pos)
|