Created
June 19, 2023 14:13
-
-
Save default1200/6c5dcb82d84e51a69e286ac2929f3efb to your computer and use it in GitHub Desktop.
Player GD with raycast and direction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Actor | |
func _physics_process(delta: float) -> void: | |
if is_on_wall(): | |
$RayCast2D.position.x = $collisionShape2D.shape.get_extents().x * direction | |
direction = direction * -1.0 | |
var is_jump_interrupted: = Input.is_action_just_released("jump") and velocity.y < 0.0 | |
var direction = get_direction() | |
velocity = calculate_move_velocity(velocity, direction, speed, is_jump_interrupted) | |
velocity = move_and_slide(velocity, FLOOR_NORMAL) | |
func get_direction() -> Vector2: | |
return Vector2( | |
Input.get_action_strength("move_right") - Input.get_action_strength("move_left"), | |
-1.0 if Input.is_action_just_pressed("jump") and is_on_floor() else 1.0 | |
) | |
func calculate_move_velocity( | |
linear_velocity: Vector2, | |
direction: Vector2, | |
speed: Vector2, | |
is_jump_interrupted: bool | |
) -> Vector2: | |
var new_velocity: = linear_velocity | |
new_velocity.x = speed.x * direction.x | |
new_velocity.y += gravity * get_physics_process_delta_time() | |
if direction.y == -1.0: | |
new_velocity.y = speed.y * direction.y | |
if is_jump_interrupted: | |
new_velocity.y = 0.0 | |
return new_velocity |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment