Here are the simple code bits:
void Update() {
if(isTeleporting) {
teleport(new Vector3(transform.position.x, transform.position.y, transform.position.z - 1));
}
if (Input.GetKey(KeyCode.F)) {
teleport(new Vector3(transform.position.x, transform.position.y, transform.position.z - 1));
}
}
Every frame this checks if an ‘isTeleporting’ variable is true, if it is, it will call the teleport method. It also checks if I hit the ‘F’ key, if I do, it does the exact same code.
Teleport method:
public void teleport(Vector3 pos) {
isTeleporting = false;
Debug.Log("Teleport!");
transform.position = new Vector3(pos.x, pos.y, pos.z);
}
Pretty simple, it sets the ‘isTeleporting’ back to false so it doesn’t teleport every frame, prints a debug, and finally actually moves the object.
So, how does ‘isTeleporting’ become true? If I come in contact with a trigger.
public GameObject[] startLocs;
public bool isTeleporting = false;
void OnTriggerEnter(Collider col) {
for (int i = 0; i < startLocs.Length; i++) {
if (col.gameObject == startLocs*) {*
Debug.Log("Player entered: " + col.gameObject.tag + " " + i);
isTeleporting = true;
}
}
}
If the player triggers a startLocs trigger, then it will proceed to set isTeleporting to true.
Basically, all this is me trying to set up a door system. I’ve tried a million different ways but for some reason the player is not moving. The debug positions say the positions are changing, but he is in the same place.
The problem with this code is, when I come in contact with a door with a trigger, it calls the teleport method (because ‘isTeleport’ is set to true) and prints the debug “Teleport!” statement, but player does not move. Yet, when I hit the ‘F’ key, it again prints the debug “Teleport!” statement, except this time, the player does move. Why does this happen? And how can I fix this?
Any help is appreciated.