I have the following structure in Godot
- Player(rigibody2D)
- CollisiónShape2D
- Tanque(Node2D)
- Sprite
- Barrel (Position2D)
- Line2D
- Punto (Node2D)
and in the bullet instance it is a Rigidbody2D.
Player(rigibody2D)
- CollisiónShape2D
- Tanque(Node2D)
- Sprite
- Barrel (Position2D)
- Line2D
- Punto (Node2D)
extends KinematicBody2D
export var muzzle_velociy = 230
export var gravity = 250
export var Bullet = preload("res://scenes/Bullet.tscn")
onready var posit = $Tanque/Barrel
onready var line = $Tanque/Line2D
var max_point = 250
func shoot():
var b = Bullet.instance()
b.global_transform = posit.global_transform
b.apply_impulse(Vector2(), Vector2(muzzle_velociy, 0).rotated($Tanque.rotation ) )
get_tree().current_scene.add_child(b)
func _unhandled_input(event):
if event.is_action_pressed("ui_select"):
shoot()
func update_trajectory(delta):
# posit -- position2D
# line -- line2D
line.clear_points()
var pos = posit.position
var vel = posit.transform.x * muzzle_velociy
for i in max_point:
line.add_point(pos)
vel.y += gravity * delta
pos += vel * delta
func _process(delta):
if Input.is_action_pressed("ui_mouse_left"):
line.show()
update_trajectory(delta)
else:
line.clear_points()
As you can see in the image, it does not follow the correct angle that projects where the Bullet will fall.