extends CharacterBody3D const BULLET = preload("res://Scenes/bullet.tscn") @onready var bullet: CharacterBody3D = $"../Bullet" var speed = 20.0 var speed_y = 3 var initial_position var initial_rotation var mouse_sensitivity := 0.001 var twist_input := 0.0 var pitch_input = 0.0 var forwardV = Vector3.FORWARD var rightV = Vector3.RIGHT var upV = Vector3.UP var twist_angle = 0 var pitch_angle = 0 var initial_twist var score = 0 var bullet_start # Called when the node enters the scene tree for the first time. func _ready() -> void: initial_position = position initial_rotation = rotation initial_twist = rotation.y bullet_start = bullet.position - position func _physics_process(delta: float) -> void: var input_dir := Vector3(0, 0, 0) input_dir.x = Input.get_axis("ui_left", "ui_right") input_dir.y = 0 # Input.get_axis("ui_page_up", "ui_page_down") input_dir.z = Input.get_axis("ui_up", "ui_down") var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.z)).normalized() if input_dir: velocity.x = direction.x * speed velocity.y = -speed_y * input_dir.y velocity.z = direction.z * speed else: velocity.x = move_toward(velocity.x, 0, speed) velocity.y = move_toward(velocity.y, 0, speed) velocity.z = move_toward(velocity.z, 0, speed) rotate_view() if Input.is_action_just_pressed("ui_accept"): fire() var collision_info = move_and_collide(velocity*delta, true) if collision_info: if collision_info.get_collider().name.contains("Bullet"): print("I am hit") move_and_slide() func rotate_view(): rotation = initial_rotation rotate_x(pitch_angle) rotate_y(twist_angle) forwardV = forwardV.rotated(Vector3(0, 1, 0), twist_input) rightV = rightV.rotated(Vector3(0, 1, 0), twist_input) func move_camera(delta:float): var input = Input.get_action_strength("ui_up") position = position + input * forwardV * speed * delta input = Input.get_action_strength("ui_down") position = position - input * forwardV * speed * delta input = Input.get_action_strength("ui_left") position = position - input * rightV * speed * delta input = Input.get_action_strength("ui_right") position = position + input * rightV * speed * delta input = Input.get_action_strength("ui_page_up") position = position + input * upV * speed * delta input = Input.get_action_strength("ui_page_down") position = position - input * upV * speed * delta func _unhandled_input(event: InputEvent) -> void: if event is InputEventMouseMotion and Input.is_mouse_button_pressed(1) : twist_angle += -event.relative.x * mouse_sensitivity pitch_angle += -event.relative.y * mouse_sensitivity if pitch_angle > deg_to_rad(180) and pitch_angle < deg_to_rad(300): pitch_angle = deg_to_rad(300) elif pitch_angle < deg_to_rad(-60): pitch_angle = deg_to_rad(-60) elif pitch_angle > deg_to_rad(60) and pitch_angle <= deg_to_rad(180): pitch_angle = deg_to_rad(60) func reset(): position = initial_position rotation = initial_rotation pitch_angle = 0 twist_angle = 0 func fire(): var direction = transform.basis * bullet_start bullet.rotation = Vector3(90, 0, 0) bullet.rotate_y(twist_angle) bullet.position = position + direction