Advertisement
noob_killer012345678

Untitled

Aug 2nd, 2023
1,802
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ```
  2. # This script is derived from the 3d movement template script
  3.  
  4. extends CharacterBody3D
  5.  
  6. var input_dir = Vector2(0 , 0)
  7. var direction = Vector3(0, 0, 0)
  8.  
  9. const SPEED = 5.0
  10. const JUMP_VELOCITY = 5
  11. var walljump = false
  12. var ignore_input = 0
  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.  
  17. func _physics_process(delta):
  18.     # Add the gravity.
  19.     if not is_on_floor():
  20.         if is_on_wall() and velocity.y < 0:
  21.             velocity.y -= gravity / 4 * delta
  22.         else:
  23.             velocity.y -= gravity * delta
  24.    
  25.     if is_on_floor():
  26.         walljump = false
  27.    
  28.     # Handle Jump.
  29.     if Input.is_action_just_pressed("ui_accept") and is_on_floor():
  30.         velocity.y = JUMP_VELOCITY
  31.     # Handle Wall Jump
  32.     if Input.is_action_just_pressed("ui_accept") and is_on_wall_only():
  33.         walljump = true
  34.         velocity = get_wall_normal() * SPEED
  35.         velocity.y = JUMP_VELOCITY
  36.         # Ignore Input makes it so you cant move or turn for a bit after the jump
  37.         ignore_input = 20
  38.         if get_wall_normal().y < 0:
  39.             $"3DGodotRobot".set_rotation(Vector3(0,-asin(get_wall_normal().x),0))
  40.         else:
  41.             $"3DGodotRobot".set_rotation(Vector3(0,asin(get_wall_normal().x),0))
  42.    
  43.  
  44.     # Get the input direction and handle the movement/deceleration.
  45.     input_dir = Input.get_vector("Left", "Right", "Forward", "Backward")
  46.     direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
  47.     if direction and ignore_input == 0:
  48.         velocity.x = lerp(velocity.x,direction.x * SPEED,0.5)
  49.         velocity.z = lerp(velocity.z,direction.z * SPEED,0.5)
  50.        
  51.         # Turn the model
  52.         if input_dir.x < 0:
  53.             $"3DGodotRobot".set_rotation(Vector3(0,lerp_angle($"3DGodotRobot".get_rotation().y,-acos(input_dir.y),0.25),0))
  54.         else:
  55.             $"3DGodotRobot".set_rotation(Vector3(0,lerp_angle($"3DGodotRobot".get_rotation().y,acos(input_dir.y),0.25),0))
  56.         if velocity.y == 0:
  57.             $"3DGodotRobot/AnimationPlayer".play("Run")
  58.        
  59.     elif ignore_input == 0:
  60.         velocity.x = move_toward(velocity.x, 0, SPEED)
  61.         velocity.z = move_toward(velocity.z, 0, SPEED)
  62.         if velocity.y == 0:
  63.             $"3DGodotRobot/AnimationPlayer".play("Idle")
  64.    
  65.     # Play jumping and falling animations
  66.     if velocity.y > 0:
  67.         if walljump:
  68.             $"3DGodotRobot/AnimationPlayer".play("WallJump")
  69.         else:
  70.             $"3DGodotRobot/AnimationPlayer".play("Jump")
  71.        
  72.     if velocity.y < 0:
  73.         if is_on_wall():
  74.             $"3DGodotRobot/AnimationPlayer".play("WallSlide")
  75.         else:
  76.             $"3DGodotRobot/AnimationPlayer".play("Fall")
  77.            
  78.     # Tick down the ignore input
  79.     ignore_input -= 60 * delta
  80.     if ignore_input < 0:
  81.         ignore_input = 0
  82.    
  83.     move_and_slide()
  84. ```
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement