Delaying gameobject.transform = var.transform teleport

My 2d game has 4 rows your character can jump between.

I set up empty gameObjects in my scene to reference for the dynamic row locations. These are defined as row1, row2, row3, row4.

I set up a variable called currentrow to store what row you’re in at the moment, and a series of if statements which will place you in the corresponding row.

This function is running in Update

	void rowshift2()
	{


		//These first two if's allow cycling between 1 and 4
		if (currentrow > 4)
		{
			currentrow = 1;
		}
		if (currentrow < 1)
		{
			currentrow = 4;
		}



		//Handles the teleporting
		if (currentrow == 1)
		{
			gameObject.transform.position = row1.transform.position;
		}
		if (currentrow == 2)
		{
			gameObject.transform.position = row2.transform.position;
		}
		if (currentrow == 3)
		{
			gameObject.transform.position = row3.transform.position;
		}
		if (currentrow == 4)
		{
			gameObject.transform.position = row4.transform.position;
		}


		//Uses Y axis to change currentrow value.
		if (Input.GetAxis("Vertical") >0)
		{
			currentrow --;
		}
		if (Input.GetAxis("Vertical") <0)
		{
			currentrow++;
		}

	}

So now, my main problem is that you slightly tap the up/down, and you’re rapidly changing between locations… fly across multiple rows at lightning speed.

My internet searches indicate that I need a co-routine to ‘slow down’ code, but I tried to impliment and I just can’t seem to figure it out.

How do I slow down this teleport action?

bool isSwitchingRows = false;

	void Update () {
		//if (ConditionToSwitchRowIsMet) {         // pseudo, I don't know what the condition is
			StartCoroutine(DelayedRowShift());
		//}
	}

	IEnumerator DelayedRowShift()
	{
		if (!isSwitchingRows) {
			isSwitchingRows = true;

			yield return new WaitForSeconds(1f);
			rowshift2();
			isSwitchingRows = false;
		}

		yield return null;
	}

Hello,
why don’t you try using Input.GetKeyDown?

Your code would look something like this:

if (Input.GetKeyDown("DownArrow")){
     currentrow --;
}

if (Input.GetKeyDown("UpArrow")){
     currentrow++;
}

Perfect! Here is what i would do.

Store the previous frame input axis in your code, check to make sure it is not the same as last frame. I’m at work so i didnt have time to test out the code, let me know if it doesnt work and I’ll retry.

public class WhateverYourClassNameIs : MonoBehaviour
{
	float prevYInput = 0f;
	float curYInput = 0f;
	
	void Start()
	{
		//whatever your start code is
	}
    void Update()
	{
		curYInput = Input.GetAxis("Vertical");
		rowshift2();
		prevYInput = curYInput;
	}
	
	void rowshift2()
	{
		//all your other code
		//
		//
		//
		//Uses Y axis to change currentrow value.
         if (curYInput > 0 && prevYInput == 0)
         {
             currentrow --;
         }
         else if (curYInput < 0 && prevYInput == 0)
         {
             currentrow++;
         }
	}
}

Solved it everybody!

I initially used GetKeyDown as a temporary solution so I could just move on with the game… but it had limited support. I ended up creating a value “isSwitchingRows” which was true until Y axis input was reset to 0.

private bool isSwitchingRows = false;

	void Update ()
	{
		shiftingRowsCheck();
	}

	void shiftingRowsCheck()
	{
		if(Input.GetAxisRaw("Vertical") == 0)
		{
			isSwitchingRows = false;
		}
		if (Input.GetAxisRaw("Vertical")!=0 && isSwitchingRows == false)
		{
			isSwitchingRows = true;
			rowshift2();
		}
	}

The end result is something that feels really good to play. Tapping down makes the player move down. Tapping up makes the player move up. Good stuff.

Thanks for the help, I couldn’t have done it without your guidance!