- Home /
How can I make a collision sensitive teleporter for a 2D game #c
Hello Unity folks,
I am trying to create a Teleporter which is able to feel the location of collision and transform the place of impact to the same place on the second exit teleporter (Like the 3d plane collider). I have tried multiple scripts and tutorials but I cannot come up with any solution. Most of the tutorials were meant for 3d projects.
The teleporter pad is stretched from left to right. The collision will be from under as my player is floating upwards.
I have tried to use the 3d plane collider but got an error because I couldn't use it with the 2d physics engine of unity.
I appreciate any help as this is really a struggle for me.
With kind regards and thanks in advance.
How about some pictures or something? Screenshots? I think I get what you're asking, but without knowing/seeing more to be certain, I don't want to waste our time.
If I understand, you can use your collision event's contacts. On collision, transform the first contact into local space, set the teleported object's position to the other pad's position + the transformed point-of-contact.
http://docs.unity3d.com/ScriptReference/Collision2D-contacts.html
I have added a screenshot to be more clear about the problem. Is this clear enough?
Answer by AlwaysSunny · Feb 09, 2015 at 04:15 AM
I think so. Screenshots / diagrams always help. If these pairs-of-teleporters always share the same size and orientation, the easiest solution might be...
//OnTriggerEnter
Vector3 offset = object.position - teleporterA.position;
object.position = teleporterB.position + offset;
Might have the offset backwards, I'm a little decaffeinated. If teleporters didn't share orientations, things might get interesting. Lemme know if this works out, and good luck,
using UnityEngine;
using System.Collections;
public class Teleporter : $$anonymous$$onoBehaviour {
public object TeleportTo;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter (Collider other) {
Vector3 offset = object.position - teleporterA.position;
object.position = teleporterB.position + offset;
}
}
Something like this? I am missing some connections because I get a lot of red references. I am not really experienced with scripting I am a beginner, sorry :p