0 votes

I want to add bullet Holes, pump animation, and the sparks at the front of the barrel.

full script:

extends KinematicBody

var damage = 20
var spread = 10

var speed = 20
var h_acceleration = 6
var air_acceleration = 1
var normal_acceleration = 6
var gravity = 20
var jump = 10
var full_contact = false

var mouse_sensitivity = 0.2

var direction = Vector3()
var h_velocity = Vector3()
var movement = Vector3()
var gravity_vec = Vector3()

onready var head = $Head
onready var ground_check = $GroundCheck
onready var aimcast = $Head/Camera/AimCast
onready var bullet_hole = preload("res://Assets/BulletDecal.tscn")

onready var ray_container = $Head/Camera/RayContainer
onready var muzzle = $Head/Camera/SPAS12/Muzzle

func _ready():

    randomize()
    for r in ray_container.get_children():
            r.cast_to.x = rand_range(spread, -spread)
            r.cast_to.y = rand_range(spread, -spread)

func _input(event):
    if event is InputEventMouseMotion:
        rotate_y(deg2rad(-event.relative.x * mouse_sensitivity))
        head.rotate_x(deg2rad(-event.relative.y * mouse_sensitivity))
        head.rotation.x = clamp(head.rotation.x, deg2rad(-89), deg2rad(35))

func fire_shotgun():
    if Input.is_action_pressed("fire"):
        for r in ray_container.get_children():
            r.cast_to.x = rand_range(spread, -spread)
            r.cast_to.y = rand_range(spread, -spread)
            if r.is_colliding():
                if r.get_collider().is_in_group("Enemy"):
                    r.get_collider().health -= damage

func _process(delta):
    fire_shotgun()

func _physics_process(delta):

    direction = Vector3()

    if Input.is_action_just_pressed("fire"):
        if aimcast.is_colliding():
            var bullet = get_world().direct_space_state
            var collision = bullet.intersect_ray(muzzle.transform.origin, aimcast.get_collision_point())

            if collision:
                var target = collision.collider
                if target.is_in_group("Enemy"):
                    print("Hit Enemy")
                    target.health -= damage

    if ground_check.is_colliding():
        full_contact = true
    else:
        full_contact = false

    if not is_on_floor():
        gravity_vec += Vector3.DOWN * gravity * delta
        h_acceleration = air_acceleration
    elif is_on_floor() and full_contact:
        gravity_vec = -get_floor_normal() * gravity
        h_acceleration = normal_acceleration
    else:
        gravity_vec = -get_floor_normal()
        h_acceleration = normal_acceleration

    if Input.is_action_just_pressed("jump") and (is_on_floor() or ground_check.is_colliding()):
        gravity_vec = Vector3.UP * jump

    if Input.is_action_pressed("move_forward"):
        direction -= transform.basis.z
    elif Input.is_action_pressed("move_backwards"):
        direction += transform.basis.z
    if Input.is_action_pressed("move_left"):
        direction -= transform.basis.x
    elif Input.is_action_pressed("move_right"):
        direction += transform.basis.x

    direction = direction.normalized()
    h_velocity = h_velocity.linear_interpolate(direction * speed, h_acceleration * delta)
    movement.z = h_velocity.z + gravity_vec.z
    movement.x = h_velocity.x + gravity_vec.x
    movement.y = gravity_vec.y

    move_and_slide(movement, Vector3.UP)
in Engine by (51 points)
edited by

hi, and welcome to godot! Your code is kind of difficult to read because it isn't formatted. To fix this, edit your question, select your code and press the { } icon. This will make it look a lot better. thanks! :D Nice avatar btw.

thanks! (btw my profile pcture is the old 87th sentinel corps heavy trooper from battlefront II)

Yep, I recognized it. :) So for a muzzle flare, you generally make a particle system with a flare texture, but it depends what visual style you're going for. Is this supposed to look realistic, or stylistic or what?

Pretty realistic, but just realistic enough for someone to know it's a muzzle flare.

