Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody2D
- var analog_stick_center
- var analog_stick_pos
- func _ready():
- set_fixed_process(true)
- set_process_input(true)
- func _input(event):
- if (event.type == InputEvent.SCREEN_TOUCH):
- if event.is_pressed():
- analog_stick_center = event.global_pos
- else:
- analog_stick_center = null
- analog_stick_pos = null
- elif (event.type == InputEvent.SCREEN_DRAG):
- analog_stick_pos = event.global_pos
- func _fixed_process(delta):
- relative_directional_movement(delta)
- func relative_directional_movement(delta):
- if analog_stick_pos != null && analog_stick_center != null:
- var pos_vector = analog_stick_pos - analog_stick_center
- if pos_vector.length() > MOUSE_WALK_THRESHOLD:
- var target_pos = get_global_pos() + pos_vector
- move_in_rect_line_towards(target_pos)
- func move_in_rect_line_towards(pos):
- var angle = get_global_pos().angle_to_point(pos)
- var motion = move(Vector2(0, -speed).rotated(angle))
- if (is_colliding()):
- slide(motion)
- func slide(motion, apply_friction=true):
- # You can check which tile was collision against with this
- # print(get_collider_metadata())
- # Ran against something, is it the floor? Get normal
- var n = get_collision_normal()
- # For every other case of motion, our motion was interrupted.
- # Try to complete the motion by "sliding" by the normal
- var new_motion = n.slide(motion)
- if not apply_friction:
- # Ignore collision friction
- var current_angle = motion.angle()
- var slide_angle = new_motion.angle()
- new_motion = motion.rotated(slide_angle - current_angle)
- move(new_motion)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement