Hi! I have a question: how do I generate an object with another object? Something like a ship that fired a projectile and that projectile is another object.
You can create projectile as separate scene lets say: "res://projectile.tscn".
Then in your ship when fire you can instance projectile like:
var projectile = preload("res://projectile.tscn")
func fire():
var projectileInstance = projectile.instance()
#That will add projectile as ship child, you can add it also to ship parent if you want or any other node
add_child(projectileInstance)
You can create projectile as separate scene lets say: "res://projectile.tscn".
Then in your ship when fire you can instance projectile like:
var projectile = preload("res://projectile.tscn")
func fire():
var projectileInstance = projectile.instance()
#That will add projectile as ship child, you can add it also to ship parent if you want or any other node
add_child(projectileInstance)
You can create projectile as separate scene lets say: "res://projectile.tscn".
Then in your ship when fire you can instance projectile like:
var projectile = preload("res://projectile.tscn")
func fire():
var projectileInstance = projectile.instance()
#That will add projectile as ship child, you can add it also to ship parent if you want or any other node
add_child(projectileInstance)
@ewoni said:
"add_child" works a spawner an object?
That depends on what you consider as "spawning object".
Actual instance is created by: projectile.instance(), but it won't be visible until you add it to the scene tree what is done by add_child.
Nice, i maked projectile, but he flyed only one side:(
How to make a projectile flying in the direction of the place from where it takes off(like, a shot forward-flies forward, a shot down-is directed nose down and flies down)? Sorry for regular question:(
Well, that's a bit more complicated to explain, because it rely on type of project you create.
For example another logic you will need for 2D side view project and another for 2D top-down project not mentioning 3D which is another story.
Let's assume 2D top-down project where you have ship that can move in any direction in 2D space. Then my approach would be probably to create script for projectile which handle movement in specified direction (probably you have some script for projectile, because you need movement anyway) and set proper movement direction + rotation before adding projectile to scene tree.
Quickly for some demo I prepared something like:
PlayerShip - ship you control and can fire projectiles
Please take in mind that this is only super simple example. velocity is almost never used, because didn't wanted to write all smooth movement logic. That code allow you fire in any direction ship is facing (ship moves with arrows up/down and rotate with arrows left/right). Also I used KinematicBody2D for projectile, but it depends on movement you want to achieve. If you want more physics based movement you can use RigidBody, but then you need change some code since it won't be moved with move_and_slide.
Comments
Hi,
You can create projectile as separate scene lets say: "res://projectile.tscn".
Then in your ship when fire you can instance projectile like:
Thanks!:)
"add_child" works a spawner an object?
That depends on what you consider as "spawning object".
Actual instance is created by:
projectile.instance()
, but it won't be visible until you add it to the scene tree what is done byadd_child
.Thank u again!
Nice, i maked projectile, but he flyed only one side:(
How to make a projectile flying in the direction of the place from where it takes off(like, a shot forward-flies forward, a shot down-is directed nose down and flies down)? Sorry for regular question:(
Well, that's a bit more complicated to explain, because it rely on type of project you create.
For example another logic you will need for 2D side view project and another for 2D top-down project not mentioning 3D which is another story.
Let's assume 2D top-down project where you have ship that can move in any direction in 2D space. Then my approach would be probably to create script for projectile which handle movement in specified direction (probably you have some script for projectile, because you need movement anyway) and set proper movement direction + rotation before adding projectile to scene tree.
Quickly for some demo I prepared something like:
PlayerShip - ship you control and can fire projectiles
Projectile - projectile script for movement
Please take in mind that this is only super simple example.
velocity
is almost never used, because didn't wanted to write all smooth movement logic. That code allow you fire in any direction ship is facing (ship moves with arrows up/down and rotate with arrows left/right). Also I usedKinematicBody2D
for projectile, but it depends on movement you want to achieve. If you want more physics based movement you can useRigidBody
, but then you need change some code since it won't be moved withmove_and_slide
.