- Home /
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?
Just to clarify your question...is the problem that you only want to switch one row per click of the up/down button?
-Will
Yes. If the user were to press up once, and only switch rows once that would solve the problem.
In that case you should just switch the GetAxys for Get$$anonymous$$eyUp(keycode) or Get$$anonymous$$eyDown(keycode).
If you are not ai$$anonymous$$g for console platforms this should work fine.
Your initial question prompted me to search more in that direction.
I'm temporarily using "Get$$anonymous$$eyDown" and it's working on PC. I will investigate further in regards to joypad based controls if it comes to that.
Answer by DennisDuty · Jul 03, 2015 at 07:29 AM
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!
Answer by Jessespike · Jun 30, 2015 at 05:21 PM
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;
}
For this purpse the function rowshift2() would stop working properly because the input would always return false.
You can also pass a parameter to the function like this:
float delay = 1f;
void Awake()
{
bool parameterToPass = true;
StartCoroutine(FunctionToBeCalled(parameterToPass));
}
IEnumerator FunctionToBeCalled(bool param)
{
yield return new WaitForSeconds(delay);
// do your magic here
}
It worked, but after further playtesting the controls didn't work out so well. I played around even further tweaking and refining, but it just felt inprecise and frustrating to the player.
I ended up solving it by using an "isSwitchingRows" bool, but no hard-coded delay. It was set to return false only when y axis was returned 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();
}
}
Answer by mikavm · Jun 30, 2015 at 05:59 PM
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++;
}
Answer by fuego_see_money · Jun 30, 2015 at 05:59 PM
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++;
}
}
}
Your answer
Follow this Question
Related Questions
Teleporting projectiles, and maintaining speed 3 Answers
How to make enemy tp to you after 45 sec.? 1 Answer
How do I make something happen at collider 1 Answer
Issue with my Teleport Script 1 Answer
Script Delay with Teleport 0 Answers