0 votes


I am working on a drag and drop turn based game using the mouse and am trying to figure out how to get the movement working, the idea is that each unit has a movement in pixels (say 200 for this example). I have set an anchor at the units starting location and I want it to be able to pick the unit up with the mouse and move it up to the movement value away from the anchor.

the setup here is just using a kinematicbody2d with a child sprite and collisionshape2d.

I have managed to get a crude version working with an if statement that stops the model being moved if the mouse moves more than the speed away from the anchor, but what I want is for the unit to travel up to the maximum distance from the anchor point and then orbit around the anchor at that distance following the mouse.

any ideas on how to achieve that?

this is my code so far:

extends KinematicBody2D

export (int) var speed = 200

var mouse
var model
var selected
var anchor
var distance
var velocity = Vector2()
var screensize
var destination
var vector
var difference

func _ready():
    anchor = position
    screensize = get_viewport_rect().size

func _physics_process(delta):
    mouse = (get_global_mouse_position())

func select_model():
    model = position
    if mouse.distance_to(model) < size:
        if Input.is_action_just_pressed("mouse_left"):
            selected = true
    if Input.is_action_just_released("mouse_left"):
        selected = false

func get_input():
    distance = mouse.distance_to(anchor)
    difference = anchor - mouse
    if selected == true:
        if (distance < speed): 
            #position = get_viewport().get_mouse_position()
            position = Vector2((anchor.x - difference.x), (anchor.y - difference.y))
asked Jun 10 in Engine by grimvoodoo (12 points)
edited Jun 10 by Calinou

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.