0 votes

so, i set a rigidbody2d to be instanced in front of the player whenever a certain button is clicked,
problem is i want to make it spawn in different positions and shoot in different directions depending on player's input,

here is the code i'm using for shooting:

if Input.is_action_just_pressed('shoot'):
    var projectile = PROJECTILE_SCENE.instance()
    projectile.set_position(get_position()-Vector2(-10,-5))
    get_tree().get_root().add_child(projectile)

here is the whole player code just incase:

extends KinematicBody2D

const PROJECTILESCENE = preload("res://projectile.tscn")
const UP = Vector2(0,-1)
const GRAVITY = 20
const SPEED = 200
const JUMP
HEIGHT = -500

var velocity = Vector2()

func ready():
set
physics_process(true)

func get_input():
velocity.y += GRAVITY

if Input.is_action_just_pressed('shoot'):
    var projectile = PROJECTILE_SCENE.instance()
    projectile.set_position(get_position()-Vector2(-10,-5))
    get_tree().get_root().add_child(projectile)
if Input.is_action_pressed('ui_right'):
    velocity.x = SPEED
    $Sprite.flip_h = false
    $Sprite.play("run")
elif Input.is_action_pressed('ui_left'):
    velocity.x = -SPEED
    $Sprite.flip_h = true
    $Sprite.play("run")
else:
    velocity.x = 0
    $Sprite.play("idle")
if is_on_floor():
    if Input.is_action_just_pressed('ui_up'):
        velocity.y = JUMP_HEIGHT
else:
        $Sprite.play("jump")

func physicsprocess(delta):
getinput()
velocity = move
and_slide(velocity, UP)

i thought of making other rigidbody scenes and instance them in different positions in relation to the player with different velocity depending on if the sprite is flipped for right and left + checking input for 45° angles, i think it will work but it just seem like too much of a mess.
is there a way to use only 1 instanced rigidbody scene that change position and direction according to player input or something like that?
not sure if i can't figure it out bc i have no prior knowledge of programming or because i have the mental capacity of a chimp, either way i will greatly appreciate any help.

in Engine by (12 points)
edited by

1 Answer

0 votes

I solved something similar by using position2d nodes. This is a somewhat crude solution, but it works.

by (114 points)

I acually managed to get it working except for two button input(and its as messy as i expected it to be).
Not sure how position2d nodes are going to help but i'll try it, ty.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.