Godot Version
v4.3.stable.official
Question
I tried to create a hook with a solid movement and I followed a tutorial in YouTube which used Godot 3 instead of 4, so I tried my best to fix it but at the end I had a break point at line 24 which is :
$Raycast.look_at(get_global_mouse_position())
and it says Invalid call. nonexistent function ‘look_at’ in base ‘Node’.
I also have break point in the hook() part
can you help? and also can you say what my problems are in general in the process?
So this is my code :
extends CharacterBody2D
const SPEED = 130.0
const JUMP_VELOCITY = -200.0
var Sgravity = 100var motion = Vector2()
var Hook_pos = Vector2()
var hooked
var Rlength = 500
var CRlengthfunc get_hook_pos():
for raycast in $Raycast.get_children():
if raycast.is_colliding():
return raycast.get_collision_point()func Ready():
CRlength = Rlengthfunc hook():
$Raycast.look_at(get_global_mouse_position())
if Input.is_action_just_pressed(“left_click”):
Hook_pos = get_hook_pos()
if Hook_pos:
hooked = true
CRlength = global_position.distance_to(Hook_pos)
if Input.is_action_just_pressed(“right_click”):
hooked = false16:49 video about hook
func _draw():
var pos = global_position
if hooked:
draw_line(Vector2(0,5), to_local(Hook_pos), Color(1,1,1,0.25), 3, true)
else:
return
var colliding = $Raycast.is_colliding()
var collide_point = $Raycast.get_collision_point()
if colliding and pos.distance_to(collide_point) < Rlength:
draw_line(Vector2(0,5), to_local(collide_point), Color(0.35, 0.7 , 0.9), 0.5, true)func swing(delta):
var radius = global_position - Hook_pos
if motion.length() < 0.01 or radius.length() < 10: return
var angle = acos(radius.motion/(motion.length()*radius.length()))
var rad_vel = cos(angle)*motion.length()
motion += radius.normalized() * -rad_vel
if global_position.distance_to(Hook_pos) > CRlength:
global_position = Hook_pos + radius.normalized() * CRlength
motion += (Hook_pos - global_position).normalized() * 1500 * deltafunc gravity():
motion.y += Sgravityfunc _physics_process(delta: float) → void:
hook()
if hooked :
gravity()
swing(delta)
motion += 0.975
velocity = motion
move_and_slide()
motion = velocityAdd the gravity.
if not is_on_floor():
velocity += get_gravity() * deltaHandle jump.
if Input.is_action_just_pressed(“ui_accept”) and is_on_floor():
velocity.y = JUMP_VELOCITYGet the input direction and handle the movement/deceleration.
As good practice, you should replace UI actions with custom gameplay actions.
var direction := Input.get_axis(“ui_left”, “ui_right”)
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)move_and_slide()