- Home /
C# Make portals take direction into account
I have portals in my game where the player enters at high speed and exits the other portal with the speed they went in with. This works only if both portals are facing opposite (<- ->). But if the portal you are exiting is not facing the opposite direction of the portal you are exiting (<- ^) you come flying out the side/back of the portal.
Is there a way that I can shift my velocity to the direction the portal is facing?
My very simple portal code:
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == "Player")
{
other.transform.position = receiverPortal.transform.position;
}
}
Answer by AlwaysSunny · Mar 05, 2015 at 03:59 AM
Assuming the simplest case (an object's relative direction and point-of-entry are inconsequential) you could simply set the direction of the player's velocity to the portal's normal and keep the magnitude:
// hit teleporter
// do teleport
rigidbody.velocity = portal.transform.forward * rigidbody.velocity.magnitude;
If those things DO matter, your solution might involve projecting a ray along your velocity against the portal you've entered, transforming that ray from EntryPortal space to ExitPortal space, then applying it to the body.