# Teleporting

So I have a unit that can teleport.

What I need is for him to check for the first ray sensor that detects no obstructions and then change his position 15 or so units in that direction.

I wrote a script that works swimmingly except that I tried using self.localPosition[0] += 15 for example to get him to move.

Well, he moves but no matter which way he is facing self.localPosition[0] += 15 always sends him the same world direction.

so if he faces east and an enemy approaches from the east, he will move himself west. but if he faces north and an enemy approaches from the north he will still teleport west.

is there a way to have him move in essence backward from his local orientation no matter the aforementioned orientation?

“local” and “world” positional identifiers are only for seperating children parameters from global parameters. The “local” position will always align with the global orientation, which means that the way you’re doing it won’t work.
Honestly, the way I’d do it is, have an empty attached to the character (or some other target object, maybe a mouse reticle that defines exactly where to teleport to?) at that spot, and set your character’s world position to that object’s world position when you teleport. (With a tap sensor, of course, otherwise you’d be going to the next spot over and over instantly forever!)

That’s how I do it when I’m in control of the character. But right now I’m working on having the ai do it for himself.

Then the “pre-attached empty in the specific teleport spot” idea is probably the way to go.

If you have a direction you can simply say:

obj.worldPosition += direction * 15

where direction is the normalized vector difference between the destination point and the position of the object, in world coordinates.

That is:

start = obj.worldPosition
end = mathutils.Vector((x,y,z))
direction = end - start
direction.normalize()

thanks pgi. lemme try that

own.applyMovement((15,0,0),1)

or

offset = Vector([15,0,0])
offset = own.worldPosition+(own.worldOrientation*offset)
own.worldPosition=offset

In addition to what pgi said, if you want to move forward in a direction based on his orientation, then you’d have to use the worldOrientation property for this purpose. But his answer probably is what you need (though end can just be other_position.copy() rather than making an entirely new vector out of its components if it’s already in Vector form).

own.applyovement([0,15,0], True)
Is probably what you are looking for.

Perfecto. I finally decided i wasted too much time trying to find this. I didn’t know what applyMovement was.

Thanks everyone. You’re all handsome and smart and are impeccable squash players.