Advertisement
asseater64

Untitled

Apr 8th, 2024
793
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends CharacterBody3D
  2.  
  3. @onready var camera_mount = $camera_mount
  4. @onready var animation_player = $visual/RoundChar/AnimationPlayer
  5. @onready var visual = $visual
  6.  
  7. const JUMP_VELOCITY = 4.5
  8.  
  9. @export var SPEED = 5.0
  10. @export var sens_horizontal = 0.2
  11. @export var sens_vertical = 0.2
  12.  
  13. # Get the gravity from the project settings to be synced with RigidBody nodes.
  14. var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
  15.  
  16. func _ready():
  17.     Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
  18.     animation_player.speed_scale = 2
  19.  
  20.  
  21. func _input(event):
  22.     if event is InputEventMouseMotion:
  23.         var yToRotate = event.relative.x * sens_horizontal
  24.         rotate_y(deg_to_rad(-yToRotate))
  25.         visual.rotate_y(deg_to_rad(yToRotate))
  26.         camera_mount.rotate_x(deg_to_rad(-event.relative.y * sens_vertical))
  27.  
  28.  
  29. func _physics_process(delta):
  30.     # Add the gravity.
  31.     if not is_on_floor():
  32.         velocity.y -= gravity * delta
  33.  
  34.     # Handle jump.
  35.     if Input.is_action_just_pressed("ui_accept") and is_on_floor():
  36.         velocity.y = JUMP_VELOCITY
  37.  
  38.     # Get the input direction and handle the movement/deceleration.
  39.     # As good practice, you should replace UI actions with custom gameplay actions.
  40.     var input_dir = Input.get_vector("left", "right", "forward", "backward")
  41.     var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
  42.     if direction:
  43.         play_anim("Run")
  44.         visual.look_at(position + direction)
  45.         velocity.x = direction.x * SPEED
  46.         velocity.z = direction.z * SPEED
  47.     else:
  48.         play_anim("Idle")
  49.         velocity.x = move_toward(velocity.x, 0, SPEED)
  50.         velocity.z = move_toward(velocity.z, 0, SPEED)
  51.  
  52.     move_and_slide()
  53.  
  54.  
  55. func play_anim(anim_name: String):
  56.     if (animation_player.current_animation != anim_name):
  57.         animation_player.play(anim_name)
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement