Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. extends KinematicBody2D
  2.  
  3. # notice how we don't need 'distance' anymore
  4.  
  5. # this is now just a number instead of a Vector2. it represents the desired x position
  6. var destination = 0
  7. # how fast (in pixels per second) the thing will move towards its destination
  8. var speed = 100
  9.  
  10. func _ready():
  11. position = Vector2(400,500)
  12. # you can access the x and y properties of a vector easily like this
  13. destination = position.x
  14.  
  15. func _input(event):
  16. if event.is_action_pressed("ui_mouse_left"):
  17. # notice the difference here. we're still getting a Vector2 from get_global_mouse_position,
  18. # but we're now only interested in the x component
  19. destination = get_global_mouse_position().x
  20. print(destination)
  21.  
  22. func _process(delta):
  23. # if our position's x coordinate is LESS than our destination's, that means
  24. # we are to the LEFT of the mouse click.
  25. if position.x < destination:
  26. # which means we need to go RIGHT to get to our destination
  27. move_and_slide(Vector2.RIGHT * speed)
  28. # if our position's x coordinate is GREATER than our destination's, that means
  29. # we are to the RIGHT of the mouse click.
  30. elif position.x > destination:
  31. # which means we need to go LEFT to get to our destination
  32. move_and_slide(Vector2.LEFT * speed)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement