working on player character
This commit is contained in:
61
Scripts/mouse_pos.gd
Normal file
61
Scripts/mouse_pos.gd
Normal file
@@ -0,0 +1,61 @@
|
||||
# GLOBAL SCRIPT. ACCESS via RaycastSystem.foo()
|
||||
# Raycast queries are defined here. All other modules can use it.
|
||||
|
||||
extends Node3D
|
||||
|
||||
const RAY_LENGTH := 1000
|
||||
|
||||
"""
|
||||
Uses default collision_mask. But can be overrided for custom collision
|
||||
mask/layers.
|
||||
Output format:
|
||||
output_dict = {
|
||||
"collider": None, # The colliding object (if any)
|
||||
"collider_id": None, # The colliding object's ID (if any)
|
||||
"normal": [0, 0, 0], # The surface normal at the intersection point
|
||||
"position": [0, 0, 0], # The intersection point
|
||||
"face_index": -1, # The face index at the intersection point
|
||||
"rid": None, # The intersecting object's RID
|
||||
"shape": None # The shape index of the colliding shape
|
||||
}
|
||||
"""
|
||||
# Returns raycast result after it hits an object in the world.
|
||||
# @return Dictionary or null
|
||||
func _do_raycast_on_mouse_position(collision_mask: int = 0b00000000_00000000_00000000_00000001):
|
||||
# Raycast related code
|
||||
var space_state = get_world_3d().direct_space_state
|
||||
var cam = get_viewport().get_camera_3d()
|
||||
var mousepos = get_viewport().get_mouse_position()
|
||||
|
||||
var origin = cam.project_ray_origin(mousepos)
|
||||
var end = origin + cam.project_ray_normal(mousepos) * RAY_LENGTH
|
||||
var query = PhysicsRayQueryParameters3D.create(origin, end)
|
||||
query.collide_with_areas = true
|
||||
query.collision_mask = collision_mask
|
||||
|
||||
var result = space_state.intersect_ray(query) # raycast result
|
||||
return result
|
||||
|
||||
|
||||
|
||||
# # # # # # # # # # # # # # # # # #
|
||||
# # # # # # # # # # # # # # # # # #
|
||||
# API below:
|
||||
|
||||
|
||||
# Gets ray-cast hit position from camera to world.
|
||||
# @return Vector3 or null
|
||||
func get_mouse_world_position(collision_mask: int = 0b00000000_00000000_00000000_00000001):
|
||||
var raycast_result = _do_raycast_on_mouse_position(collision_mask)
|
||||
if raycast_result:
|
||||
return raycast_result.position
|
||||
return null
|
||||
|
||||
|
||||
# Gets ray-cast hit object from camera to world.
|
||||
# @return Object or null
|
||||
func get_raycast_hit_object(collision_mask: int = 0b00000000_00000000_00000000_00000001):
|
||||
var raycast_result = _do_raycast_on_mouse_position(collision_mask)
|
||||
if raycast_result:
|
||||
return raycast_result.collider
|
||||
return null
|
||||
1
Scripts/mouse_pos.gd.uid
Normal file
1
Scripts/mouse_pos.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://jinjgqiuwyt7
|
||||
34
Scripts/player.gd
Normal file
34
Scripts/player.gd
Normal file
@@ -0,0 +1,34 @@
|
||||
extends CharacterBody3D
|
||||
class_name Player
|
||||
|
||||
var input_dir
|
||||
|
||||
@onready var body: MeshInstance3D = $Body
|
||||
|
||||
|
||||
const MOVE_SPEED = 10.0
|
||||
const MOVE_TRANSITION_SPEED = 7.0
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if is_on_floor():
|
||||
standard_movement(delta)
|
||||
|
||||
body_look_at_mouse()
|
||||
apply_gravity(delta)
|
||||
move_and_slide()
|
||||
|
||||
func body_look_at_mouse():
|
||||
var mouse_raycast = MousePos.get_mouse_world_position()
|
||||
if mouse_raycast != null:
|
||||
body.look_at(Vector3(mouse_raycast.x,body.global_position.y,mouse_raycast.y),Vector3.UP)
|
||||
|
||||
func apply_gravity(delta):
|
||||
if !is_on_floor():
|
||||
velocity.y -= 9.8 * delta
|
||||
|
||||
func standard_movement(delta):
|
||||
input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
|
||||
|
||||
if input_dir != null:
|
||||
velocity.x = lerp(velocity.x, input_dir.x * MOVE_SPEED,delta * MOVE_TRANSITION_SPEED)
|
||||
velocity.z = lerp(velocity.z, input_dir.y * MOVE_SPEED,delta * MOVE_TRANSITION_SPEED)
|
||||
1
Scripts/player.gd.uid
Normal file
1
Scripts/player.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bcgt64gerg8pv
|
||||
16
Scripts/player_camera.gd
Normal file
16
Scripts/player_camera.gd
Normal file
@@ -0,0 +1,16 @@
|
||||
extends Camera3D
|
||||
class_name PlayerCamera
|
||||
|
||||
@export var player : Player
|
||||
|
||||
const CAM_MOVE_SPEED = 5
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
follow_player(delta)
|
||||
|
||||
|
||||
func follow_player(delta):
|
||||
var player_pos = player.global_position
|
||||
|
||||
global_position.x = lerp(global_position.x,player_pos.x,delta * CAM_MOVE_SPEED)
|
||||
global_position.z = lerp(global_position.z,player_pos.z,delta * CAM_MOVE_SPEED)
|
||||
1
Scripts/player_camera.gd.uid
Normal file
1
Scripts/player_camera.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bici4k5vkuoos
|
||||
Reference in New Issue
Block a user