0 votes

So, as the title says I want to create shooting mechanics for the game I am making. I am having trouble in particular with the script for both the bullet and the player (I don't know how to get the bullet to come out of the barrel of the gun). I drew inspiration for this game from Hotline Miami and I would like to make the shooting in my game resemble the shooting from Hotline Miami. I am a beginner and most of the script I have written until now was with the help of tutorials so bare with me. I will include screenshots and the script for both the bullet and the player.( I don't know which parts to show you so I will paste the whole thing and hope nobody gets mad at me)
Player Script:

extends KinematicBody2D

export var MOTION_SPEED = 1000
const IDLE_SPEED = 10

var RayNode

var PlayerAnimNode
var anim = ""
var animNew = ""
onready var aspect_ratio = get_viewport_rect().size.x / get_viewport_rect().size.y

func _ready():

  RayNode = get_node("RayCast2D")
  PlayerAnimNode = get_node("AnimatedSprite")
  PlayerAnimNode = get_node("feet")

func _fixed_process(delta):
var motion = Vector2()
var mpos = get_global_mouse_pos()

if (Input.is_action_pressed("ui_up")):
    motion += Vector2(0, -1)

if (Input.is_action_pressed("ui_down")):
    motion += Vector2(0, 1)

if (Input.is_action_pressed("ui_right")):
    motion += Vector2(1, 0)

if (Input.is_action_pressed("ui_left")):
    motion += Vector2(-1, 0)

motion = motion.normalized()*MOTION_SPEED*delta


if (motion.length() > IDLE_SPEED*0.09):
    if (Input.is_action_pressed("ui_right")):
        anim = "Move"
        anim = "move feet"
    if (Input.is_action_pressed("ui_left")):
        anim = "Move"
        anim = "move feet"
    if (Input.is_action_pressed("ui_up")):
        anim = "move feet"
    if (Input.is_action_pressed("ui_down")):
        anim = "move feet"

    if (RayNode.get_rotd() == 180):
        anim = "idle feet"
    if (RayNode.get_rotd() == 0):
        anim = "idel feet"
    if (RayNode.get_rotd() == 90):
        anim = "Idle"
        anim = "idle feet"
    if (RayNode.get_rotd() == -90):
        anim = "Idle"
        anim = "idle feet"

if anim != animNew:
    animNew = anim

Bullet Script

extends RigidBody2D

var force = Vector2(0,0)
var source
var die_on_timeout = true
export var take_player_speed = 0.5
export var speed = 60.0
export var time_left = 10.0
export var damage = 1.0
var animationPlayer
export var dangerous = true

func _ready():
    animationPlayer = get_node("AnimationPlayer")
    animationPlayer.connect("finished", self, "anim_player_finished")

func _process(delta):
time_left -= delta
if((time_left <= 0.0) and (die_on_timeout)):
    dangerous = false
    die_on_timeout = false
if (dangerous):
    set_linear_velocity(get_linear_velocity() + force)
    force = force.linear_interpolate(Vector2(0,0), delta*4)
    for body in get_colliding_bodies():
        dangerous = false
        if (die_on_timeout):
func anim_player_finsihed():


in Engine by (12 points)

2 Answers

0 votes

The easiest way to set a position for object placement is to use a Node2D/Position2D (or 3D equivalents) as a child of the moving node.

Player (KinematicBody2D)
|- BulletOrigin (Position2D)

When you instance the bullet (not as child of the shooter but somewhere else), set the position as the node global position of the "spawn" location.

You can use the node rotation to modify the bullet initial velocity (rotate the velocity vector) so it shoots to the direction is facing.

If your game uses many guns, the guns may be the ones that manages the origin of the shots (and there could be more than one).

by (7,894 points)
0 votes

See official demo - Space Shooter

by (208 points)
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.