The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

–1 vote

move the player to raycast2d Collision Point

in Engine by (108 points)
edited by

1 Answer

0 votes

Whats your player node? KinematicBody2D?

Anyways, in most cases, something like this should be enough

$Player.global_position = $RayCast2D.get_collision_point( )

You should previously check if the raycast did collide with something .
For better answer, you should provide more information. What type of node is your player? is the raycast child of the player? Do you want to make the movement in the player's script or anywhere else?

by (3,505 points)

My player node is a KinematicBody2D, yes the raycast is son of the player and i want it in the player script thanks :D

Ok, so in the player script, you need to do something like:

global_position = $RayCast2D.get_collision_point( )

You should previously check if the raycast collided with $RayCast2D.is_colliding()

that worked but i don't want to teleport...

I'm trying to do this:

func _proces(delta):

func raycast(delta):
if Input is action pressed("activateraycast"):
position = $RayCast2D.getcollisionpoint( ) * delta

but its not going with the time its teleporting

Ah ok, yo dont want to move the body to the point instantly.. that wasnt clear in the question. So there are many ways. You first need to define if you want to move it a constant speed, or move it in a specific time, etc... also, is it a top down game? what would be your case?
For example, for constant speed, you may do something like this:

func _process(delta):
    if Input.is_action_pressed("activate_raycast"):
        direction = global_position.direction_to($RayCast2D.get_collision_point())
        velocity = direction * speed
        velocity = move_and_slide(velocity)
   else: #only use this if you want to stop movement after releasing
        velocity = Vector2.ZERO

with speed a float and velocity a Vector2 global variables

would this be similar for 3d? I've been struggling to make it work

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.