How to make?

ewoniewoni Posts: 14Member

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.


Tags :

Comments

  • GlyphTheWolfGlyphTheWolf Posts: 78Member

    Hi,

    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)
    
  • ewoniewoni Posts: 14Member

    @GlyphTheWolf said:
    Hi,

    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)
    

    Thanks!:)

  • ewoniewoni Posts: 14Member

    @GlyphTheWolf said:
    Hi,

    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)
    

    "add_child" works a spawner an object?

  • GlyphTheWolfGlyphTheWolf Posts: 78Member

    @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.

  • ewoniewoni Posts: 14Member

    @GlyphTheWolf said:

    @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.

    Thank u again! <3

  • ewoniewoni Posts: 14Member

    @GlyphTheWolf said:

    @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:(

  • GlyphTheWolfGlyphTheWolf Posts: 78Member
    edited April 8

    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

    extends KinematicBody2D
    
    class_name PlayerShip
    
    var projectile = preload("res://Projectile.tscn")
    
    const ROTATE_SPEED : float = deg2rad(90.0)
    const MOVE_SPEED : float = 100.0
    var velocity : Vector2 = Vector2.ZERO
    
    func _unhandled_input(event):
        if(event is InputEventKey):
            if(event.scancode == KEY_F && event.pressed):
                fire()
    
    func _physics_process(delta):
        var rotate = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
        var move = Input.get_action_strength("ui_up") - Input.get_action_strength("ui_down")
        var moveDir = (Vector2.UP * move).rotated(rotation)
    
        velocity = move_and_slide(moveDir * MOVE_SPEED)
        rotate(rotate * ROTATE_SPEED * delta)
    
    func fire():
        var projectileInstance = projectile.instance()
        projectileInstance.move_direction = Vector2.UP.rotated(rotation)
        projectileInstance.position = position
        projectileInstance.rotation = rotation
        get_parent().add_child(projectileInstance)
    

    Projectile - projectile script for movement

    extends KinematicBody2D
    
    class_name Projectile
    
    const PROJECTILE_SPEED : float = 200.0
    var move_direction : Vector2 = Vector2.ZERO
    var velocity : Vector2 = Vector2.ZERO
    
    func _physics_process(delta):
        velocity = move_and_slide(move_direction * PROJECTILE_SPEED)
    

    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.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file