Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Orggrim · Aug 03, 2019 at 10:07 PM · teleportteleporting

Issue with Teleporting Object

Hello there!

I am looking to teleport my player cube object (solid red cube) via using the teleport blocks( see-through yellow and purple blocks).

I am attempting to do this by checking for if the player is at the same localPosition of either teleport block AND if the bool variable "teleported" is false.

The issue I am running into is that while it does move the player cube fine from TeleporterA to TeleporterB, it immediately moves back from B to A like the if statement condition was met, even though it was not. It is this particular block of code that runs when it shouldnt:

 else if (InTeleporterB() && teleported == false)
         {
             print("Conditions met to move to Teleporter A");
             teleported = true;
             MoveToTeleporterA();
         }

Here is the full code I am currently using for teleporting. This script is attached to both teleporter blocks:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TeleportBehavior : MonoBehaviour
 {
 
     GameObject cube;
     public GameObject t1;
     public GameObject t2;
 
     Vector3 t1Pos;
     Vector3 t2Pos;
     Vector3 cubeStartPos;
 
     bool teleported;
 
     // Start is called before the first frame update
     void Start()
     {
         cube = GameObject.FindGameObjectWithTag("Player");
         t1Pos = t1.transform.localPosition;
         t2Pos = t2.transform.localPosition;
         teleported = false;
     }
 
     void Update()
     {
 
         if(InTeleporterA() && teleported == false)
         {
             print("Conditions met to move to Teleporter B");
             teleported = true;
             MoveToTeleporterB();
         }
         else if (InTeleporterB() && teleported == false)
         {
             print("Conditions met to move to Teleporter A");
             teleported = true;
             MoveToTeleporterA();
         }
         
 
         print("In Teleporter A: " + InTeleporterA());
 
 
         print("In Teleporter B: " + InTeleporterB() + 
             " and Teleported: " + teleported);
         
     }
 
     bool InTeleporterA()
     {
         return (cube.transform.localPosition == t1Pos);
     }
 
     bool InTeleporterB()
     {
         return (cube.transform.localPosition == t2Pos);
     }
     
     void MoveToTeleporterB()
     {
         teleported = true;
         print("Moving to Teleporter B...");
         cube.transform.localPosition = t2Pos;
     }
     void MoveToTeleporterA()
     {
         teleported = true;
         cube.transform.localPosition = t1Pos;
         print("Moving to Teleporter A...");
     }
 }
 

Here are two images showing visually what im working with, including what both public GameObjects in the script (t1 and t2) have hooked up to them. It also includes console log showing it going through with the if statement conditions not being met: alt text

alt text

Please let me know if there is any additional information I can supply. Thank you very much for your help! <3

unityview1.jpg (180.8 kB)
unityview2.jpg (192.3 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by niiicolai · Aug 04, 2019 at 02:31 AM

Hi @Orggrim. It seems odd that your script is not working since you aren't setting ´teleported´ to ´false´ anywhere else than the Start() function. But you do have the script attached to two different game objects (TeleportA and TeleportB), in which case it should run two times because they don't share the value of ´teleported´


So my guess is that the first script runs the function and sets ´teleported´ to true and then the script attached to the other object does the same, but in this case, is the player at teleport B and therefore move back to A. Try to turn off one of the scripts to confirm this is the case.


Alternative: If you are building a game where you want the player to walk into the cube and then teleport to the other cube you could do something like this instead:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TeleportTrigger : MonoBehaviour {
 
     [SerializeField] private TeleportTrigger otherTrigger;
     [SerializeField] private Transform spawnPoint;
 
     private string playerTag = "Player"; 
 
     /// <summary>
     /// OnTriggerEnter is called when the Collider other enters the trigger.
     /// </summary>
     /// <param name="other">The other Collider involved in this collision.</param>
     void OnTriggerEnter(Collider other) {
         if (other.CompareTag(playerTag)) {
             other.transform.position = otherTrigger.spawnPoint.position;
         }
     }
 }


How to use "TeleportTrigger.cs":

  1. Add the script to your teleport cube.

  2. Ensure the cube only has a collider where "trigger" is enabled.

  3. Create a cube inside it, and place it in front of your teleport cube. This cube will work as a spawn point for other teleporters.

  4. Just disable or remove the mesh renderer from the "spawn point cube" after placing it.

  5. Add the spawn point cube to the TeleportTrigger component on the cube and duplicate it.

  6. Move the duplicated cube to its wanted position and add the original cube as "other trigger".

  7. Add the duplicated cube as "other trigger" to the original cube.

  8. Ensure to add a tag named "Player" to the player GameObject and that it have a rigidbody, since the rigidbody is required on either the player or teleportTrigger for the OnTriggerEnter(Collider) function to be runned. I recommend you put it on the player, so you only need 1.

That should be it. Check out the docs on the OnTriggerEnter function here https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html

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

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

108 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 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 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 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 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 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

FPSContoller Teleportation Script is blocking somehow 2 Answers

Portal between scenes? 3 Answers

Line of code not working 2 Answers

Teleporting Objects,Teleporting Object 0 Answers

Teleportation 1 Answer


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