#i need help with code this is an

1 messages · Page 1 of 1 (latest)

blazing minnow
#

code for the grappling hook the grapple attach to the object but i want the player to the grapple pls help the node2d has 2 childs ray cast and line2d here the code: extends Node2D

@onready var ray_cast: RayCast2D = $RayCast2D
@onready var player = get_parent()
var target: Vector2
var is_grappling = false
var grapple_speed = 300 # Speed at which the player moves towards the grapple point
var velocity = Vector2.ZERO # Declare the velocity

func _physics_process(delta):
ray_cast.look_at(get_global_mouse_position()) # Point the raycast to the mouse position

if Input.is_action_just_pressed("grapple"):
    if ray_cast.is_colliding():  # If the ray hits something
        target = ray_cast.get_collision_point()  # Store the grapple target
        is_grappling = true
        $Line2D.visible = true  # Show the grapple line

if is_grappling:
    # Calculate direction from the player to the target
    var direction = (target - global_position).normalized()  # Direction towards target
    velocity = direction * grapple_speed  # Set the player's velocity towards the target

    # Move the player towards the target using velocity
    global_position += velocity * delta  # Apply velocity over time

    # Update Line2D points
    $Line2D.points = [Vector2.ZERO, to_local(target)]  # to_local for Line2D

if Input.is_action_just_released("grapple"):
    retract_grapple()  # Release the grapple when the button is released

func retract_grapple():
is_grappling = false
$Line2D.visible = false # Hide the grapple line
velocity = Vector2.ZERO # Stop the player when retracting

hoary smelt
#

You don't specify what type of objects they are. You seem to be moving it via global_position, which is generally reserved for things which don't have their own movement options

blazing minnow
#

can you change it for me i dont basicaly understand pls it will be a lot of help

#

the old code is buggy