Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```
- # This script is derived from the 3d movement template script
- extends CharacterBody3D
- var input_dir = Vector2(0 , 0)
- var direction = Vector3(0, 0, 0)
- const SPEED = 5.0
- const JUMP_VELOCITY = 5
- var walljump = false
- var ignore_input = 0
- # Get the gravity from the project settings to be synced with RigidBody nodes.
- var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
- func _physics_process(delta):
- # Add the gravity.
- if not is_on_floor():
- if is_on_wall() and velocity.y < 0:
- velocity.y -= gravity / 4 * delta
- else:
- velocity.y -= gravity * delta
- if is_on_floor():
- walljump = false
- # Handle Jump.
- if Input.is_action_just_pressed("ui_accept") and is_on_floor():
- velocity.y = JUMP_VELOCITY
- # Handle Wall Jump
- if Input.is_action_just_pressed("ui_accept") and is_on_wall_only():
- walljump = true
- velocity = get_wall_normal() * SPEED
- velocity.y = JUMP_VELOCITY
- # Ignore Input makes it so you cant move or turn for a bit after the jump
- ignore_input = 20
- if get_wall_normal().y < 0:
- $"3DGodotRobot".set_rotation(Vector3(0,-asin(get_wall_normal().x),0))
- else:
- $"3DGodotRobot".set_rotation(Vector3(0,asin(get_wall_normal().x),0))
- # Get the input direction and handle the movement/deceleration.
- input_dir = Input.get_vector("Left", "Right", "Forward", "Backward")
- direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
- if direction and ignore_input == 0:
- velocity.x = lerp(velocity.x,direction.x * SPEED,0.5)
- velocity.z = lerp(velocity.z,direction.z * SPEED,0.5)
- # Turn the model
- if input_dir.x < 0:
- $"3DGodotRobot".set_rotation(Vector3(0,lerp_angle($"3DGodotRobot".get_rotation().y,-acos(input_dir.y),0.25),0))
- else:
- $"3DGodotRobot".set_rotation(Vector3(0,lerp_angle($"3DGodotRobot".get_rotation().y,acos(input_dir.y),0.25),0))
- if velocity.y == 0:
- $"3DGodotRobot/AnimationPlayer".play("Run")
- elif ignore_input == 0:
- velocity.x = move_toward(velocity.x, 0, SPEED)
- velocity.z = move_toward(velocity.z, 0, SPEED)
- if velocity.y == 0:
- $"3DGodotRobot/AnimationPlayer".play("Idle")
- # Play jumping and falling animations
- if velocity.y > 0:
- if walljump:
- $"3DGodotRobot/AnimationPlayer".play("WallJump")
- else:
- $"3DGodotRobot/AnimationPlayer".play("Jump")
- if velocity.y < 0:
- if is_on_wall():
- $"3DGodotRobot/AnimationPlayer".play("WallSlide")
- else:
- $"3DGodotRobot/AnimationPlayer".play("Fall")
- # Tick down the ignore input
- ignore_input -= 60 * delta
- if ignore_input < 0:
- ignore_input = 0
- move_and_slide()
- ```
Advertisement
Advertisement