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