0 votes

I have a little chest that I'd like a coin to appear out of and then fly to the upper right corner.

enter image description here

What I'm trying:
I placed the coin on a CanvasLayer and I'm doing a tween to send it to the corner. The problem is I need to get where the chest is located and then translate that to the CanvasLayer's components to then tween to the corner.

tween.interpolate_property(sprite, "position:x", sprite.position.x, 310, 1, Tween.EASE_IN, Tween.TRANS_LINEAR)
tween.interpolate_property(sprite, "position:y", sprite.position.y, 10, 1, Tween.EASE_IN, Tween.TRANS_LINEAR)
tween.start()

The sprite position that I'm passing into coin to start the tween is I think the global_position to the entire game. Not sure how to translate between the two.

in Engine by (109 points)

1 Answer

0 votes
Best answer

I've done something similar by using the get_global_transform_with_canvas() function of the CanvasItem class.

https://docs.godotengine.org/en/stable/classes/class_canvasitem.html?highlight=get_global_transform_with_canvas#class-canvasitem-method-get-global-transform-with-canvas

In my case, when a coin collision is detected, I fire this signal:

Events.emit_signal("collect_coin", coin.get_global_transform_with_canvas().origin)

After the signal fires, I delete the in-game coin that triggered the collision.

When the signal is received (in my case by the CanvasLayer-based HUD scene) I instance a new coin at the (converted) position passed in the signal. From there, the new coin's start and end position are both in canvas coordinates, which is easy to handle via a simple Tween.

I'm not sure if there's a better way, but the above worked for me.

by (10,856 points)
selected by

I couldn't find a better way, or any way for that matter. The trick was to use the getglobaltransformwithcanvas origin. I didn't know this exsisted and still don't know what the difference is between the different positions but this definitely worked. Thanks! I had actually given up on making "collections" in my game this way so glad I could add it back in :)

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.