- Home /
Trying to teleport back and forth, but it randomly doesn't teleport
This is by for the most confusing bug I've seen. The way the code is supposed to work is that you press a key, it'll spawn a physics sphere to see if anything is in the way. If there isn't, it'll teleport you while playing an animation. It works 80% of the time. I don't know how, I don't know why. If someone knows what's going on, please tell me, I'm very confused.
Here's the movement script(moves player):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
[Header("Player")]
public CharacterController player;
public float speed;
public float airSpeed;
public float jumpHeight = 1f;
[Header("Ground & Gravity")]
public Transform groundCheck;
public LayerMask ground;
public Vector3 velocity;
public float gravityValue = -9.81f;
public ChangeLevels lvl;
private void Start()
{
}
void Update()
{
if (velocity.y >= 5f) { velocity.y = 5f; }
//Inputs
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
bool jump = Input.GetButtonDown("Jump");
bool isGrounded = Physics.CheckSphere(groundCheck.position, 0.1f, ground);
Vector3 move = transform.right * x + transform.forward * z;
if (isGrounded)
{
player.Move(move * speed * Time.deltaTime);
if (velocity.y < 0) { velocity.y = -2f; }
if (jump) { velocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue); }
}
else { player.Move(move * airSpeed * Time.deltaTime); }
velocity.y += -9.84f * Time.deltaTime;
player.Move(velocity * Time.deltaTime);
}
public void moveHere()
{
this.transform.position = lvl.pos;
lvl.offset *= -1f;
}
}
Here's the teleporting script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class ChangeLevels : MonoBehaviour
{
public GameObject player;
public Vector3 pos;
public float offset;
public TMP_Text text;
public Animator anim;
public bool canSw;
public LayerMask ground;
public bool isTouching;
Movement move;
void Start()
{
move = GetComponent<Movement>();
}
void Update()
{
pos = new Vector3(player.transform.position.x + offset, player.transform.position.y, player.transform.position.z);
isTouching = Physics.CheckSphere(pos, .4f, ground);
if (Input.GetKeyDown(KeyCode.R)) { if (canSw) StartCoroutine(switchLevel()); else { inWay("[Wait For Cooldown]"); } }
}
IEnumerator switchLevel()
{
anim.SetTrigger("Switch");
yield return new WaitForSeconds(0.16f);
if (!isTouching)
{
canSw = false;
move.moveHere();
yield return new WaitForSeconds(1f);
canSw = true;
}
else { inWay("[Object In Way]"); }
}
public void inWay(string newText) { text.text = newText; anim.SetTrigger("Warning"); }
}
Note: It is actually random of when it wants to work or not. I've set up debug.logs all over the place one time. It does hit all the debugs that it needs to, it just won't teleport the player.
the way you placed the
canSw = false;
makes it possible for multiple instances of the Coroutine to be running at the same time. Could that be it?