Advertisement
Ragdev

Player/Main Script

Apr 16th, 2024 (edited)
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends CharacterBody3D
  2.  
  3. @export var Speed = 10.0
  4. @export var JumpStrenght = 9.0
  5. @export var Gravity = 25.0
  6.  
  7. var Velocity = Vector3.DOWN
  8. var SnapVector = Vector3.DOWN
  9.  
  10. @onready var CamArm = $CamArm
  11. @onready var Model = $Model
  12.  
  13. func _physics_process(delta):
  14.     Movement(delta)
  15.  
  16. func _process(delta):
  17.     window_activity()
  18.     CamArm.position = position
  19.  
  20. func Movement(delta):
  21.     var MoveDir = Vector3.ZERO
  22.     MoveDir.x = Input.get_action_strength("Right") - Input.get_action_strength("Left")
  23.     MoveDir.z = Input.get_action_strength("Back") - Input.get_action_strength("Forward")
  24.     MoveDir = MoveDir.rotated(Vector3.UP, CamArm.rotation.y).normalized()
  25.    
  26.     Velocity.x = MoveDir.x * Speed
  27.     Velocity.z = MoveDir.z * Speed
  28.     Velocity.y -= Gravity * delta
  29.    
  30.     var JustLanded = is_on_floor() and SnapVector == Vector3.ZERO
  31.    
  32.     if Input.is_action_just_pressed("Jump") and is_on_floor():
  33.         Velocity.y = JumpStrenght
  34.         SnapVector = Vector3.ZERO
  35.    
  36.     elif JustLanded:
  37.         SnapVector = Vector3.DOWN
  38.    
  39.     set_velocity(Velocity)
  40.     set_up_direction(Vector3.UP)
  41.     set_floor_stop_on_slope_enabled(true)
  42.     move_and_slide()
  43.     Velocity = velocity
  44.  
  45.     if Velocity.length() > 0.2:
  46.         var LookDirection = Vector2(Velocity.z, Velocity.x)
  47.         Model.rotation.y = lerp_angle(Model.rotation.y, LookDirection.angle(), delta*5)
  48.  
  49.  
  50. func window_activity():
  51.     if Input.is_action_just_pressed("ui_cancel") or Input.is_action_just_pressed("Fullscreen"):
  52.         if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
  53.             Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  54.         else:
  55.             Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
Tags: Godot Gdscript 3D
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement