[Solved]Portal-Style Game: Rotating Velocity

I’ve been trying to create something similar to Portal (the game) but I’ve hit one snag. I’m not entirely sure scripting-wise how to transfer the velocity once the object enters the portal. I’ll try to explain better: Say I have two portals. Portal A is located on the floor, and Portal B is located on the wall. I drop a cube through portal A. Its velocity looks something like (0, -1, 0) when it exits from Portal B. The velocity should be ‘rotated’, so that its end velocity looks like (1, 0, 0). I can code that individual case, but it should work regardless of what surface it enters and exits from. Of course I don’t expect a whole script, but a little demonstration snippet or some ideas would be nice. Thanks in advance!

Set the velocity equal to the portal’s transform.forward. Or maybe -transform.forward, depending on which way it’s facing.

–Eric

Thanks for the reply, but unless I’m misunderstanding you, that won’t work. The object’s velocity won’t/shouldn’t always equal 1.

Off the top of my head, I’d do it like this:

• store the velocity in a Vector3 variable
• assign the correct vector (x, y, or z) of that variable to the appropriate vector (x,y, or z) of RigidBody’s velocity

Lemme give an example. Let’s say it’s falling down a hole in the floor, then coming out a hole in the wall on one side of the screen.

``````var v3_velocity = rigidbody.velocity;
//teleport the GameObject to the hole in the wall
rigidbody.velocity.x = v3_velocity.y; //this might need to be negative
rigidbody.velocity.y = v3_velocity.z;
rigidbody.velocity.z = v3_velocity.x;
``````

Those assignments are probably totally wrong, but you get the idea. You’ll need to create algorithms for each of the transformations (downward to leftward, downward to rightward, leftward to rightward, rightward to upward, etc.).

And … I’ve never played Portal so I’m totally guessing what you want. If I misunderstood, I’ll just say “whoops”!

Well, multiply the portal’s transform.forward by the rigidbody’s velocity.magnitude, and the result is the rigidbody’s new velocity.

–Eric

I can tell you right now that none of these previous solutions will give you what you’re looking for.

I think you’re after something like this:

block.rigidbody.velocity = portal.transform.TransformDirection(block.rigidbody.velocity)

Actually, no, that won’t work, but it’s close. Going to test this…

Edit:

``````var OtherPortal : GameObject;
var TeleportOffset : float;

function OnTriggerEnter(collider)
{
if (collider.GetComponent(Rigidbody))
{
var relPoint = transform.InverseTransformPoint(collider.transform.position);
var relVelocity = -transform.InverseTransformDirection(collider.rigidbody.velocity);
collider.rigidbody.velocity = OtherPortal.transform.TransformDirection(relVelocity);
collider.transform.position = OtherPortal.transform.TransformPoint(relPoint) + (collider.rigidbody.velocity.normalized * TeleportOffset);
}
}
``````

Works great. Pop it in any object that has a trigger collider and point it to the destination.

I beg to differ. From what I said in my last post:

``````var otherPortal : Transform;
var teleportOffset = 1.0;

function OnTriggerEnter (other : Collider) {
other.rigidbody.velocity = otherPortal.forward * other.rigidbody.velocity.magnitude;
other.transform.position = otherPortal.position + otherPortal.forward * teleportOffset;
}
``````

No need to make things over-complicated…

–Eric

Eric, DallonF is correct (though I might choose to do something more simple for position - but that depends on how precisely you want your portals to work). Your solution would always make the object come out of the portal, moving into the direction the portal is facing. If you look at the game “Portal”, you will see that that is not how it is working there. Instead, when you move into the portal with a sideways velocity, you want to come out of the other end with the same sideways motion, transformed to the new portal’s direction.

1 Like

Wow, that does exactly what I was looking for, and it works great. Thanks! I appreciate everyone’s help, even if I didn’t use your solutions.

Just wanted to say thanks for this, saved me a ton of time. Greatly appreciated.