![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Noddy |
;TLDR I’m trying to use the mouse’s x coord to rotate the RigidBody FPS controller I made using angular velocity.
Right now I’m trying to create a RigidBody FPS controller. Not new I know, but the main issue is that I find myself disliking most of the other examples that are out on the market since they add so many of their own physics calculations it makes it seem pointless to use a RigidBody instead of a KinematicBody.
So far I’ve actually gotten some janky but decent progress. Though right now I’m trying to use the x coord of the mouse to rotate the RigidBody controller using angular_velocity/state.set_angular_velocity. It’s set up pretty much like the Godot Docs FPS Tutorial, but I’m trying to replace self.rotate_y()
with my own physics integrated version since I’m trying to work out some of the jank that’s present. Good news about that, I can get the camera rotating using it, bad news, it won’t stop.
Here is my code:
extends RigidBody
var MOUSE_SENSITIVITY : float = 0.2
var look_vector : Vector3 = Vector3()
onready var rotation_helper = $Rotation_Helper
onready var camera = $Rotation_Helper/Camera
onready var feet = $Feet
var speed : float = 10
var max_speed : float = 20
var jump_speed : float = 10
var deacceleration : float = 0.1
var rot_y : float = 0
# Called when the node enters the scene tree for the first time.
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _physics_process(_delta):
if Input.is_action_just_pressed("ui_cancel"):
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
else:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func integrated_rotate_y(state, radians : float):
state.set_angular_velocity(Vector3.UP * radians)
func _integrate_forces(state):
if Input.is_action_pressed("move_forward"):
linear_velocity += (Vector3.FORWARD * speed).rotated(Vector3.UP, rotation.y)
if Input.is_action_pressed("move_backward"):
linear_velocity += (Vector3.BACK * speed).rotated(Vector3.UP, rotation.y)
if Input.is_action_pressed("move_left"):
linear_velocity += (Vector3.LEFT * speed).rotated(Vector3.UP, rotation.y)
if Input.is_action_pressed("move_right"):
linear_velocity += (Vector3.RIGHT * speed).rotated(Vector3.UP, rotation.y)
if Input.is_action_just_pressed("move_jump") and feet.is_colliding():
linear_velocity.y += jump_speed
integrated_rotate_y(state, rot_y)
linear_velocity.x = clamp(linear_velocity.x, -max_speed, max_speed)
linear_velocity.y = clamp(linear_velocity.y, -jump_speed, jump_speed)
linear_velocity.z = clamp(linear_velocity.z, -max_speed, max_speed)
linear_velocity.x = lerp(linear_velocity.x, 0, deacceleration)
linear_velocity.z = lerp(linear_velocity.z, 0, deacceleration)
func _input(event):
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
rotation_helper.rotate_x(deg2rad(event.relative.y * MOUSE_SENSITIVITY * -1))
rot_y += deg2rad(event.relative.x * MOUSE_SENSITIVITY * -1)
rotation_helper.rotation_degrees.x = clamp(rotation_helper.rotation_degrees.x, -70, 70)
And my node setup looks like:
Player : RigidBody (mass = 3, continuous_cd, contact_monitor, axis lock: ang x, ang z)
=>Rotation_Helper : Spatial (translation.y = 1)
==>Camera (current, fov = 90)
=>CollisionShape (Shape = capsule, rotation_degrees.x = 90)
=>Feet : Raycast (cast_to = (0, -1.5, 0)