How can I code this?

Godot Version

godot 4

Question

I want to code a system when u click ur left mouse u can throw a bomb. So i already set the Input and called it “skill”. And then I dont know how to code so that the bomb will be throw and move to ur mouse cursor location. Plss Help.

Is this 2d or 3d?
You need to get the mouses position in the world

2D

get_viewport().get_mouse_position()

3D - get the viewport mouse position, then cast a ray from the camera to this 2d(viewport) position, which gets you a 3d direction. Then you find the point where this ray intersects something in your 3d scene(like a ground plane).

func get_mouse_position_on_3D_plane():
var mouse_pos = get_viewport().get_mouse_position()
var ray_from = Camera.project_ray_origin(mouse_pos)
var ray_to = ray_from + Camera.project_ray_normal(mouse_pos) * ray_length
var ray_dir = (ray_to - ray_from).normalized()
var t = -ray_from.y / ray_dir.y
mouse_intersection_point = ray_from + ray_dir * t

start there, and maybe create a marker(mesh node of some kind) at the mouse position to debug.

$marker_mesh.position = mouse_intersection_point

Once you get that, you can make a projectile, but without more info idk if you want a projectile arc, or a straight shot, etc.

1 Like

it 2d and i want a straight shot for a top down game. and the bomb gonna move to the mouse cursor when clicked

Try this out. I tried to keep the scene tree simple to start with. There’s a root CharacterBody2D node, with a script attached. It has a color rect for the player, and color rect for the projectile(turn off it’s visibility in the scene editor)

This way you can add character movement later on. But best practice could be to make the projectile a separate scene.

attach this script to your root characterbody2d node, and keep the node names the same as in this image

scene_tree


extends Node2D

#mouse position vector 2
var mouse_pos  : Vector2

#projectile variables
var projectile : ColorRect
var projectile_speed = 100.0
var projectile_direction : Vector2
var projectile_array = []


# mouse input left click
# duplicates projectile, sets it to visible
# gets direction from projectile to mouse position on screen
# stores projectile duplicate and direction as pairs in an array
func _input(event):
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
			print("left mouse clicked")
			#duplicate projectile
			var projectile_instance = projectile.duplicate()
			# add instance as child
			add_child(projectile_instance)
			projectile_instance.visible = true
			# get mouse position on screen
			mouse_pos = get_viewport().get_mouse_position()
			# get direction from projectile to mouse position
			projectile_direction = (mouse_pos - projectile.global_position).normalized()
			# appends instance and direction in array to be used later in process()
			projectile_array.append([projectile_instance, projectile_direction])
	

		
# Called when the node enters the scene tree for the first time.
func _ready():
	# sets projectile variable to the node in your scene tree
	projectile = $ColorRect_projectile

	

func _process(delta):
	# loop through projectile array, so you can set each projectiles velocity manually
	for i in projectile_array:
		#i[0] gets the projectile instance
		#i[1] gets the corresponding projectiled_direction
		#i loops through each pair in the projectile_array
		
		# += adds computed movement vector to projectile_instance position
		# projectile_instance position------instance.global_position
		# computed movement vector------direction*speed*time
		i[0].global_position += i[1] * projectile_speed * delta
1 Like

This working for now Thank you so muchh

1 Like

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