Teleport 2D Character on TriggerEnter
I am currently making a simple game for year 1 game design, and for some reason just can't figure out how to make my 2D character teleport along the X axis when he enters and presses "e" a trigger. Even helping me get him to move only upon entering the trigger, and not having to press a button would be of great help. Here is my current void OnTriggerEnter2D(note: the top portion is obviously irrelevant to the translation):
void OnTriggerEnter2D(Collider2D col)
{
if (col.CompareTag("Coins"))
{
Destroy(col.gameObject);
gm.points += 1;
}
if(col.CompareTag("Door") && Input.GetKeyDown("e"))
{
transform.position = new Vector2(transform.position.x + speed, 0);
print("door working");
}
}
I also have this way above the prior bit of code:
public float speed = 50f;
Answer by DiGiaCom-Tech · May 27, 2017 at 03:24 AM
Please check this link out. It is written for 3D but can be easily adapted for 2D.
http://answers.unity3d.com/questions/1136481/making-portals.html?childToView=1195273#comment-1195273
Answer by ismaelnascimento01 · May 26, 2017 at 07:41 PM
And do the Input.GetKeyDown("e")) in other local in script.
I'm confused as to how that link would help me. I have gotten it to work before, in a similar script. The other script I used was when I teleported the character into an entirely new scene, ins$$anonymous$$d of just along the X axis.
And why do you mean when you say "other local in script"?
Change line: transform.position = new Vector2(transform.position.x + speed, 0);
For: transform.localPosition = new Vector2(transform.position.x + speed, 0);
And check these links: https://docs.unity3d.com/ScriptReference/Transform-localPosition.html - https://docs.unity3d.com/ScriptReference/Vector3.$$anonymous$$oveTowards.html
Thank you very much, I don't know why I didn't think of making it localPosition.