Hello!
I'm new to game development, but have done some tutorials for 2D platformer and top down game.
I'm trying to implement a point-and-click behavior with collision objects. The main point and click movement flow is described nicely here:
https://docs.godotengine.org/en/3.1/tutorials/2d/2d_movement.html#click-and-move
I'm using it in a similar way with moveandslide, however when bringing in some collision objects the KinematicBody2D still slides on a collision object if colliding it with a definitive size greater or lower than 90 deg angle. I'd like the player(KinematicBody2D) to stop or slide it across the colliding body(wall - StaticBody2D) without slowing down gradually.
Currently I'm detecting a moveandslide collision and forcing the KinematicBody2D to moveandcollide which nicely stop the player but I'm unable to get the player away from the collision object. It's just snapped to it and can't make it back to move.
How to make a player back to move after moveandcollide stopped it with a point and click moves used in my script? I tried it with setting a new position but seems I don't know how to use the 'set_position(value)' in Node2D
Here's my script:
extends KinematicBody2D
var speed = 200
# original starting point for character
var targetPos = Vector2(980, 1200)
var velocity = Vector2()
var distance = 0
var floor_normal = Vector2(0, 0)
var max_bounces = 0
var collision
var collided = false
func _input(event):
if event.is_action_pressed('click'):
targetPos = get_global_mouse_position()
distance = position.distance_to(targetPos)
func _physics_process(delta):
velocity = (targetPos - position).normalized() * speed
movePlayer(delta)
func movePlayer(delta):
for i in get_slide_count():
collision = get_slide_collision(0)
if get_slide_count() > 0:
collided = true
else:
collided = false
if collided:
move_and_collide(velocity * 0 * delta)
var newPosition = Vector2(668, 1031)
# error
# set_position(newPosition)
# make it move to a target position
elif (targetPos - position).length() > 5:
move_and_slide(velocity, floor_normal, true)
# need to stop movement if clicking too close to a character
elif distance < 5:
move_and_slide(velocity * 0, floor_normal, true)