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?
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?
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
Ok, bare with me here. This is what you are going to do:
make a script for the button that is moving with a function that moves it to a location
make a script for the targets with a custom signal that sends a location
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.
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
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.
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.