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

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.