Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody2D
- class_name RigidKinematicBody2D
- export(Vector2) var linear_velocity = Vector2.ZERO
- export(Vector2) var gravity = Vector2(0, 9.8) setget set_gravity
- export(float) var dampening = 0.01
- export(float, 0.0, 1.0) var bounciness = 0.5
- func set_gravity(value:Vector2)->void:
- gravity = value
- func set_linear_velocity(value:Vector2)->void:
- linear_velocity = value
- func _physics_process(delta)->void:
- linear_velocity += gravity
- var collision = move_and_collide(linear_velocity * delta) #apply physics
- linear_velocity = linear_velocity * (1 - dampening) #reduce speed over time
- if collision: #collision detected
- var normal:Vector2 = collision.normal #surface normal
- var strenght:float = normal.dot(linear_velocity)
- linear_velocity -= normal * strenght * (1 - bounciness) #dampen velocity in floor direction
- linear_velocity = (linear_velocity + collision.remainder).bounce(normal) #bounce off the surface
- print(linear_velocity.x)
- func apply_impulse(value:Vector2)->void:
- linear_velocity += value
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement