Hi guys!
i have some code I'm working on (its a little crude) but basically moves a sprite towards a point in steps, checking for collisions. And if it would collide, it moves in the closest possible direction. Trying to get this part to work and i can incorporate it into the bigger picture of what I'm trying to make.
It works fine without collisions and moves along happily in any direction.
but when it will collide, the game screen freezes.
any tips on what i can do to fix this?
Here's the code in question: (rl = vector2 of ray length in x and y. the ray itself is (10,0))
extends Sprite
onready var ray = get_node("CharRay")
var rl := Vector2(0,0)
var cw
var ccw
func _unhandled_input(event):
var points = []
if not event is InputEventMouseButton:
return
if event.button_index != BUTTON_RIGHT or not event.pressed:
return
var goal = get_global_mouse_position()
var angle = self.position.angle_to_point(goal)
ray.set_global_rotation(angle)
print(ray.is_colliding())
if not ray.is_colliding():
rl.x = ray.cast_to.x*cos(ray.global_rotation)
rl.y = ray.cast_to.x*sin(ray.global_rotation)
points.append(position-rl)
else:
while ray.is_colliding():
ray.set_global_rotation_degrees(ray.global_rotation_degrees+1)
cw = ray.global_rotation
ray.set_global_rotation(angle)
while ray.is_colliding():
ray.set_global_rotation_degrees(ray.global_rotation_degrees-1)
ccw = ray.global_rotation
if cw < ccw:
ray.set_global_rotation(cw)
rl.x = ray.cast_to*cos(ray.global_rotation)
rl.y = ray.cast_to*sin(ray.global_rotation)
points.append(position-rl)
self.position = points[0]