Godot Tutorial | Best Camera & Movement Code
A Relaxing Voice
extends Control
func _input(event): [tab] var MouseMode = false # Remove for console [tab] var Paused = not get_tree().paused [tab] if event.is_action_pressed("start"): [tabx2] get_tree().paused = Paused [tabx2] MouseMode = Paused # Remove for console [tabx2] if (MouseMode): Input.set_mouse_mode(0) # Remove for console [tabx2] else: Input.set_mouse_mode(2) # Remove for console
extends KinematicBody
const DIRECTION = Vector3.DOWN # Of arugment 2 of "move_and_slide_with_snap" const GRAVITY = -0.98 # Of Earth near sea-level const LENGTH = 35 # Of argument 2 of "move_and_slide_with_snap" const LIMIT = 3.1415926535897932384626433832795/180 * 50 # Slope
var A = 1 # In air var C = DIRECTION * LENGTH # To floor (argument 2 of "move_and_slide_with_snap") var Di = Vector3.ZERO var F = 20 # Jump var Fr = 20 # On ground var G: float var S = 0 # Idle var S2 = 5 # Walk var S3 = 11 # Run var V = Vector3.ZERO # Of vectors var V2 = 54 # Terminal
-# For quicker workflow:
-# A = Acceleration | An = Angle | C = Connection/Contact | D = De-acceleration | Di = Direction -# F = Force | Fr = Friction | G = Gravity | L = Limit | Le = Length (range) | M = Mass | P = Position -# R = Rotation | S = Speed | T = Threshold | V = Velocity (of vector[s]) | Q = Quantity | Z = Zone (area)
-# When a variable is a constant, they are fully written out and in all-caps. -# When a variable is a standard, they are abbreviated letters as above. -# When variables are used as a non-standard or are non-standard themselves, they are fully-written-out words -# with the first letter of the words capitalized; there are also no spaces or underscores between words unless there are -# more than 2 words. Examples:
-# ACCELERATION = constant variable -# A = standard variable -# Acceleration = standard as one-time, non-standardized variable -# CoolVariable = non-standard variable -# Cool_Sweet_Variable = non-standard, 3+ variable
-#-----------------------------------------------------------------------# View onready var HideMouse = Input.set_mouse_mode(2) # Remove for console onready var CameraPost = $"Spatial" var Angle = -50 var Angle2 = 70
func _unhandled_input(event): [tab] var Mouse = 0.1 # Mouse sensitivity [tab] if event is InputEventMouseMotion: [tabx2] rotation_degrees.y -= event.relative.x * Mouse [tabx2] CameraPost.rotation_degrees.x -= event.relative.y * Mouse [tabx2] CameraPost.rotation_degrees.x = clamp(CameraPost.rotation_degrees.x, Angle, Angle2)
func _process(_delta): [tab] var Stick = 2.5 # Joystick sensitivity [tab] rotation_degrees.y -= Input.get_action_strength("right2") * Stick - Input.get_action_strength("left2") * Stick [tab] CameraPost.rotation_degrees.x -= Input.get_action_strength("down2") * Stick - Input.get_action_strength("up2") * Stick [tab] CameraPost.rotation_degrees.x = clamp(CameraPost.rotation_degrees.x, Angle, Angle2)
-#-----------------------------------------------------------------------# Movement func _physics_process(delta): [tab] # Direction of Input [tab] var Direction = $"Spatial".global_transform.basis.get_euler().y [tab] Di = Vector3(Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left"), 0, [tab] Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")).rotated(Vector3.UP, Direction).normalized() [tab] # Speed [tab] if Input.is_action_pressed("secondary"): S = S3 [tab] else: S = S2 [tab] # Velocity, Acceleration, and Friction [tab] if is_on_floor(): V = lerp(V, Di * S, delta * Fr) [tab] else: V = lerp(V, Di * S, delta * A) [tab] # Gravity [tab] if is_on_floor(): G = -0.01 [tab] else: G = clamp(G + GRAVITY, -V2, V2) [tab] # Jump [tab] if is_on_floor() and Input.is_action_just_pressed("primary"): G = F [tab] V.y = G [tab] C = C * 0 [tab] # Movement Type [tab] V.y = move_and_slide_with_snap(V, C, Vector3.UP, true, 4, LIMIT, false).y
172115039 Bytes