How to make a Vector with the same angle as a certain object?

Hi Im making a Portal style game in Unity but in 2D. And I got stuck while making the portal system (Im a Unity noob). I want my character to be shoot out of the portal that he exited in the direction that the portal is facing (with Rigidbody2D.AddForce).

I also want the AddForce vector “power” to be dependent of the velocity that the character has before entering the first portal. I have tried everything I could have come up with and I cant seem to figure it out (without making a ton of IF statements… sigh). Any help Please?! :slight_smile:

Im kind of bad at explaining so I made 2 pictures in Paint to demonstrate (excuse my shitty Paint skills :wink: ):

You could ‘undo’ the effect of the entered portals rotation, by multiplying by the inverse of its rotation. The at the other end, redo the rotation by multiplying by the new portals rotation:

The following is pseudo code

Vector3 velocity;
Quaternion rotation;
Vector3 position;

void OnEnterPortal(Collider2D collider){
	if(collider is not a portal) return; //make sure we are hitting a portal

	GameObject thisPortal = collider.gameObject;
	GameObject otherPortal; //get your other portal somehow

	//setup mapping rotations
	Quaternion undoRot = Quaternion.Inverse(thisPortal.transform.rotation);
	Quaternion redoRot = otherPortal.transform.rotation;

	velocity = undoRot * ourCharacter.velocity;
	rotation = ourCharacter.rotation * undoRot;
	position = undoRot * (ourCharacter.position - thisPortal.transform.position);

	//Set new variables
	ourCharacter.position = otherPortal.transform.position + (redoRot * position);
	ourCharacter.rotation = rotation * redoRot;
	ourCharacter.velocity = redoRot * velocity;
}

Hope that helps!