Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by DennisDuty · Jun 30, 2015 at 04:32 PM · teleportslow down

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?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fuego_see_money · Jun 30, 2015 at 05:10 PM 0
Share

Just to clarify your question...is the problem that you only want to switch one row per click of the up/down button?

-Will

avatar image DennisDuty · Jun 30, 2015 at 05:14 PM 0
Share

Yes. If the user were to press up once, and only switch rows once that would solve the problem.

avatar image brunopava · Jun 30, 2015 at 05:44 PM 0
Share

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.

avatar image DennisDuty · Jun 30, 2015 at 06:00 PM 0
Share

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.

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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;
     }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image brunopava · Jun 30, 2015 at 05:38 PM 1
Share

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
 }

avatar image DennisDuty · Jul 02, 2015 at 05:11 PM 0
Share

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();
         }
     }
avatar image
0

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++;
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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++;
          }
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Spawning System 1 Answer

Touch teleport script isnt working 2 Answers

Mirror a Vector3 1 Answer

Velocity not resetting to zero? 1 Answer

Camera Following character that teleported 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges