xyz
@onready var cameraRig = $CameraRig
@onready var camera = $CameraRig/Camera3D
const JumpVelocity = 16
const Acceleration = 0.2
const Deceleration = 1.5
const TopSpeed = 15.0
const TempGravity = 55.0
const SmoothingTime = 4.0
var lookSensitity : float = ProjectSettings.get_setting("player/look_sensitivity")
var gravity : float = ProjectSettings.get_setting("physics/3d/default_gravity")
var floorNormal : Vector3 = Vector3()
var floorBasis : Basis = Basis()
var floorAngle : float = 0.0
func _physics_process(delta):
var new_velocity : Vector3 = velocity
if(is_on_floor()):
if (Input.is_action_just_pressed("ui_accept")):
new_velocity.y = JumpVelocity
floorAngle = get_floor_angle()
if (floorAngle > 0.4):
floorNormal = get_floor_normal()
else:
floorNormal = Vector3.UP
else:
new_velocity.y -= TempGravity * delta
floorNormal = Vector3.UP
var inputDir : Vector2 = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
var direction : Vector3 = (transform.basis * Vector3(inputDir.x, 0, inputDir.y)).normalized().rotated(Vector3.UP, cameraRig.rotation.y)
if (direction != Vector3.ZERO):
var xVelNew : float = new_velocity.x + direction.x * Acceleration
var zVelNew : float = new_velocity.z + direction.z * Acceleration
new_velocity.x = clamp(xVelNew, -TopSpeed, TopSpeed)
new_velocity.z = clamp(zVelNew, -TopSpeed, TopSpeed)
else:
new_velocity.x = move_toward(velocity.x, 0, Deceleration)
new_velocity.z = move_toward(velocity.z, 0, Deceleration)
velocity = new_velocity
floorBasis = calc_basis(get_floor_normal(), camera.rotation)
transform.basis = floorBasis
move_and_slide()
func _input(event):
if(event is InputEventMouseMotion):
cameraRig.rotate_y(-event.relative.x * lookSensitity)
camera.rotate_x(-event.relative.y * lookSensitity)
var x : float = clamp(camera.rotation.x, -PI / 2, PI / 2)
camera.rotation = Vector3(x, camera.rotation.y, camera.rotation.z)
func calc_basis(floor_normal: Vector3, camera_look: Vector3) -> Basis:
var b = Basis()
b.y = floor_normal
b.z = Plane(b.y).project(camera_look)
b.x = b.y.cross(b.z)
return b.orthonormalized()
Here is my code.
I did fix the camera so that it's on its own rig instead of the collider, but that shouldn't affect things.
I don't think I put the wrong arguments in. I'm passing a vector that keeps the floor normal if the floor angle is past a certain point, and points up otherwise. I did verify this by just passing in get_floor_normal() with the same results. And as for camera_look I'm just passing in the camera's rotation.