Area2D Bullet

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Icarus
:warning: Old Version Published before Godot 3 was released.

I have a bullet scene that is simply Area2D > Sprite.

My bullet is very simple as it just requires queue_free when it collides and all the more complex reactions will be on the side of the enemy node.

How do I make the Area2D move?

Or should I add a RigidBody2D as the parent?

I’m using the G3.0 beta.

:bust_in_silhouette: Reply From: rustyStriker

You can use:

 positoin+= Speed * delta # Speed is a vector2 of the velocity

and connect the body_enter signal to a function in order to do all the thingys

Thanks for your reply. That was the method I considered initially, however, as I’m using my own function outside of _physics_process, delta is unavailable. As such all that it does it change the position as opposed to produce ‘motion’.

Is there a way to utilise delta in my own bespoke functions?

Icarus | 2017-12-11 16:06

Just hand it over as a parameter:

func get_position(d):
  return speed * d

func _physics_process(delta):
  position = get_position(delta)

Or something like that. It depends on your code structure but it is always possible to call a function outside of _process and use the current delta as a parameter (d becomes delta here).

rolfpancake | 2017-12-11 16:24

Okay. Something is not quite right. I’ve included my Player.gd code below. The projectile scene is instanced in there. The projectile scene itself is simply Area2D > Sprite with no script.

extends KinematicBody2D

#Set Member Variables
export (int) var PLAYER_SPEED
export (int) var PROJECTILE_SPEED

var projectile_instance = preload("res://scenes/Projectile.tscn")

onready var projectile_spawn_location = get_node("ProjectileSpawnLocation")

func _ready():
    set_physics_process(true)

func _physics_process(delta):

    #Set Player Aiming
    var player_aim_angle
    player_aim_angle = get_angle_to(get_global_mouse_position())
    self.rotate(player_aim_angle)

    #Set Player Movement
    var velocity = Vector2()
    if Input.is_action_pressed("move_down"):
	    velocity.y += 1
    if Input.is_action_pressed("move_up"):
	    velocity.y -= 1
    if Input.is_action_pressed("move_left"):
	    velocity.x -= 1
    if Input.is_action_pressed("move_right"):
	    velocity.x += 1
    move_and_slide(velocity.normalized() * PLAYER_SPEED)

    #Spawn & Move Projectile
    if Input.is_action_pressed("shoot"):
	    spawn_projectile()

func spawn_projectile():
    #Spawn Projectile
    var projectile = projectile_instance.instance()
    projectile_spawn_location.add_child(projectile)

    #Move Projectile
    var projectile_velocity = Vector2(PROJECTILE_SPEED,0)
    projectile.position += projectile_velocity * delta

That produces an area because of delta not being recognised in the bespoke function. But when I simply move the code from the spawn_projectile function and put it directly in the if statement of the action_pressed it still doesn’t work as hoped.

Icarus | 2017-12-11 16:33

moving the projectile itself needs to be in the projectile’s code and be called repeatedly( aka every call of _physics_process) so it will make it appear as if the projectile is moving and not just teleporting around…

motion in video games is simply “teleporting” the object around in a straight line with a little difference in every jump, while checking for collisions in the way…

so you need to make a script for the projectile in order to move him, and call the function or whatever part of code you got there to move the projectile every _physics_process or else(like what you did) it will move once and will stop

rustyStriker | 2017-12-11 16:43

Thankyou, that solved the projectile not being in ‘motion’. However, the projectile now, while in ‘motion’ will rotate as the player rotates.

How would I adjust my code to ensure the projectile’s rotation is set on spawn but is then independent of it’s parent?

Icarus | 2017-12-11 16:57

You instantiate your projectile under your player node but it must be instantiated under the root node.

rolfpancake | 2017-12-11 17:00

When you say root, do you mean the World Scene or the Root Node that is above Player in the Player scene?

Icarus | 2017-12-11 17:16

It depends how your game is structured but I am sure it should be instanced into your world scene. You spawn a new object into your world which interacts with the environment (collision) and after it is shoot it has nothing to do with the player. So it should be instantiated like every other enemy, crate, coin or whatever. Like:

world (root)
  ground
  player
    gun
  enemy1
  enemy2
  projectile1
  projectile2

The guns position is always depending on the players position - so it is instantiated under the player. The projectile only starts at the guns position but moves inside the world and has nothing to do with the players movement.

rolfpancake | 2017-12-11 17:26

you can use either the World node itself, the root(that can be achieved by get_tree().get_root() or anything that doesnt move at all and is placed at 0,0 with rotation of 0 degrees

rustyStriker | 2017-12-11 17:27

Okay, In my World Scene I had:

Node2D > RigidBody2D(Player)
       > RigidBody2D(Enemy)

So I will add the projectile so it is like below:

Node2D > RigidBody2D(Player)
       > RigidBody2D(Enemy)
     **> Area2D(PlayerProjectile)**

Icarus | 2017-12-11 17:31

Looks okay to me. Next time you ask a question please consider providing a minimal sample project. This would make the task a little bit easier.

rolfpancake | 2017-12-11 17:33

I’m ever so grateful for you taking your time to help a beginner like me.

It made logical sense to me that the projectile should be controlled by the player but obviously it inherits too many properties from the player as the parent causing the discussed issues.

I will consider the sample projects in future. I thought this may be simple enough but I can see how it can be confusing not having all the info.

Icarus | 2017-12-11 17:40

No problem. Things will get easier and your questions will become harder to answer. :smiley:
We’ll do our best to help you where we can :wink:

rolfpancake | 2017-12-11 18:34