Grappling/Swinging System in Godot 4 3D

63 Views Asked by At

I am fairly new to Godot, but have a little experience with Unity and Python. My goal is to have a grappling system like the one used in the game Karlson, where you can swing back and fourth/side to side, but still with gravity/momentum applied. I have spent quite a few hours trying different things - I started by following a tutorial for a pulling style grappling system by Garbaj, then started looking through different tutorials, Godot Documentation, and trying stuff on my own. At first I thought that using a Pinjoint would work, but it seems you cant change the connection node value within code. So now I think that the solution could be to lock the raycast to the position where it collides when the player left clicks ("shoot"), and then somehow calculate swinging based off of the player position and the raycast collision position, but I have no clue how to go about doing that.

Here is my current code, right now it just pulls me towards the collision point. Variables:

@onready var ray_cast_3d = $RayCast3D
@onready var camera_3d = $Neck/Head/Camera3D
@onready var grapplecast = $Neck/Head/grapplecast
var grappling = false
var grapple_point = Vector3.ZERO
var grapple_point_get = false

Grapple Function

func grapple(delta):
    if Input.is_action_pressed("shoot"):
        if grapplecast.is_colliding():
            if not grappling:
                grappling = true
    else:
        grappling = false
    if grappling:
        if not grapple_point_get:
            grapple_point = grapplecast.get_collision_point()
            grapple_point_get = true
        if grapple_point.distance_to(transform.origin) > 1:
            if grapple_point_get:
                transform.origin = lerp(transform.origin, grapple_point, delta * 1.5)
    else:
        grappling = false
        grapple_point_get = false
    if Input.is_action_just_released("shoot"):
        velocity.y += 5

Any idea how to change this to allow for swinging, or am I going in a completely wrong direction here?

1

There are 1 best solutions below

0
Jay Mehta On

I would do a finite state machine with one state being normal platformer movement and one being pendulum physics like in @cak3_lover 's comment.