Godot Version
v4.3.stable.official [77dcf97d8]
Question
Hello! sorry if this question has been asked before, I’ve tried a lot of things and either got confused or they didn’t work. I figured if someone could personally help me, that might be the best option.
I am new to Godot and coding in general, and am using GDscript for my game.
I am using a CharacterBody2D for the player character, as well as a CollisionShape2D
I’d like to make a grappling hook that can swing the player, and also can be activated by mouse click. so if you click somewhere, the grappling hook will connect to that object that is clicked or clicked near.
I’d also like a grappling hook which the length can be changed by scrolling the scroll wheel up and down.
I figure I’ll have to use a RayCast2D, and have the raycast look at where the mouse is positioned, but I’m kind of lost on what to do next.
Do I use a joint node for the swing physics? Would that work with a CharacterBody2D?
here’s my current code for my player node!
it includes jump buffering, coyote time, and allowing the player to fall faster over time.
extends CharacterBody2D
const SPEED = 900.0
const JUMP_VELOCITY = -1500.0
const BONUS_GRAVITY = 2.0
const GRAVITY = 50
const CHAIN_PULL = 105
var air_time = 0.0
var chain_velocity := Vector2(0,0)
var can_jump = true
var jump_buffer_frames = 0
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var coyote_timer: Timer = $CoyoteTimer
func _physics_process(delta: float) -> void:
if can_jump == false and is_on_floor():
can_jump = true
# jump buffer
if jump_buffer_frames > 0:
if is_on_floor():
jump()
jump_buffer_frames = 0
else:
jump_buffer_frames -= 1
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# fall faster over time
if is_on_floor():
air_time = 0.0
else:
air_time += delta + 1
velocity.y += (GRAVITY * air_time * BONUS_GRAVITY) * delta
# Handle jump.
if Input.is_action_just_pressed("jump"):
if is_on_floor():
jump()
else:
jump_buffer_frames = 15
if (is_on_floor()== false) and can_jump == true and coyote_timer.is_stopped():
coyote_timer.start()
if coyote_timer.time_left > 0:
if Input.is_action_just_pressed("jump"):
jump()
# direction input, left: -1, idle: 0, right: 1
var direction := Input.get_axis("move_left", "move_right")
# flip sprite
if direction > 0 :
animated_sprite.flip_h = false
elif direction < 0:
animated_sprite.flip_h = true
# play animations
if is_on_floor():
if direction == 0:
animated_sprite.play("idle")
else:
animated_sprite.play("run")
else:
animated_sprite.play("jump")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
#grapple hook
func jump():
velocity.y = JUMP_VELOCITY
can_jump = false
func _on_coyote_timer_timeout() -> void:
can_jump == false