0 votes

enter image description here

How do I make the object follow the mouse position but is limited by the radius?
I got the way in unity, but after i try in godot it doesn't work
https://answers.unity.com/questions/1176172/object-follow-mouse-in-radius.html

Godot version latest
in Engine by (32 points)

2 Answers

+1 vote
Best answer

Hello,

What do you want to do? Is it in 2D or in 3D? Should the object smoothly move towards the mouse or teleport to it?

I am going to assume the most simple case: a 2D constraint where the object teleports.
What you have to do is simple:

  1. Get the constrained object position relative to the area center,
  2. constraint this position to the radius if it is too big,
  3. place the constrained object at the calculated position.

Here is a script that does that:

# This is the "constrainer" script
# You can move the constrainer node to move the area center.

tool
extends Node2D


## The radius of the constrained area.
export var radius := 30 setget set_radius

export var constrained_object_path: NodePath
onready var constrained_object: Node2D = get_node(constrained_object_path)


func _process(_delta: float) -> void:
    # 1. Get the constrained object position relative to the area center
    var global_dir := get_global_mouse_position() - global_position
    # 2. constraint this position to the radius if it is too big
    if global_dir.length() > radius:
        global_dir = radius * global_dir.normalized()
    # 3. place the constrained object at the calculated position
    constrained_object.global_position = global_position + global_dir


func _draw() -> void:
    draw_circle(Vector2.ZERO, radius, Color(1.0, 0.0, 0.0, 0.25))


func set_radius(r):
    radius = r
    update() # this triggers a _draw() call.
by (728 points)
edited by
+1 vote

Assuming 2d, verboseness for clarity:

export var radius = 5

func _process(_delta):
    var mouse_pos = get_tree().get_mouse_position()
    var player_pos = player.transform.origin 
    var mouse_dir = (mouse_pos-player_pos).normalized()

    obj.transform.origin = player_pos + (mouse_dir * radius)

I've not tested this code so apologies in advance if there's a mistake. If you want the obj to also face the mouse then obj.look_at(mouse_pos)

by (813 points)

enter image description here
Thank You, Your Comment helped Me a lot.
It works fine, i modified it a bit

export var radius = 5
func _process(delta: float) -> void:
    var mouse_pos = get_global_mouse_position()
    var player_pos = player.global_transform.origin 
    var distance = player_pos.distance_to(mouse_pos) 
    var mouse_dir = (mouse_pos-player_pos).normalized()
    if distance > radius:
        mouse_pos = player_pos + (mouse_dir * radius)
    object.global_transform.origin = mouse_pos

Glad it helped! Some sensible modifications you've made there. Can't believe the amount of faffing about Unity requires to do something so simple. Stick with Godot! ;)

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.
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 webmaster@godotengine.org with your username.