0 votes

Hi,

I am working on a drag and drop style game and am trying to get the movement working correctly, I want to have the model only move up to its speed away from its starting point each turn and I can use clamp to do this, if I set clamp to the x and y it works by locking it to a square. but I want it to be locked to a circle.

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())
    select_model()

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
    get_input()

func get_input():
    distance = mouse.distance_to(anchor)
    difference = anchor - mouse
    if selected == true:
    position = Vector2((anchor.x - difference.x), (anchor.y - difference.y))
    position.x = clamp(position.x, anchor.x - speed, anchor.x + speed)
    position.y = clamp(position.y, anchor.y - speed, anchor.y + speed)
in Engine by (12 points)
edited by

1 Answer

0 votes

Maybe you apply the code from the answer to the following question:
https://godotengine.org/qa/116981/object-follow-mouse-in-radius

by (73 points)
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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.