I’ve been trying to create a way to teleport an entity (the player) in front of another (call it Bob for explaining reasons), but I’m not sure how to do this. it always needs to be positioned a certain amount of meters away from the direction that Bob is facing. if you could, please also help me with how to make the player’s camera look in the direction of Bob’s local z value + 10. Thanks so much!
Can you have an invisible marker/object that’s always 10 in front of bob (a child of bob)?
Then you always teleport to the marker position. this approach doesn’t require trying to manually calculate where “10 in front of bob” is.
Obj.position.z += 10
I will try that real quick, thanks!
this doesn’t work, becasue I want the entity being teleported to always be in front of bob, no matter bob’s orientation.
Copy Bob’s Object3D.getWorldDirection and scale it by negative distance (-10), then add it to Bob’s position to get the world position for the teleport object.
If you want the object to remain ahead of Bob forever, attach it to Bob and set the child object’s position to (0, 0, -10)
Update the child object’s Object3D.matrixWorld every frame and tell the camera to look at it.
anidivr is correct; games often use an invisible dummy object for things like this.
So, for some more context, I am making portals. If I child an entity to the portal (Bob), then the portal will have a hitbox stretching from the portal to the child. Im using Bob’s hitbox to detect a collision, and teleport to the other portal. However, it’s teleporting to the portal, and sends you back to bob, causing an infinite cycle of teleporting. I could also shut off the portal’s hitbox until the player stops touching it as well. The getWorldDirection could work, though. I will test that soon.
Can’t you just getWorldDirection of Bob (Bobs z axis) normalise it multiply it by 10 and add it to your objects position as suggested earlier?
How do I multiply it?
obj.multplyScalar? How would I go about doing this?
if you getWorldDirection of bob, the result is fed to a vector3, let’s call this “bobsDirectionVector”, so,
this is sudo but essentially the approach i’m suggesting
ok, I can try that! seems like it’ll work!
yes, just tested, here’s a working example…
have a look at the
setPosition() function which is the solution in question…
thanks so much! That fixed it! And if you don’t mind, how would I rotate the camera in the direction of bob after being teleported? (or do I need to start a new question?) since these are portals, it’s more immersive to look in the direction the portal is facing.
You can use camera.lookAt(bob.position) if you need to adjust the camera position also, you can use the same formula as before
Ok, thanks so much! I’ve been trying to figure this out for the longest time now.
No problem, once you get used to the vast support for these types of transformations in three you’ll be flying.