- Home /
[WITH IMAGES] Portal teleporter works, but only when walking through very slowly
Hey all. I'm creating a game in which a player is able to pass a portal through a portal and back. The scene looks like this:
For some reason, the player does not get teleported to the other portal, unless I walk through the portal very slowly. So the code does function, but just very poorly.
Both portals are made up of a render plane (for the illusion of looking through the portal) and an invisible collider plane. The collider plane has the following script attached:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PortalTeleporter : MonoBehaviour
{
public Transform player;
// Receiver is the position of the collider plane of the other portal
public Transform receiver;
private bool playerIsOverlapping = false;
// Update is called once per frame
void Update() {
if (playerIsOverlapping)
{
Vector3 portalToPlayer = player.position - transform.position;
float dotProduct = Vector3.Dot(transform.up, portalToPlayer);
// If this is true: The player has moved across the portal
if (dotProduct < 0f)
{
//Teleport him!
Debug.Log("Teleport!");
float rotationDiff = -Quaternion.Angle(transform.rotation, receiver.rotation);
rotationDiff += 180;
player.Rotate(Vector3.up, rotationDiff);
Vector3 positionOffset = Quaternion.Euler(0f, rotationDiff, 0f) * portalToPlayer;
player.position = receiver.position + positionOffset;
playerIsOverlapping = false;
}
}
}
void OnTriggerEnter(Collider other)
{
playerIsOverlapping = true;
Debug.Log("Player is now overlapping");
}
void OnTriggerExit(Collider other)
{
playerIsOverlapping = false;
Debug.Log("Player is no longer overlapping");
}
}
Is something wrong with my code? I've followed this tutorial step by step, yet the player gets teleported very inconsistently or not at all.
I hope I've given you enough information. Thanks a lot for your time and help!
Answer by Azehlf · May 22, 2020 at 01:16 PM
Hello ! I'm not very experienced but it may have something to do with Collision Detection in the Rigidbody 2D : https://docs.unity3d.com/Manual/class-Rigidbody2D.html Setting it to "Continuous" instead of "Discrete" may solve the issue. Hope it helps !
Thanks for your help! Unfortunately I don't think that's the issue because my Player (Character controller) actually doesn't use a Rigidbody.
These are the components of the Player and the ColliderPlane.
Hmmm, maybe try with fixupdate, if you have a slow pc it may not have the time to update correctly. Does the "OnTriggerEnter" message displays at high speed collision ?
Actually, now that you mention it: yes. The console does report overlapping every time I pass through the gate. It even displays 'Teleporting!' accordingly, every single time. So the only issue then is that the player doesn't get teleported each time.
So I suppose that line 25 through 34 do not function well?
Your answer
![](https://koobas.hobune.stream/wayback/20220612234944im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Object Changing Y Coordinate in Game View 0 Answers
Strange graphical bug in Editor GUI 1 Answer
Lighting bug Unity? 0 Answers
Flickering issue when objects touch terrain. 1 Answer
Character moves on its own 1 Answer