├── BadGuy.gd ├── README.md ├── LICENSE └── Hitscan.gd /BadGuy.gd: -------------------------------------------------------------------------------- 1 | extends KinematicBody 2 | 3 | var health = 200 4 | 5 | func _ready(): 6 | pass 7 | 8 | func _process(delta): 9 | if health <= 0: 10 | queue_free() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # godot-hitscan-weapons 2 | 3 | Source code for a simple FPS hitscan weapon system in the Godot game engine. Watch the video tutorial here: https://youtu.be/4jbfIN4t83k 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Garbaj 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Hitscan.gd: -------------------------------------------------------------------------------- 1 | extends KinematicBody 2 | 3 | var speed = 7 4 | var acceleration = 50 5 | var gravity = 20 6 | var jump = 10 7 | 8 | var damage = 100 9 | 10 | var mouse_sensitivity = 0.03 11 | 12 | var direction = Vector3() 13 | var velocity = Vector3() 14 | var fall = Vector3() 15 | 16 | onready var head = $Head 17 | onready var aimcast = $Head/Camera/AimCast 18 | onready var muzzle = $Head/Gun/Muzzle 19 | 20 | func _ready(): 21 | 22 | Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) 23 | 24 | func _input(event): 25 | 26 | if event is InputEventMouseMotion: 27 | rotate_y(deg2rad(-event.relative.x * mouse_sensitivity)) 28 | head.rotate_x(deg2rad(-event.relative.y * mouse_sensitivity)) 29 | head.rotation.x = clamp(head.rotation.x, deg2rad(-90), deg2rad(90)) 30 | 31 | func _physics_process(delta): 32 | 33 | direction = Vector3() 34 | 35 | if Input.is_action_just_pressed("fire"): 36 | if aimcast.is_colliding(): 37 | var bullet = get_world().direct_space_state 38 | var collision = bullet.intersect_ray(muzzle.global_transform.origin, aimcast.get_collision_point()) 39 | 40 | if collision: 41 | var target = collision.collider 42 | 43 | if target.is_in_group("Enemy"): 44 | print("hit enemy") 45 | target.health -= damage 46 | 47 | 48 | if not is_on_floor(): 49 | fall.y -= gravity * delta 50 | 51 | 52 | if Input.is_action_just_pressed("ui_cancel"): 53 | Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) 54 | 55 | if Input.is_action_just_pressed("jump") and is_on_floor(): 56 | fall.y = jump 57 | 58 | 59 | if Input.is_action_pressed("move_forward"): 60 | 61 | direction -= transform.basis.z 62 | 63 | elif Input.is_action_pressed("move_backward"): 64 | 65 | direction += transform.basis.z 66 | 67 | if Input.is_action_pressed("move_left"): 68 | 69 | direction -= transform.basis.x 70 | 71 | elif Input.is_action_pressed("move_right"): 72 | 73 | direction += transform.basis.x 74 | 75 | 76 | direction = direction.normalized() 77 | velocity = velocity.linear_interpolate(direction * speed, acceleration * delta) 78 | velocity = move_and_slide(velocity, Vector3.UP) 79 | move_and_slide(fall, Vector3.UP) 80 | 81 | --------------------------------------------------------------------------------