Is there any way to move an object to another object or to global coordinates, instead of moving in a certain direction, in the game engine?
for example to move the player to a wall when he gets near it.
In a python script:
cont = GameLogic.getCurrentController()
own = cont.owner
own.setPosition([0, 5, 3]) # or wherever you need
Or, to move to another object:
cont = GameLogic.getCurrentController()
own = cont.owner
scene = GameLogic.getCurrentScene()
object2 = scene.getObjectList () ["OBobject2"] # the name is OB + object name
own.setPosition(object2.getPosition())
this does just set the new postion, doesn’t it? I want to have it like an animation with a fluid frame transition. Like in Far Cry 2 where you can press a key when you are near a damaged car which you can repair, and the camera moves to it automatically before the repair-animation of the hands is played. I need it to let the player go to a door automatically when he gets near it.
(another example: in Metro 2033 you can press ‘use’ near a damaged gate, and the player moves to it and pulls automatically, even if you didn’t stand directly in front of it.)
You could use an iterative loop to make smooth transitions, an example:
smoothMove.blend (135 KB)
change the sensitivity in the script to a larger value to make it move slower/smoother, given it’s a person’s movements, it moves a tad too fast when it is set to 10.
better use
- a trackto actuator to face the right direction (the target object)
- a motion actuator that let your object move forward.
Detect if the object is near the target with a near sensor (small amount).
The easiest is to use a small invisible plane as target (empties are not sensed by near). When you start the animation, place the target at the position you want (e.g. with the code from post 2). Make the target an actor that it can be sensed by the near sensor.
When the target is sensed, deactivate the motion and trackto actuators (e.g. by switching states). Place the object at the exact position of the target (e.g. with the above code).
If you need an example let me know.
Alternative way (time based):
Prent your object to an target. which can be an empty.
Enable slow parent with an reasonable offset.
When you move the target (e.g. with the above code) the object will follow the target, but with different speed (dependent on the distance). It will also follow the orientation of the target!
Let me know if you need a demo