- Home /
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);
}
}
}
Answer by Kelstek · Apr 14, 2015 at 03:49 PM
Are you positive you assigned the TargetTeleportTransform on the second teleported GameObject?
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.
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().
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
Your answer
Follow this Question
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