- Home /
How to change the movement direction between portals?
I'm making a game similar to Portal and already done the teleporter. But when the Player or other object enter on the first portal and is teleported to de other portal, he keeps the same trajectory. I wish the trajectory of the object changes based on the position of the portal. Ex: If Player or other object enter in a vertical Portal, but the other portal is in horizontal position, the Player is launched up. This is what I've already did.
using UnityEngine;
using System.Collections;
public class ObjetoPortalavel : MonoBehaviour {
public GameObject ThisObject;
public GameObject Portal1;
public GameObject Portal2;
void OnCollisionEnter2D(Collision2D collision)
{
Portal1.GetComponent<Collider2D>().enabled = true;
Portal2.GetComponent<Collider2D>().enabled = true;
if (collision.gameObject.tag == "Portal1")
{
ThisObject.transform.position = Portal2.transform.position;
ThisObject.transform.eulerAngles = Portal2.transform.eulerAngles;
Portal2.GetComponent<Collider2D>().enabled = false;
}
if (collision.gameObject.tag == "Portal2")
{
ThisObject.transform.position = Portal1.transform.position;
ThisObject.transform.eulerAngles = Portal1.transform.eulerAngles;
Portal1.GetComponent<Collider2D>().enabled = false;
}
}
// Update is called once per frame
void Update () {
Portal1.gameObject.tag = "Portal1";
Portal2.gameObject.tag = "Portal2";
}
}
Answer by FernandoGBR · Oct 01, 2017 at 07:52 AM
How are you moving your character?
I think if you make your players move by applying forces to them, and then you suddenly change their positions and rotations. It shall present a portal-like behaviour.
In case the above does not works a more complex solution would involve saving the relative speed (or kinetic energy) to the entrance portal (taking into account the relative rotation) moving the player to the other portal (as you do now) and then appying the player the same relative kinetic energy but now to the second portal.
Hope this help.
You're right! I think is how I move my character as you said. I made some changes in the script and discovered how to make what I want. And now works to any objects with this new Script. But the Player still getting problems. I'm using the standard Player's Script from the Unity, so the problem can be this. I'll make my own script to control the "Player" and see if this solve the problem. Thank's bro... Sorry for this bad english haha
Your answer
![](https://koobas.hobune.stream/wayback/20220612140031im_/https://answers.unity.com/themes/thub/images/avi.jpg)