Godot Version
4.1.1
Question
I am making a 3d shooter game and am trying to make a menu where i can change the gun.
I havent posted before so ask if anything is unclear:
I have 2 scenes:
Node3D which is my main 3d scene
and a control scene called menu for the home screen/menu
in the menu there are 3 buttons:
The play button swaps the scene to Node3d which is correct
The weapon button goes to another scene for choosing a weapon. i have 2 weapons at the moment - a pistol and an smg:
my scene/node tree? looks like this:
These are my scripts so far…
The Player:
extends CharacterBody3D
@onready var gunRay = $Head/Camera3d/RayCast3d as RayCast3D
@onready var Cam = $Head/Camera3d as Camera3D
@onready var weapon = ‘pistol’
@onready var smg = $Head/Camera3d/Node3D2
@onready var pistol = $Head/Camera3d/Node3D
@onready var pistol_inout = $Head/Camera3d/Node3D/MSR/Stash
@onready var smg_inout = $Head/Camera3d/Node3D2/MG3/AnimationPlayer
@export var speed_modifier = 1
@export var _bullet_scene : PackedScene
@export var gravity_mult : float = 1.0
@export var mouseSensibility = 300
var mouse_relative_x = 0
var mouse_relative_y = 0
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting(“physics/3d/default_gravity”)
func _ready():
#Captures mouse and stops rgun from hitting yourself
gunRay.add_exception(self)
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
var self_var = self
get_node(“res://options_menu.gd”).connect(“smgselect”, self_var, 2)
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta * gravity_mult
# Handle Jump.
if Input.is_action_just_pressed("Jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
#Weapon switching
if Input.is_action_just_pressed("1"):
smg_inout.play("stash")
await get_tree().create_timer(0.6).timeout
smg.hide()
pistol.show()
pistol_inout.play("grab")
weapon = "Pistol"
await get_tree().create_timer(0.4).timeout
elif Input.is_action_just_pressed("2"):
pistol_inout.play("stash_pistol")
await get_tree().create_timer(0.4).timeout
pistol.hide()
smg.show()
smg_inout.play("grabsmg")
weapon = "SMG"
await get_tree().create_timer(0.6).timeout
# Handle Shooting
if Input.is_action_pressed("Shoot"):
if weapon == "SMG":
shoot_smg()
elif weapon == "Pistol":
shoot_pistol()
# Get the input direction and handle the movement/deceleration.
var input_dir = Input.get_vector("moveLeft", "moveRight", "moveUp", "moveDown")
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
move_and_slide()
func _input(event):
if event is InputEventMouseMotion:
rotation.y -= event.relative.x / mouseSensibility
$Head/Camera3d.rotation.x -= event.relative.y / mouseSensibility
$Head/Camera3d.rotation.x = clamp($Head/Camera3d.rotation.x, deg_to_rad(-90), deg_to_rad(90) )
mouse_relative_x = clamp(event.relative.x, -50, 50)
mouse_relative_y = clamp(event.relative.y, -50, 10)
func shoot_pistol():
var pistol_shoot = $Head/Camera3d/Node3D/MSR/AnimationPlayer
if !pistol_shoot.is_playing():
if not gunRay.is_colliding():
print(“not touching”)
pistol_shoot.play(“shoot”)
var bulletInst = _bullet_scene.instantiate() as Node3D
bulletInst.set_as_top_level(true)
get_parent().add_child(bulletInst)
bulletInst.global_transform.origin = gunRay.get_collision_point() as Vector3
bulletInst.look_at((gunRay.get_collision_point()+gunRay.get_collision_normal()),Vector3.BACK)
print(gunRay.get_collision_point())
print(gunRay.get_collision_point()+gunRay.get_collision_normal())
#if gunRay.is_colliding():
# if gunRay.get_collider().is_in_group(“Enemies”):
# gunRay.get_collider().hit()
func shoot_smg():
var smg_shoot = $Head/Camera3d/Node3D2/MG3/AnimationPlayer
if !smg_shoot.is_playing():
if not gunRay.is_colliding():
print(“not touching”)
return
smg_shoot.play(“shootsmg”)
var bulletInst = _bullet_scene.instantiate() as Node3D
bulletInst.set_as_top_level(true)
get_parent().add_child(bulletInst)
bulletInst.global_transform.origin = gunRay.get_collision_point() as Vector3
bulletInst.look_at((gunRay.get_collision_point()+gunRay.get_collision_normal()),Vector3.BACK)
print(gunRay.get_collision_point())
print(gunRay.get_collision_point()+gunRay.get_collision_normal())
func hit():
emit_signal(“enemy_hit”)
The 1st menu:
extends Control
func _on_play_pressed():
get_tree().change_scene_to_file(“res://levels/node_3d.tscn”)
func _on_loadout_pressed():
get_tree().change_scene_to_file(“res://options_menu.tscn”)
func _on_quit_pressed():
get_tree().quit()
The 2nd (weapon menu):
extends Control
signal smgselect
func _on_loadout_1_pressed():
emit_signal(“pistolselect”)
func _on_smg_pressed():
emit_signal(“smgselect”)
func _on_back_pressed():
get_tree().change_scene_to_file(“res://menu.tscn”)
could anyone help???