fps sample setup
This commit is contained in:
26
scripts/player.gd
Normal file
26
scripts/player.gd
Normal file
@@ -0,0 +1,26 @@
|
||||
extends CharacterBody3D
|
||||
|
||||
const SPEED = 5.0
|
||||
const JUMP_VELOCITY = 4.5
|
||||
|
||||
func _physics_process(delta):
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
|
||||
# Get the input direction and handle the movement/deceleration.
|
||||
# As good practice, you should replace UI actions with custom gameplay actions.
|
||||
var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
|
||||
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()
|
||||
1
scripts/player.gd.uid
Normal file
1
scripts/player.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://ccj8w85qobhpl
|
||||
68
scripts/player_camera.gd
Normal file
68
scripts/player_camera.gd
Normal file
@@ -0,0 +1,68 @@
|
||||
# taken from
|
||||
# https://github.com/tavurth/godot-simple-fps-camera/blob/master/player/Camera.gd
|
||||
#
|
||||
extends Camera3D
|
||||
|
||||
@onready var Player = get_parent()
|
||||
|
||||
## Increase this value to give a slower turn speed
|
||||
const CAMERA_TURN_SPEED = 400
|
||||
|
||||
func _ready():
|
||||
## Tell Godot that we want to handle input
|
||||
set_process_input(true)
|
||||
|
||||
func look_updown_rotation(new_rotation = 0):
|
||||
"""
|
||||
Returns a new Vector3 which contains only the x direction
|
||||
We'll use this vector to compute the final 3D rotation later
|
||||
"""
|
||||
var toReturn = self.get_rotation() + Vector3(new_rotation, 0, 0)
|
||||
|
||||
##
|
||||
## We don't want the player to be able to bend over backwards
|
||||
## neither to be able to look under their arse.
|
||||
## Here we'll clamp the vertical look to 90° up and down
|
||||
toReturn.x = clamp(toReturn.x, PI / -2, PI / 2)
|
||||
|
||||
return toReturn
|
||||
|
||||
func look_leftright_rotation(new_rotation = 0):
|
||||
"""
|
||||
Returns a new Vector3 which contains only the y direction
|
||||
We'll use this vector to compute the final 3D rotation later
|
||||
"""
|
||||
return Player.get_rotation() + Vector3(0, new_rotation, 0)
|
||||
|
||||
func _input(event):
|
||||
"""
|
||||
First person camera controls
|
||||
"""
|
||||
##
|
||||
## We'll only process mouse motion events
|
||||
if not event is InputEventMouseMotion:
|
||||
return
|
||||
|
||||
##
|
||||
## We'll use the parent node "Player" to set our left-right rotation
|
||||
## This prevents us from adding the x-rotation to the y-rotation
|
||||
## which would result in a kind of flight-simulator camera
|
||||
Player.set_rotation(look_leftright_rotation(event.relative.x / -CAMERA_TURN_SPEED))
|
||||
|
||||
##
|
||||
## Now we can simply set our y-rotation for the camera, and let godot
|
||||
## handle the transformation of both together
|
||||
self.set_rotation(look_updown_rotation(event.relative.y / -CAMERA_TURN_SPEED))
|
||||
|
||||
func _enter_tree():
|
||||
"""
|
||||
Hide the mouse when we start
|
||||
"""
|
||||
Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
|
||||
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
||||
|
||||
func _leave_tree():
|
||||
"""
|
||||
Show the mouse when we leave
|
||||
"""
|
||||
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
|
||||
1
scripts/player_camera.gd.uid
Normal file
1
scripts/player_camera.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bs7pcycdeiwqx
|
||||
Reference in New Issue
Block a user