- Home /
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:
Please let me know if there is any additional information I can supply. Thank you very much for your help! <3
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":
Add the script to your teleport cube.
Ensure the cube only has a collider where "trigger" is enabled.
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.
Just disable or remove the mesh renderer from the "spawn point cube" after placing it.
Add the spawn point cube to the TeleportTrigger component on the cube and duplicate it.
Move the duplicated cube to its wanted position and add the original cube as "other trigger".
Add the duplicated cube as "other trigger" to the original cube.
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
Your answer
Follow this Question
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