Godot Version
4
Question
Hi,
I’m currently exploring the possibility of a game and I’m trying to get a mechanic working, but I’m stuck on how I should code it.
I currently have a ball that is rotating around a pivot point in a circle when a user clicks. When the user lets go, I want to “fire” the ball but I want it to move in the direction that it would go based on it’s position rotating around the circle. A bit like a hammer throw at the olympics might be an easier way to describe it.
Here is my main code:
extends Node2D
@export var rotation_speed = PI
@export var Projectile:PackedScene
var pressed = false
func _process(delta):
if pressed:
$Circle/Pivot.rotation += rotation_speed * delta
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseButton:
if event.pressed:
pressed = true
else:
$Circle/Pivot.rotation = 0
var b = Projectile.instantiate()
owner.add_child(b)
b.transform = $Circle/Pivot.global_transform
pressed = false
My “projectile” does instantiate but at the moment it just shoots to the right due to how I’ve set things up in that class, but I’d like to get it to travel in a direction like I just mentioned. i’m assuming I need to pass the x,y position it should fire towards but I don’t know how I can calculate this from the rotation.
Any help would be appreciated.
thanks