-
-
Save bramreth/d9634f4cbbb96f9273622f5c78cd3672 to your computer and use it in GitHub Desktop.
extends RigidBody3D | |
var mouse_sensitivity := 0.001 | |
var twist_input := 0.0 | |
var pitch_input := 0.0 | |
@onready var twist_pivot := $TwistPivot | |
@onready var pitch_pivot := $TwistPivot/PitchPivot | |
func _ready() -> void: | |
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) | |
# Called every frame. 'delta' is the elapsed time since the previous frame. | |
func _process(delta: float) -> void: | |
var input := Vector3.ZERO | |
input.x = Input.get_axis("move_left", "move_right") | |
input.z = Input.get_axis("move_forward", "move_back") | |
apply_central_force(twist_pivot.basis * input * 1200.0 * delta) | |
var aligned_force = twist_pivot.basis * input | |
if Input.is_action_just_pressed("ui_cancel"): | |
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) | |
twist_pivot.rotate_y(twist_input) | |
pitch_pivot.rotate_x(pitch_input) | |
pitch_pivot.rotation.x = clamp(pitch_pivot.rotation.x, | |
deg_to_rad(-30), | |
deg_to_rad(30) | |
) | |
twist_input = 0.0 | |
pitch_input = 0.0 | |
func _unhandled_input(event: InputEvent) -> void: | |
if event is InputEventMouseMotion: | |
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED: | |
twist_input = - event.relative.x * mouse_sensitivity | |
pitch_input = - event.relative.y * mouse_sensitivity |
Error at line 20 (unused variable)
Hello i am getting a error at Line 14
Select correct path for the @onready
variables at lines 7 and 8.
Error at line 20 (unused variable)
Yeah it isn't included in the tutorial video. I just deleted line 21, "var aligned_force = twist_pivot.basis * input" and everything started working fine.
Hello I got an error at the line 27
extends CharacterBody3D
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
var mouse_sensitivity := 0.001
var twist_input := 0.0
var pitch_input := 0.0
@onready var twist_pivot := $TwistPivot
@onready var pitch_pivot := $TwistPivot/PitchPivot
@onready var camera := $TwistPivot/PitchPivot/Camera3D
var lock_on := false
var locked_twist := 0.0
var locked_pitch := 0.0
func _ready() -> void:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _physics_process(delta):
var gravity = -9.8 # Define the gravity value
if is_on_floor():
if Input.is_action_just_pressed("ui_accept"):
velocity.y = JUMP_VELOCITY
else:
velocity.y += gravity * delta # Apply gravity when not on floor
var input_dir = Vector2()
input_dir.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
input_dir.y = Input.get_action_strength("move_backward") - Input.get_action_strength("move_forward")
var direction = (twist_pivot.global_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()
func _process(delta: float) -> void:
if Input.is_action_just_pressed("ui_cancel"):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
if lock_on:
twist_pivot.rotation.y = locked_twist
pitch_pivot.rotation.x = locked_pitch
else:
twist_pivot.rotate_y(twist_input)
pitch_pivot.rotate_x(pitch_input)
pitch_pivot.rotation.x = clamp(pitch_pivot.rotation.x, deg_to_rad(-30), deg_to_rad(30))
twist_input = 0.0
pitch_input = 0.0
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseMotion:
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED and not lock_on:
twist_input -= event.relative.x * mouse_sensitivity
pitch_input -= event.relative.y * mouse_sensitivity
elif event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT and Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
elif event.button_index == MOUSE_BUTTON_RIGHT:
if event.pressed:
lock_on = true
locked_twist = twist_pivot.rotation.y
locked_pitch = pitch_pivot.rotation.x
else:
lock_on = false
for anyone who's having issues, this code seems to work for me. :)
You should still be able to follow along with his video in terms of scene trees just make sure the pitch and twist pivots are correct and I used a characterbody3d instead of a rigidbody3d. (Just watch his video and then use this code if you have issues)
Bro using this code in godot 4 the player just fips down starts to roll how can i fix it
Bro using this code in godot 4 the player just fips down starts to roll how can i fix it
Turn on Deactivation -> Lock Rotation in the properties for the base player object
whatever I do, ANYTHING I do I keep getting errors in lines 39 and 40!!! >⌓<
Script inherits from native type 'RigidBody3D', so it can't be assigned to an object of type: 'MeshInstance3D'
Hello i am getting a error at Line 14