extends CharacterBody3D # Enum for weapon states enum WeaponState { UNEQUIPPED, EQUIPPED } var current_state = WeaponState.UNEQUIPPED @export var S_H = 0.1 @export var S_v = 0.1 @onready var animation_player = $MeshRoot/AuxScene/AnimationPlayer @onready var C_M = $Node3D var SPEED = 2 const JUMP_VELOCITY = 4.5 var running = false var running_speed = 5 var walking_speed = 2 # Get the gravity from the project settings to be synced with RigidBody nodes. var gravity = ProjectSettings.get_setting("physics/3d/default_gravity") func _ready(): Input.mouse_mode = Input.MOUSE_MODE_CAPTURED func _input(event): if event is InputEventMouseMotion: rotate_y(deg_to_rad(-event.relative.x*S_H)) C_M.rotate_x(deg_to_rad(-event.relative.y*S_v)) func _physics_process(delta): if Input.is_action_pressed("run"): SPEED = running_speed running = true else: SPEED = walking_speed running = false # Add the gravity. if not is_on_floor(): velocity.y -= gravity * delta # Handle jump. if Input.is_action_just_pressed("ui_accept") and is_on_floor(): velocity.y = JUMP_VELOCITY # Get the input direction and handle the movement/deceleration. # As good practice, you should replace UI actions with custom gameplay actions. var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down") var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() if direction: if running: if animation_player.current_animation != "running": animation_player.play("Running(2)0") else: if animation_player.current_animation != "WalkWithBriefcase0": animation_player.play("WalkWithBriefcase0") velocity.x = direction.x * SPEED velocity.z = direction.z * SPEED else: if animation_player.current_animation != "idle": animation_player.play("Idle(3)0") velocity.x = move_toward(velocity.x, 0, SPEED) velocity.z = move_toward(velocity.z, 0, SPEED) move_and_slide()
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter