Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody2D
- # notice how we don't need 'distance' anymore
- # this is now just a number instead of a Vector2. it represents the desired x position
- var destination = 0
- # how fast (in pixels per second) the thing will move towards its destination
- var speed = 100
- func _ready():
- position = Vector2(400,500)
- # you can access the x and y properties of a vector easily like this
- destination = position.x
- func _input(event):
- if event.is_action_pressed("ui_mouse_left"):
- # notice the difference here. we're still getting a Vector2 from get_global_mouse_position,
- # but we're now only interested in the x component
- destination = get_global_mouse_position().x
- print(destination)
- func _process(delta):
- # if our position's x coordinate is LESS than our destination's, that means
- # we are to the LEFT of the mouse click.
- if position.x < destination:
- # which means we need to go RIGHT to get to our destination
- move_and_slide(Vector2.RIGHT * speed)
- # if our position's x coordinate is GREATER than our destination's, that means
- # we are to the RIGHT of the mouse click.
- elif position.x > destination:
- # which means we need to go LEFT to get to our destination
- move_and_slide(Vector2.LEFT * speed)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement