Moving a body along a given vector.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By THE HELIX

I am creating a project where the body is made of KinematicBody 2D node.I would like to get the position of the body as a “player_pos” in GD script.For example let’s take the position of the body as Vector2(450,500).How can i make the body to move to a new position ,where the body’s position changes to new value let it be Vector2(700,800).Any suggestions?

And I forgot the body should change position when i add a code like
if Input.Vector2 (20,40) < Vector2(450,500) :
player_pos = Vector2(700,800)
and sorry for my bad english and sorry for coding mistakes.

THE HELIX | 2019-01-12 06:01

Thanks for ur help kidscancode . How can i contact u if i have further doubts in the project.

THE HELIX | 2019-01-13 13:50

Is it possible to use accelerometer in movement of a KinematicBody2D or RigidBody2d in a 2D game.
If possible can you help me in coding and movement of body ?

THE HELIX | 2019-01-13 14:19

:bust_in_silhouette: Reply From: kidscancode

A KinematicBody2D’s position is already stored in a member variable called position, so there’s no need to create another variable.

As for moving, when you move a KinematicBody2D, you need to use either the move_and_collide() or move_and_slide() methods. You supply these methods with your desired motion vector.

There is a detailed explanation with examples in the official docs:

Thanks for your answer. But how to access position in gd script. Can you please help in coding ?

THE HELIX | 2019-01-12 16:25

You access the property by using its name. This would print the body’s coordinates when you press “run”:

func _ready():

I really recommend you read through the “Getting Started” section of the docs. All of this basic information is covered.

kidscancode | 2019-01-12 16:54