Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CrouchCharacter_1
- Scene_tree
- https://pasteboard.co/26gm9Ut5Xgj6.png
- CharacterBody3d Script:
- '''
- extends CharacterBody3D
- const SPEED = 5.0
- const JUMP_VELOCITY = 4.5
- #set the crouch component
- @export var crouch_component: CrouchComponent
- # 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():
- velocity.y -= gravity * delta
- # Handle crouch.
- crouch_component.can_uncrouch = not move_and_collide(crouch_component.grow_dir + Vector3(0,.2,0),true)
- # 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("ui_left", "ui_right", "ui_up", "ui_down")
- var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
- if direction:
- velocity.x = direction.x * SPEED
- velocity.z = direction.z * SPEED
- else:
- velocity.x = move_toward(velocity.x, 0, SPEED)
- velocity.z = move_toward(velocity.z, 0, SPEED)
- move_and_slide()
- '''
- CrouchComponent Script:
- '''
- extends Node3D
- class_name CrouchComponent
- const CROUCH_SPEED: float = 0.1
- const MIN_HEIGHT: float = 0.2
- @export var capsule_or_cylinder_collision_shape: CollisionShape3D
- @export var mesh_instance: MeshInstance3D
- @export var print_debug: bool = false
- @onready var collision_max_height = capsule_or_cylinder_collision_shape.shape.height
- @onready var mesh_max_height = mesh_instance.mesh.height
- var crouching: bool = false
- var can_uncrouch: bool = true
- var grow_dir: Vector3 = Vector3.ZERO
- func _process(delta):
- if print_debug: print(grow_dir)
- if Input.is_action_pressed("crouch"):
- crouching = true
- else:
- crouching = false
- if crouching:
- decrease_height()
- return
- if can_uncrouch:
- increase_height()
- grow_dir = Vector3.ZERO
- return
- # Function to set the height of the capsule shape
- func set_capsule_height(collision_height: float, mesh_height: float) -> void:
- # Set the height of the capsule shape
- collision_height = clamp(collision_height,MIN_HEIGHT, collision_max_height)
- mesh_height = clamp(mesh_height,MIN_HEIGHT, mesh_max_height)
- capsule_or_cylinder_collision_shape.shape.height = collision_height
- mesh_instance.mesh.height = mesh_height
- # Adjust the position to ensure it grows from the bottom
- var parent_position = capsule_or_cylinder_collision_shape.position
- parent_position.y = collision_height / 2.0
- capsule_or_cylinder_collision_shape.position = parent_position
- var mesh_parent_position = mesh_instance.position
- mesh_parent_position.y = mesh_height / 2.0
- mesh_instance.position = mesh_parent_position
- # Example function to increase the height
- func increase_height() -> void:
- var new_collision_height = capsule_or_cylinder_collision_shape.shape.height + CROUCH_SPEED
- var new_mesh_height = mesh_instance.mesh.height + CROUCH_SPEED
- set_capsule_height(new_collision_height, new_mesh_height)
- # The CROUCH_SPEED is multiplicated with 2 because we cant to test the future grow direction
- # for collision. otherwise the objects collision would bump into the ceiling an is unable to move
- grow_dir = Vector3(0,CROUCH_SPEED * 2, 0)
- # Example function to decrease the height
- func decrease_height() -> void:
- var new_collision_height = capsule_or_cylinder_collision_shape.shape.height - CROUCH_SPEED # Ensure height does not go below 1
- var new_mesh_height = mesh_instance.mesh.height - CROUCH_SPEED
- set_capsule_height(new_collision_height, new_mesh_height)
- '''
- CrouchCharacter_2
- Scene_tree
- https://pasteboard.co/8cI6hl3dhibv.png
- CharacterBody3d Script:
- '''
- extends CharacterBody3D
- const SPEED = 5.0
- const JUMP_VELOCITY = 4.5
- const CROUCH_SPEED: float = 2
- const MIN_SCALE_Y: float = 0.2
- const MAX_SCALE_Y: float = 1.0
- @export var root: Marker3D
- # 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():
- 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("ui_left", "ui_right", "ui_up", "ui_down")
- var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
- if direction:
- velocity.x = direction.x * SPEED
- velocity.z = direction.z * SPEED
- else:
- velocity.x = move_toward(velocity.x, 0, SPEED)
- velocity.z = move_toward(velocity.z, 0, SPEED)
- handle_crouch()
- move_and_slide()
- func handle_crouch():
- var delta_time = get_process_delta_time()
- var is_crouching = Input.is_action_pressed("crouch")
- if is_crouching:
- # Crouch: Decrease the scale.y value gradually
- root.scale.y -= CROUCH_SPEED * delta_time
- root.scale.y = clamp(root.scale.y, MIN_SCALE_Y, MAX_SCALE_Y)
- else:
- # Uncrouch: Increase the scale.y value gradually, but check for collisions
- var new_scale_y = min(MAX_SCALE_Y, root.scale.y + CROUCH_SPEED * delta_time)
- # Temporarily update the scale for collision checking
- var original_scale_y = root.scale.y
- root.scale.y = new_scale_y
- # Use a small upward velocity for collision checking
- var _velocity = Vector3(0, 0.1, 0)
- # we don't want to move with move_and_collide so we set 'test_collision' to true
- var collision = move_and_collide(_velocity * delta_time, true)
- if collision:
- # If there is a collision, revert the scale
- root.scale.y = original_scale_y
- else:
- # If no collision, apply the new scale
- root.scale.y = new_scale_y
- # Ensure the scale is within bounds
- root.scale.y = clamp(root.scale.y, MIN_SCALE_Y, MAX_SCALE_Y)
- '''
Advertisement
Add Comment
Please, Sign In to add comment