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 RoboticSarcasm · Apr 14, 2015 at 11:18 AM · c#teleport

Teleporter teleports one way not back [C#]

Hello everybody,

after the writing of this code, that is supposed to be a two-way teleporter (you can teleport to another teleporter and SHOULD teleport back), ive run into a problem. It only teleports one way. The teleporter teleports to the other one, but then once you activate it again it teleports to the same teleporter, so you just teleport to the teleporter you are at. Does anybody know how to fix this?

EDITED FOR NEW CODE:

 using UnityEngine;

using System.Collections;

public class PlayerTeleporter : MonoBehaviour {

 public Transform playerTransform;
 public Transform curTeleportTransform;
 public Transform targetTeleportTransform;
 public float curDistance;
 public float useRange;

 


 

 
 void FixedUpdate () {
     playerTransform = GameObject.Find("player").transform;
     curTeleportTransform = transform;
     curDistance = Vector3.Distance(curTeleportTransform.position, playerTransform.position);


     if (Input.GetButtonDown("Use teleporter") && curDistance < useRange) 
     {

         playerTransform.position = targetTeleportTransform.position;
         Debug.Log(targetTeleportTransform.position);
     
     }

     

 }

}

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Kelstek · Apr 14, 2015 at 03:49 PM

Are you positive you assigned the TargetTeleportTransform on the second teleported GameObject?

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 RoboticSarcasm · Apr 14, 2015 at 03:55 PM 0
Share

yes. i am positive.

avatar image
0

Answer by Kelstek · Apr 14, 2015 at 05:27 PM

By the way, you're setting the playerTransform once during Awake - I suspect you want that to be in Update(), as it will only be set once (where as te player's transform is constantly updating).

Likely, the playerTransform distance to the second teleporter's transform is greater than this starting distance, preventing it from triggering via your conditional statement.

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 Kelstek · Apr 14, 2015 at 05:27 PM

Sorry for the scattered posting, but I think you'd do better to just attach a SphereCollider to the object, and use the OnCollide() method to detect when an object want to teleport, and to get their current transform. You can then use tags or the object's name to discriminate and only allow certain objects through (players only, players and enemies, players and projectiles, what have you).

Also, that'd be less costly performance wise than my above suggestion of putting a FindObject() inside an Update().

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 KdRWaylander · Apr 14, 2015 at 12:30 PM

Hi,

You need to initialise your targetTeleportTransform, right now it's not the case. And you don't need this line: curTeleportTransform = transform; since you already said it in your Awake() :)

What you could do is in your Awake add something like that (C#):

 if (tag == "TP1") {
     targetTeleportTransform = GameObject.FindGameObjectWithTag("TP2");
 }else if ("TP2") {
     targetTeleportTransform = GameObject.FindGameObjectWithTag("TP1");
 }

Or with a switch case, which would be nicer

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 RoboticSarcasm · Apr 14, 2015 at 12:35 PM 0
Share

target is assigned in the editor

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Renderer on object disabled after level reload 1 Answer

How to teleport the player on which direction its facing? 1 Answer

How do I make an object teleport 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