# Setting direction and speed of a bullet instantiated from a tank

4.2.1

### Question

I am learning more on Godot in 3D and I have a tank scene that will instantiate a bullet scene. I got the code as follow in the tank scene:

``````if (event.is_pressed() and Input.is_action_just_pressed("tank_shoot")):

var tank_bullet_instance = tank_bullet.instantiate()
var tank_cannon = get_node("tank_cannon")

tank_bullet_instance.transform.origin =
tank_cannon.global_position

#print(tank_cannon.global_position)
#tank_bullet_instance.transform.origin =
tank_cannon.global_position
#print(tank_cannon.position)
##### tank_bullet_instance.look_at_from_position(tank_cannon.transform.origin, tank_bullet_instance.transform.origin, Vector3.UP) #how come the bullet disappeared...?
# tank_bullet_instance.velocity =....not sure how to set this. I want to set like 10 meters per second at the angle that tank cannon is pointing at.

``````

I am stuck on 2 things:

1. The instantiated bullet should look at the same 3D direction as the tank cannon, but right now with the code above, the bullet somehow does not appear. Without the â€ślook atâ€ť code, the bullet appears. How to do this correctly?
2. How to set the bullet velocity? The bullet is to have velocity of 10 meters per second travelling at the angle that the tank cannon is pointing at.
1. Iâ€™m not sure if this is the only problem with it, but is it possible that the look_at code is not working correctly because of the â€śVector3.UPâ€ť? Itâ€™s not that the bullet â€śdisappearedâ€ť, but rather you canâ€™t see it because you shot it straight up.

2. Itâ€™s hard to tell just from looking at this and not the tank_bullet script, but the way Iâ€™ve done it in the past, is to make sure that the tank_bullet.gd has something like

``````func _process(_delta):
velocity = speed  *  direction
move_and_slide()
``````

or whatever it is you use in 3D projects. So changing the velocity is actually done by a) setting the speed, which can be a constant in the tank_bullet.gd since it probably doesnâ€™t change between shots, and b) setting its direction, which is what that look_at code is trying to do.

1 Like

You told it to move to the tanks origin, not itâ€™s global origin. If I had to guess, your tank cannon is the cold of another node so itâ€™s origin and global origin are different.

Try `tank_cannon.global_transform.origin" instead and see if that works!

1 Like

@potatochopsticks @greencloversgames Thank you for your answers. Right now, I got the bullet spawning in front of my tank cannon, and the bullet is travelling from the front. The bullet scene only has `move_and_slide()` in their `func _physics_process(delta)`.

For the tankâ€™s code, it is now like this:

``````# tank's input event
func _input(event):
...
...
if (event.is_pressed() and Input.is_action_just_pressed("tank_shoot")):

var tank_bullet_instance = tank_bullet.instantiate()

var tank_cannon = get_node("tank_cannon")
var tank_cannon_farther = get_node("tank_cannon_farther")

var bullet_create_at_position = tank_cannon.global_transform.origin
var target_position = tank_cannon_farther.global_transform.origin

var bullet_speed = 10
var bullet_direction = bullet_create_at_position.direction_to(target_position)

tank_bullet_instance.velocity = bullet_speed * bullet_direction
tank_bullet_instance.look_at_from_position( bullet_create_at_position, target_position )