Browse Source

Adding mouse movement and moving camera to player

pull/1/head
Ryan Reed 1 month ago
parent
commit
c6cf1eca31
4 changed files with 19 additions and 4 deletions
  1. +15
    -1
      scenes/characters/player.gd
  2. +3
    -0
      scenes/characters/player.tscn
  3. +1
    -0
      scenes/world/world.gd
  4. +0
    -3
      scenes/world/world.tscn

+ 15
- 1
scenes/characters/player.gd View File

@ -2,8 +2,13 @@ class_name Player
extends CharacterBody3D
@export var speed: float = 5.0
@export var jump_velocity: float = 4.5
@export var mouse_sensitivity_horizontal: float = 0.003
@export var mouse_sensitivity_vertical: float = 0.003
@export var speed: float = 5.0
@onready var camera: Camera3D = $Camera3D
func _physics_process(delta: float) -> void:
@ -13,6 +18,10 @@ func _physics_process(delta: float) -> void:
move_and_slide()
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseMotion:
handle_mouse_movement(event)
func apply_gravity(delta: float) -> void:
if is_on_floor(): return
@ -22,6 +31,11 @@ func handle_jump() -> void:
if not Input.is_action_just_pressed("jump") or not is_on_floor(): return
velocity.y = jump_velocity
func handle_mouse_movement(event: InputEvent) -> void:
rotation.y = rotation.y - event.relative.x * mouse_sensitivity_horizontal
camera.rotation.x = camera.rotation.x - event.relative.y * mouse_sensitivity_vertical
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-90), deg_to_rad(90))
func handle_movement() -> void:
var input_dir: Vector2 = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
var direction: Vector3 = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()


+ 3
- 0
scenes/characters/player.tscn View File

@ -18,3 +18,6 @@ shape = SubResource("BoxShape3D_wocps")
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.953133, 0)
mesh = SubResource("BoxMesh_rp718")
[node name="Camera3D" type="Camera3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.86156, -0.00542843)

+ 1
- 0
scenes/world/world.gd View File

@ -6,6 +6,7 @@ const BLOCK_PREFAB: PackedScene = preload("res://scenes/blocks/block.tscn")
func _ready() -> void:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
_initialize_ground()
#create_block(Block.Types.DIRT, Vector3.ZERO)
#create_block(Block.Types.STONE, Vector3.ONE)


+ 0
- 3
scenes/world/world.tscn View File

@ -26,7 +26,4 @@ environment = SubResource("Environment_sl2e5")
transform = Transform3D(-0.866025, -0.433013, 0.25, 0, 0.5, 0.866025, -0.5, 0.75, -0.433013, 0, 0, 0)
shadow_enabled = true
[node name="Camera3D" type="Camera3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 0.866025, 0.5, 0, -0.5, 0.866025, 0.1, 1.86802, 3.38687)
[node name="Player" parent="." instance=ExtResource("2_sl2e5")]

Loading…
Cancel
Save