# WorldPosition with LinearVelocity

Hi guys. I have a question. How can I set world position for an object with setting linearVelocity as well??
This is example of my code:

from bge import logic, events
from mathutils import Vector

def floor():
cont = logic.getCurrentController()
own = cont.owner
scene = logic.getCurrentScene()
keyboard = logic.keyboard

``````first_floor = Vector([-0.35608, 0, -2.4525])

if keyboard.events[events.ZEROKEY] == logic.KX_INPUT_ACTIVE:
own.worldPosition = first_floor
own.setLinearVelocity((0.0,120.0,0.0),True)
``````

if name == ‘main’:
floor()

I get this world position after pressing “0” but get it imediatelly not with my velocity. Why? Can anybody help?

Is that object parented to something else?

Is it an ‘actor’?

Nope. It has only always actuator with python script.

Nope to which question? If you don’t have any physics enabled on it then it won’t move with velocity. You can still move a physics-less object, but by adjusting its position.

Hmmm think that nope to both. Could you tell me how i should add physic then? When i do getLinearVelocity i receive value when i change position constantly by myself

velocity get’s added every pulse so True pulse need to be on. Then your script waits till the button is pressed, once it’s pressed it will execute the code in single pulse, thus moving it a tiny bit or a lot (120 velocity is a lot).

so you can either write an other function, or get the velocity out of the function, or program a way so it can be run in true pulse.

also your code can be shortened to:

``````from bge import logic, events

def floor(cont):

own = cont.owner
scene = own.scene
keyboard = logic.keyboard

first_floor = [-0.35608, 0, -2.4525]

if keyboard.events[events.ZEROKEY] == logic.KX_INPUT_ACTIVE:
own.worldPosition = first_floor

own.setLinearVelocity([0.0,120.0,0.0],True)
``````

Thank you very much, will try to do this like that and will give you a feedback

Unfortunately it is not working as i wanted
i have “always” sensor with pulse==True and what i want to achieve is to by one keyboard click “4” get position from Vector e.g. [0, 0, 2] with seted velocity eg to 0.2. Is it possible?

i’m not sure what your goal is here.

so on hitting a key you want to grab a vector and set a speed? or move it to that vector by the speed? or how do i need to see this?

i guess you want to move it and set the 0.2 speed

``````from bge import logic, events

def floor(cont):

own = cont.owner
keyboard = logic.keyboard

#set the current position on startup into a property to be used
if not 'old_pos' in own:
own['old_pos'] = own.worldPosition

#default speed
x=0
y=0
z=0

#the vectors as worldPosition xyz
first_floor = [0.0, 1.0, 0.0]

#if the button is pressed
if keyboard.events[events.ZEROKEY] == logic.KX_INPUT_ACTIVE:

#set the position
own.worldPosition = first_floor

#set the new position as old
own['old_pos'] = own.worldPosition.copy()

#if current and old_pos is not the same
if own.worldPosition != own['old_pos']:

#set a speed on y axis
y = 0.2

# set/activate the speed/movement
own.localLinearVelocity = [x,y,z]
``````

#edit
code was not working so made it work

Thank you! This works for me

1 Like