Godot Version
4
I am having trouble, i followed this tutorial https://www.youtube.com/watch?v=6bbPHsB9TtI , and this github file ShootingMechanics/Scripts/Player.gd at main · LegionGames/ShootingMechanics · GitHub , but im still getting an error with the position of an instance??? idk what to do, cant figure it out please help
extends CharacterBody3D
const SPEED = 5.0
const JUMP_VELOCITY = 2.75
var gravity = ProjectSettings.get_setting(“physics/3d/default_gravity”)
var instance
var mouse_sensitivity = 0.3
@onready var gun_barrel = $glock19/RayCast3D
var yaw = 0.0
var pitch = 0.0
var bullet = load(“res://bullet.tscn”) # Corrected the path here
@onready var gun_anim = $glock19/AnimationPlayer
@onready var glock19 = $glock19 # Get the glock19 node directly
func _ready():
Input.mouse_mode = Input.MOUSE_MODE_HIDDEN
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _physics_process(delta):
if not is_on_floor():
velocity.y -= gravity * delta
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
if !gun_anim.is_playing():
gun_anim.play("gun_anim")
fireprojectile()
var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
var movement_direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if movement_direction:
velocity.x = movement_direction.x * SPEED
velocity.z = movement_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:
yaw -= event.relative.x * mouse_sensitivity
pitch -= event.relative.y * mouse_sensitivity
pitch = clamp(pitch, -90, 90)
$Camera3D.rotation_degrees.x = pitch
rotation_degrees.y = yaw
if Input.is_action_just_pressed("pause"):
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
if Input.is_action_pressed("fullscreen"):
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
func fireprojectile():
# Ensure gun_barrel is not null
if gun_barrel == null:
print(“Error: gun_barrel is not initialized.”)
return
var bullet_scene = preload("res://bullet.tscn")
# Instantiate the bullet
var bullet_instance = bullet_scene.instantiate()
# Position the bullet at the gun barrel's position
bullet_instance.global_transform.origin = gun_barrel.global_transform.origin
# Align the bullet's direction with the gun barrel's direction
bullet_instance.transform.basis = gun_barrel.global_transform.basis
# Set the bullet's velocity to move forward
if bullet_instance is RigidBody3D:
bullet_instance.linear_velocity = -bullet_instance.transform.basis.z * 40.0 # Adjust speed as needed
# Optionally scale the bullet (if needed)
bullet_instance.scale = Vector3(0.05, 0.05, 0.05)
# Add the bullet to the scene
get_tree().root.add_child(bullet_instance)
instance.position = gun_barrel.global_position
instance.transform.basis = gun_barrel.global_transform.basis
# Set the position of the bullet
bullet_instance.global_transform.origin = glock19.global_transform.origin
# For testing, move the bullet along a fixed direction
var test_direction = Vector3(1, 0, 0) # Replace with desired direction
if bullet_instance is RigidBody3D:
bullet_instance.linear_velocity = test_direction * 40.0 # Adjust speed as needed
bullet_instance.scale = Vector3(0.05, 0.05, 0.05) # Example scale
# Add the instance to the scene
get_tree().root.add_child(bullet_instance)