You can do this with magic (shader)!
Secret: Cutting Assistant In Half
Of course, we know she is never actually cut in half. So, there has to be some logical explanation for it all. Well, he certainly relies on the help of an assistant alright. But it’s not one – he has two of them!
https://www.editorchoice.com/magic-tricks-revealed/
as you see in the below picture we need two persons or objects to achieve this effect.
you can achieve this effect easily by using world space shader that I explained before
I did this with childing twin but you can use code to teleport.
Shader "Smkgames/worldSpaceFade" {
Properties {
_Size("Size",Vector) = (2,0,2,0)
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_BumpMap ("Bumpmap", 2D) = "bump" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
Pass {
ZWrite On
ColorMask 0
}
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows alpha:fade
#pragma target 3.0
sampler2D _MainTex,_BumpMap;
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
float3 worldPos: TEXCOORD2;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
float4 _Size;
void vert (inout appdata_full v, out Input o){
UNITY_INITIALIZE_OUTPUT(Input,o);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
}
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o) {
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
float4 equation = (IN.worldPos.x/_Size.x)*(IN.worldPos.x/_Size.x);
float4 InvertEquation = (IN.worldPos.z/_Size.z)*(IN.worldPos.z/_Size.z);
float4 finalEquiation = max(equation,InvertEquation);
o.Albedo = c.rgb;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = step(finalEquiation,0.5);
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
}
ENDCG
}
FallBack "Diffuse"
}
Object Pooling Concept
another concept that you should know is Object Pooling.
when one person or object arrived to the destination then move it back to the first position and her twin will appears.
Dynamic Teleport
better way is Teleporting by code:
using UnityEngine;
public class Teleport : MonoBehaviour
{
public Transform teleportTarget;
private void OnTriggerEnter(Collider other) {
var pos = other.transform.position;
pos.x = teleportTarget.position.x;
other.transform.position = pos;
}
}