Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody
- var movement_amplifier = 10
- var velocity = Vector3()
- var desired_velocity = Vector3()
- var friction = 0.1
- var gravity_strength = -60
- var facing_direction = Vector3()
- var direction = Vector3()
- var upwards_direction = Vector3(0, 1, 0)
- func right_of():
- return -facing_direction.rotated(Vector3(0, 1, 0), deg2rad(-90))
- func left_of():
- return -facing_direction.rotated(Vector3(0, 1, 0), deg2rad(90))
- var highest_reached = 0
- func _process(delta):
- # Gravity and deacceleration
- velocity.y+=gravity_strength*delta
- desired_velocity=Vector3(0, 0, 0)
- # Variable friction
- if is_on_floor():
- friction = 10
- else:
- friction = 1
- friction*=delta
- # Input handling
- if Input.is_action_pressed("forward"):
- desired_velocity += -facing_direction * movement_amplifier
- if Input.is_action_pressed("backward"):
- desired_velocity += facing_direction * movement_amplifier
- if Input.is_action_pressed("left"):
- desired_velocity += left_of() * movement_amplifier
- if Input.is_action_pressed("right"):
- desired_velocity += right_of() * movement_amplifier
- if Input.is_action_pressed("jump") and is_on_floor():
- velocity.y += 4*movement_amplifier
- desired_velocity.y += 4*movement_amplifier
- facing_direction = global_transform.basis.z
- # Gradually go from your velocity, to your desired velocity - thanks to lerp()
- velocity = move_and_slide(lerp(velocity, desired_velocity, friction), upwards_direction)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement