Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends CharacterBody3D
- @onready var camera_mount = $camera_mount
- @onready var animation_player = $visual/RoundChar/AnimationPlayer
- @onready var visual = $visual
- const JUMP_VELOCITY = 4.5
- @export var SPEED = 5.0
- @export var sens_horizontal = 0.2
- @export var sens_vertical = 0.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
- animation_player.speed_scale = 2
- func _input(event):
- if event is InputEventMouseMotion:
- var yToRotate = event.relative.x * sens_horizontal
- rotate_y(deg_to_rad(-yToRotate))
- visual.rotate_y(deg_to_rad(yToRotate))
- camera_mount.rotate_x(deg_to_rad(-event.relative.y * sens_vertical))
- func _physics_process(delta):
- # 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("left", "right", "forward", "backward")
- var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
- if direction:
- play_anim("Run")
- visual.look_at(position + direction)
- velocity.x = direction.x * SPEED
- velocity.z = direction.z * SPEED
- else:
- play_anim("Idle")
- velocity.x = move_toward(velocity.x, 0, SPEED)
- velocity.z = move_toward(velocity.z, 0, SPEED)
- move_and_slide()
- func play_anim(anim_name: String):
- if (animation_player.current_animation != anim_name):
- animation_player.play(anim_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement