add player rotating towards the mouse

This commit is contained in:
Teatov 2025-02-17 21:29:01 +10:00
parent 87c864263e
commit 2116490e1f
3 changed files with 30 additions and 9 deletions

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=4 format=3 uid="uid://b73y71y3efmv"]
[gd_scene load_steps=5 format=3 uid="uid://b73y71y3efmv"]
[ext_resource type="Script" path="res://scripts/player.gd" id="1_xt3i8"]
@ -8,6 +8,9 @@ height = 1.8
[sub_resource type="CapsuleMesh" id="CapsuleMesh_dtg5r"]
height = 1.8
[sub_resource type="BoxMesh" id="BoxMesh_fdktb"]
size = Vector3(0.5, 0.5, 0.5)
[node name="Player" type="CharacterBody3D"]
script = ExtResource("1_xt3i8")
@ -18,3 +21,7 @@ shape = SubResource("CapsuleShape3D_14v7g")
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.9, 0)
mesh = SubResource("CapsuleMesh_dtg5r")
[node name="MeshInstance3D2" type="MeshInstance3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.17779, 0.357218)
mesh = SubResource("BoxMesh_fdktb")

View File

@ -10,6 +10,8 @@
[node name="Test" type="Node"]
[node name="Player" parent="." instance=ExtResource("2_f4ehn")]
process_priority = 1
process_physics_priority = 1
[node name="Geometry" type="Node" parent="."]
@ -24,17 +26,19 @@ size = Vector3(50, 1, 50)
[node name="Lights" type="Node" parent="."]
[node name="DirectionalLight3D" type="DirectionalLight3D" parent="Lights"]
transform = Transform3D(-0.409414, -0.775634, 0.480389, 0, 0.52654, 0.85015, -0.912349, 0.348063, -0.215573, 0, 0, 0)
transform = Transform3D(0.994881, 0.0858869, -0.0532522, 0, 0.526956, 0.849893, 0.101056, -0.845542, 0.524258, 0, 0, 0)
shadow_enabled = true
[node name="Cameras" type="Node" parent="."]
[node name="FollowingCamera" type="Camera3D" parent="Cameras"]
fov = 20.0
near = 35.0
far = 100.0
script = ExtResource("2_7j742")
height_offset = 0.5
distance = 50.0
angle_degrees = Vector3(-35, 45, 0)
angle_degrees = Vector3(-35, -45, 0)
[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
environment = SubResource("Environment_wctar")

View File

@ -19,6 +19,7 @@ func _ready() -> void:
func _process(_delta: float) -> void:
_mouse_turning()
_process_respawning()
@ -26,12 +27,25 @@ func _physics_process(delta: float) -> void:
if not is_multiplayer_authority():
return
_mouse_turning()
_lateral_movement(delta)
_vertical_movement(delta)
move_and_slide()
func _mouse_turning() -> void:
var mouse_pos := get_viewport().get_mouse_position()
var camera := Referencer.main_camera
var from := camera.project_ray_origin(mouse_pos)
var direction := camera.project_ray_normal(mouse_pos)
var plane := Plane(Vector3.UP, global_position)
var intersection: Variant = plane.intersects_ray(from, direction)
if not intersection:
return
look_at(intersection as Vector3, Vector3.UP, true)
func _process_respawning() -> void:
if global_position.y < _respawn_height:
global_position = _respawn_point
@ -39,15 +53,11 @@ func _process_respawning() -> void:
func _lateral_movement(delta: float) -> void:
var input_dir := Input.get_vector(
"move_left", "move_right", "move_up", "move_down"
)
var input_dir := Input.get_vector("move_left", "move_right", "move_up", "move_down")
var has_input := input_dir.length() > 0
if has_input:
var direction := (
(transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
)
var direction := Vector3(input_dir.x, 0, input_dir.y).normalized()
var new_velocity := (direction * MOVE_SPEED).rotated(
Vector3.UP, Referencer.main_camera.rotation.y
)