So here's what I tried: I got a flare texture from this free cc0 particle pack from kenney.nl, but you can use whatever you want. I added a spatial node, and added a plane as a child to it. I gave the plane a spatial material, and changed these material settings.

under Flags, I checked transparent, and do not receive shadows.

under Parameters, I set cull mode to disabled.

under Albedo, I changed the color to orange (it was a white texture) and set the texture to the flare texture.

under Emission, I checked on, set the color to reddish orange, and upped the energy.

Next I duplicated the plane, and rotated it 90 degrees. Then I continued to duplicate and rotate. The goal is to make the plane look 3d from the angles you look at it.

To make it work, all you would have to do, is select the spatial node all the planes are parented to, and set its scale so small you cant see it. Then, in the code when you fire, you could set its scale up so you can see it, and you would have a basic flare.

To up it, you could have an animated texture of a flare on the plane, so you could get a more fluid result, and it would make it disappear nicely. I'm not an expert on materials, so I'm not entirely sure how to do that, but this should get you started at least. :)

Thanks, soo much! I got the animation player by myself, and I was following Garbaj tutorials. If you know how to add bullet holes every time I shoot, then That would be great! Also, if you added a spatial node, where did you add the spatial node to? Did you add it to the gun?

Yes, you need to parent it to the shotgun. I'm kind of a beginner too, so my system probably isn't perfect, I got quite a bit of it from research. As for bullet holes, apparently Godot doesn't have a decal system yet, but it will in Godot 4.0, so you will have to either find a plugin or a workaround. One basic idea for a workaround I saw when researching, was to find the raycasts intersect point, get the normal of the collisions face, and add a plane with a transparent texture of a bullet hole. Tricky, but it sounds doable.

Good luck with your game, it's cool that you're making an fps. :D

Thanks a lot! Your help is appreciated.

Also, should I use the regular particle, or should I use the rotated ones?

unsure what you mean by particle. I didn't end up using a particle system.

If you're referring to all the planes with textures on them, they're all part of the same flare. Its a technique games have used in the past to make the effects look good from all angles without making a 3d mesh. So, I guess in answer, you should use all of them.

this is just one way to do it, there are lots of ways to achieve a gunflare.

Also, can you share the code to add onto the player/shotgun?

Ok, first I need to explain some stuff: The spatial node with all the planes parented to it is called "Flare." I added a timer to it called "Timer" with a wait time of .01. I added a script to Flare, and in the script I added this:

extends Spatial


func _ready():
    hide()


func _physics_process(delta):
    if Input.is_action_just_pressed("fire"):
        show()
        $Timer.start()

This is missing one line of code. I selected Timer, and in the Node panel, I double clicked on timeout. when they asked what node to connect it to, I selected Flare. Now the code should look like this:

extends Spatial


func _ready():
    hide()


func _physics_process(delta):
    if Input.is_action_just_pressed("fire"):
        show()
        $Timer.start()


func _on_Timer_timeout():
    pass

under on_Timer_timeout(): replace the pass function with hide().

Now you should have a basic flare, which appears and disappears when you fire. It's pretty basic, but it may be all you need.

Could you link the tutorial you're following, I might want to watch it sometime. :)

have you learned about Godot signals yet? if not, they're worth looking up. :-) I used one in the code I just posted.

sure thing! The tutorials I have been following have been Garbaj tutorials, which are basically tutorials with stuff but it hard to piece them together.

https://www.youtube.com/channel/UCPUe9uOcp1UMpVi6Vll60Jw

just realized I made a slight mistake in my comment, the timer's wait time should be set to .1, not .01. Thanks for the link, and good luck with your game development! :)

I don't know if you know this, but I was watching some Garbaj tutorials and I realized that he has one for bullet holes. Hope it helps. =)

Thanks (sorry for the 1 year late reply, I haven't been on godot or the godot forums until recently)

Haha I probably would've never understood what you wrote back then. So glad for all those hours of tutorials I watched over the years.

PS I do understand what you wrote now though (took me long enough) and will try to put that to use in future projects

Please log in or register to answer this question.

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.