Skip to content

Instantly share code, notes, and snippets.

@bramreth
Created July 24, 2023 23:52
Show Gist options
  • Save bramreth/d9634f4cbbb96f9273622f5c78cd3672 to your computer and use it in GitHub Desktop.
Save bramreth/d9634f4cbbb96f9273622f5c78cd3672 to your computer and use it in GitHub Desktop.
How to make a 3D Platfomer in Godot 4: Setup, Movement, and Camera Controls - code dump
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
@harsh160101065
Copy link

Bro using this code in godot 4 the player just fips down starts to roll how can i fix it

@python-nerd-3
Copy link

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

@Spluffy927
Copy link

whatever I do, ANYTHING I do I keep getting errors in lines 39 and 40!!! >⌓<

@teslakitty1
Copy link

Script inherits from native type 'RigidBody3D', so it can't be assigned to an object of type: 'MeshInstance3D'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment