Moving an object move to a pre-destined location (4.2.1)

Godot Version

4.2.1

Question

I am currently trying to make a button move onto screen, but am having trouble move to a specific point. or moving at all for that matter.
is there a way to get a button, character 2D, etc. to move from where they are to a pre-destined location?

Hi.
This is the most basic move you can have if your button is in your world, relative to the objects in your game (not relative to your viewport).

If it’s relative to your viewport it also applies, but you have to take into account your screen resolution and probably other variables.

extends Button

const SPEED = 50

var destination = Vector2(100, 100)

func _process(delta):
	position += position.direction_to(destination) * SPEED * delta

1 Like

Do you mean “move” it during the game? Or move it while you are editing the game?

If you mean to move it while the game is running, I assume you have some script?

Can you share the text of your script and an image of your scene tree?

1 Like

ya sure

yes, this would be during the gameplay

here is the tree (with some documentation):
tree
the two unmarked nodes are for sensing when the mouse is in a specific area

and here is my current code:

also I’m new to this language so any critiques on this small bit can be and probably is helpful.

My last question is what do you want to trigger the movement? When should it move?

it should move when the mouse is hovering over the “leftrs” node

I’m guessing you’re trying to do something like this?

extends TextureButton

const SPEED = 50

var destination = Vector2(0, 0)

func _ready():
	destination = position

func _process(delta):
	position += position.direction_to(destination) * SPEED * delta

func _on_position_in_entered():
	destination = position_in.position

func _on_position_out_entered():
	destination = position_out.position

Could you please describe in your words how must the button behave? Like if you’re holding the mouse pointer inside the area the button should go towards that area, but if it’s not in that area anymore, then it shouldn’t go there?

You have two functions, one called _process, which is Godot’s built in function, and the other one is called “prosses” (mispelled?) I still don’t understand what you’re trying to do (and how). I know it might be obvious to you, but, for instance, I don’t understand what “leftrs” means or what position in and out represent. Could you please explain in detail?

2 Likes

Exactly! if your mouse is hovering over an the area by the edge of the screen, the button should go to a predestined position, possibly coordinates. and when the mouse is not hovering over the area, the button should retract out of the screen

the “leftrs” is the area the trigers when the mouse is hovering over it.

Ok, bare with me here. This is what you are going to do:

  1. make a script for the button that is moving with a function that moves it to a location

  2. make a script for the targets with a custom signal that sends a location

  3. connect that signal to that function

script for button:

extends TextureButton

const SPEED = 50

var destination = Vector2(0, 0)

func _ready():
	pass

func _process(delta):
	var distance_to_destination
	var distance_to_move
	if position != destination: # only move if we aren't there
		distance_to_destination = position.distance_to(destination)
		distance_to_move = SPEED * delta
		if abs(distance_to_destination) < abs(distance_to_move): # if we are close, just move to destination
			distance_to_move = distance_to_destination
		position += position.direction_to(destination) * distance_to_move


func set_destination(new_destination):
	destination = new_destination
	print("new_destination:", new_destination)

script for target e.g. leftrs:

extends CollisionObject2D

@export var target_location: Vector2 = Vector2(0, 0)

signal summon_node(destination: Vector2)

func _mouse_enter():
	summon_node.emit(target_location)

You must set the target location in the inspector. It is exported in that second script. I have it default to 0,0

You must connect the signal in the inspector
select leftrs go to node, select the “summon_node” signal.

connect it to the set_destination function
use the “picker” to select the “set_destination” function you already have.

EDIT: You can then connect the “signal script” to any node that is or derives from CollisionObject2D. You can connect as many as you want to move that button all over the place.

EDIT2: “extends CollisionObject2D” didnt show up in the script I pased…so I added it in.

1 Like

what i was originally trying to do was make the button go out when the area or the button itself had the mouse hovering over it
sorry for making things confusing

thank you so much for all your help!

1 Like

You are welcome. The “summon_node” signal would be better name “send_location_on_mouse_enter” because that is what it is doing and you could use it for anything that needs that custom location when the mouse enters…

but I wanted to try to keep it clear and simple for your use case.

1 Like

And, honestly, I would probably make the SPEED a variable and export it so you can change it from the inspector instead of editing the script. Also shout out to @javier.8 as I adapted his basic movement script to your needs.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.