I have two rectangles on each side of the screen which are set as triggers. When the object the script is attached to, enters one of the rectangles, it teleports to the other rectangle while keeping the same y coordinates and velocity.
I would like to know how to do this in C#. Could someone please give me some pointers on how to do so? I’m new to C# and it’s a bit confusing to me.
You can set the object’s position to the other trigger’s position - but keeping the original Y coordinate. Place this script in both triggers, and set the otherTrigger variable in each one to the other trigger:
public Transform otherTrigger; // drag the other trigger here
void OnTriggerEnter(Collider col){
if (col.transform.name=="ObjName"){ // only teleports objects with the right name
Vector3 newPos = otherTrigger.position; // get position of the other trigger...
newPos.y = col.transform.position.y; // but keep the object's y
col.transform.position = newPos; // teleport the object
}
}