- Home /
Teleport 2D Player to another room
Okay, so I'm an artist trying to figure out this code thing and prototype my game. Please forgive my complete lack of knowledge. I've been doing a lot of trial and error and can't find an answer that works.
I want my player to transport from point A to point B upon entering a trigger and pressing "space" to simulate going into a building (as illustrated in image). Whenever the character walks into the trigger collider with the Teleport script, nothing happens. I tried to make an empty game object the destination, and that didn't work, so I just entered the x and y axis straight into the code and still no results. What I would like is to have a public variable for the x and y axis so I can adjust those easier and reuse the code, but I couldn't figure it out if you look at my Inspector. I would also like to press "space" to teleport once I actually figure out how to teleport.
I've tried several scripts I found in the forums and none work. This is the best I get without any error messages:
#pragma strict
var destination : Transform;
function OnTriggerEnter(other : Collider)
{
if (other.tag == "Player");
{
other.transform.position = new Vector2(-50,1);
}
}
function Update () {
}
Any help with my poopy code would be much appreciated.
Thanks!
James
P.S. Where are you guys learning code?
Answer by shieldgenerator7 · Dec 26, 2015 at 11:17 AM
Unity uses a different set of methods for 2D
You're overriding OnTriggerEnter, but you should be overriding OnTriggerEnter2D, with a Collider2D parameter
Answer by DajBuzi · Oct 30, 2014 at 10:34 PM
Hello,
Your scripts looks good and that's the way you suppose to "teleport" or switch possition of desired object. I would do this in "event-like" form like this:
void OnTriggerEnter2D(Collider col)
{
if(col.tag.Equals("Player") && onTriggerEnter != null)
onTriggerEnter(this.triggerId);
}
This example is written in C# and since ou're using JS it's not going to work but as i've mentioned before. Your code looks like it should.
If you want to learn code you have to make many mistakes and try to not give up :)
Regards, M.Rogalski
Oh boy. Thanks. I'm going to fiddle around with C# to see if I can get this to work.