Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jamesloymartin · Oct 30, 2014 at 04:07 AM · 2dplayerteleport

Teleport 2D Player to another room

alt text

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?

teleporthelp.jpg (272.5 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jamesloymartin · Oct 30, 2014 at 08:53 PM 0
Share

Oh, and my player is tagged as "Player"

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jamesloymartin · Oct 31, 2014 at 03:14 AM 0
Share

Oh boy. Thanks. I'm going to fiddle around with C# to see if I can get this to work.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I make a collision sensitive teleporter for a 2D game #c 1 Answer

Issues with player prefabs when I try to teleport it 1 Answer

the player is flying off the ramp (collider) 1 Answer

Coth physics in 2d 1 Answer

I need to teleport a FPS chara controller 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges