Teleportation and Coroutine

Hi Everyone

I really need assistance on this. First, here’s the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerTeleport : MonoBehaviour
{
    public bool isMouseUp;               //This is just a check for response from the Editor
    public bool isMouseDown;          //Likewise

    Transform r_Position;                   //A child of main character 
    SpriteRenderer r_Position_Sprite;    //SpriteRenderer of child to enable only on a key press to help players see the destination

    Transform l_Position;                //The second child gameObject that 'll be positioned at the character's left
    SpriteRenderer l_Position_Sprite;    

    // Start is called before the first frame update
    void Awake()
    {
        r_Position              = this.transform.GetChild(0).GetComponent<Transform>();
        r_Position_Sprite  = this.transform.GetChild(0).GetComponent<SpriteRenderer>();

        l_Position               = this.transform.GetChild(1).GetComponent<Transform>();
        l_Position_Sprite   = this.transform.GetChild(1).GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void FixedUpdate()
    {
        StartCoroutine(TeleportCheck());
    }

    IEnumerator TeleportCheck()
    {
        l_Position_Sprite.enabled     = false;
        r_Position_Sprite.enabled     = false;


        if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
        {

            r_Position_Sprite.enabled = true;      //To see the teleportation destination on each sides
            l_Position_Sprite.enabled = true;

            if (Input.GetAxis("Mouse ScrollWheel") > 0.0f)
            {
                TeleportToFront();
                isMouseUp   = true;
                Debug.Log("Mouse up");
                yield return new WaitForSeconds(3f);
            }
            else
                isMouseUp   = false;


            if (Input.GetAxisRaw("Mouse ScrollWheel") < 0.0f)
            {
                TeleportToBack();
                isMouseDown = true;
                Debug.Log("Mouse down");
                yield return new WaitForSeconds(3f);
            }
            else
                isMouseDown = false;
        }
    }

    void TeleportToFront()
    {
        transform.position = r_Position.transform.position;
        //Forward teleportation animation
    }

    void TeleportToBack()
    {
        transform.position = l_Position.transform.position;
        //Back teleportation animation
    }
}

I’m trying to get the player to teleport only once after a teleportation’s registered (Controlled by coroutine) either to his front or to the back. If I’m on a reasonable path, can you tell what’s wrong with this script, else can you tell of a more tidier way to go about this if there’s any

The debug logs are working fine, the status boolean flags do not work consistently and the coroutines are entirely another problem, they’re not working at all.

Sorry if the description’s too long

Okay, I think I understood what you’re aiming for, and I might have actually managed to re-write things without breaking them. Let me know how this works out for you, I commented most of my changes with comment blocks though some stuff I moved around because, well it made more sense to me that way. I also tend to make things variable rather than hard coded values so I don’t need to re-compile every time I want to change a number, so you’ll want to change some stuff via the inspector instead of VS.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerTeleport : MonoBehaviour
{
    public bool isMouseUp;               //This is just a check for response from the Editor
    public bool isMouseDown;          //Likewise

    Transform r_Position;                   //A child of main character 
    SpriteRenderer r_Position_Sprite;    //SpriteRenderer of child to enable only on a key press to help players see the destination

    Transform l_Position;                //The second child gameObject that 'll be positioned at the character's left
    SpriteRenderer l_Position_Sprite;

    public bool canTeleport;        /*Holds the players teleport state*/
    public int maxTeleports;        /*How many times can the player TP*/
    public int teleports;           /*Holds number of teleports the player has performed*/
    public float cooldownTime;      /*How long a cooldown should take*/

    //K-Anator was here ;)
    // Start is called before the first frame update
    void Awake()
    {
        r_Position = this.transform.GetChild(0).GetComponent<Transform>();
        r_Position_Sprite = this.transform.GetChild(0).GetComponent<SpriteRenderer>();

        l_Position = this.transform.GetChild(1).GetComponent<Transform>();
        l_Position_Sprite = this.transform.GetChild(1).GetComponent<SpriteRenderer>();


        l_Position_Sprite.enabled = false; /* moved these to awake so they default to false*/
        r_Position_Sprite.enabled = false;

        canTeleport = true; /*Start with the player allowed to TP*/
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void FixedUpdate()
    {
        if (canTeleport && teleports < maxTeleports) /*If the player is allowed to TP, and the number of previous TPs is less than the max allowed TPs do the things*/

            if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
            {

                r_Position_Sprite.enabled = true;      //To see the teleportation destination on each sides
                l_Position_Sprite.enabled = true;

                if (Input.GetAxis("Mouse ScrollWheel") > 0.0f)
                {
                    TeleportToFront();
                    canTeleport = false; /*Teleport was called, so disallow TP. Do a cooldown in a co-routine called from TeleportToFront()*/
                    isMouseUp = true;
                    Debug.Log("Mouse up");
                }
                else
                    isMouseUp = false;

                if (Input.GetAxisRaw("Mouse ScrollWheel") < 0.0f)
                {
                    TeleportToBack();
                    canTeleport = false; /*Same as above*/
                    isMouseDown = true;
                    Debug.Log("Mouse down");
                }
                else
                    isMouseDown = false;
            }


        void TeleportToFront()
        {
            transform.position = r_Position.transform.position;
            teleports++; /*increase TPs by one*/
            StartCoroutine(TeleportCooldown());/*Cool off that TP*/
            //Forward teleportation animation
        }

        void TeleportToBack()
        {
            transform.position = l_Position.transform.position;
            teleports++;
            StartCoroutine(TeleportCooldown());
            //Back teleportation animation
        }

        IEnumerator TeleportCooldown()
        {
            yield return new WaitForSeconds(cooldownTime);
            canTeleport = true;

        }
    }
}