Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | alienbeliever | |
Old Version | Published before Godot 3 was released. |
Gathering from different sources, I found the solution. Hope it helps.
NOTE: Bullet is a “RigidBody2d”, and gun is a “Sprite”
func bullet():
var rot = get_rot() #rotation
var vel = 300 #Bullet’s velocity
var dir = Vector2(sin(rot), cos(rot)) #direction
var dis_origin = 50 #distance from origin, you can set this to your own liking
var spawn_point = get_global_pos() + dir * dis_origin
var bullet = preload(“res://scenes/bullet.tscn”).instance()
bullet.set_global_pos(spawn_point)
bullet.set_linear_velocity(Vector2(0, vel).rotated(rot))
get_tree().get_root().add_child(bullet)
pass
alienbeliever | 2017-04-16 12:19