Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 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 /
  • Help Room /
avatar image
0
Question by jintimus · Mar 25, 2021 at 08:03 AM · keycodekeyboard input

Swapping KeyCode that will reflect on screen

I have 26 letters that are mapped by using KeyCode on screen, and when I press 'B' on my keyboard, then the letter B will light up on my screen.

This is what I want to do:

  1. I want to swap physical key location, so "A" and "D" will switch places on screen.

  2. Then, when I press the actual keyboard "D", I want the registered input to be A and A will highlight.

  3. Finally, gradually increase numbers of keys that are being swapped.

So far, I got 1 and 3 with this code.


public class Shuffle : MonoBehaviour

 public float letterMoveTime = 1f;

 public List<GameObject> keys;
 IEnumerator shuffleCoroutine;

 Typer progressCounter;

 private void Awake()
 {
     keys = new List<GameObject>();
     keys.AddRange(GameObject.FindGameObjectsWithTag("EditorOnly"));
     // each gameobject keys have this tag.

     shuffleCoroutine = null;
     progressCounter = GameObject.FindGameObjectWithTag("Finish").GetComponent<Typer>();
 }

 private void Update()
 {
     progressCounter = GameObject.FindGameObjectWithTag("Finish").GetComponent<Typer>();
 }

 public void StartShuffle() // call this on button click
 {
     if (shuffleCoroutine != null) return;

     shuffleCoroutine = DoShuffle();
     StartCoroutine(shuffleCoroutine);
 }

 IEnumerator DoShuffle()
 {
     List<Vector2> startPos = new List<Vector2>();
     List<Vector2> endPos = new List<Vector2>();

     //List<KeyCode> startKeyCode = new List<KeyCode>(); this does not work.
     //List<KeyCode> endKeyCode = new List<KeyCode>(); this does not work.

     foreach (GameObject letter in keys)
     {
         startPos.Add(letter.transform.position);
         endPos.Add(letter.transform.position);

         //startKeyCode.Add(letter.GetComponent<KeyCode>()); this does not work.
         //endKeyCode.Add(letter.GetComponent<KeyCode>()); this does not work.
     }

     // shuffle endPos
     for (int i = 0; i < Mathf.Ceil(progressCounter.progressCounter/5); i++)
     {
         Vector2 temp = endPos[i];
         int swapIndex = Random.Range(i, endPos.Count);
         endPos[i] = endPos[swapIndex];
         endPos[swapIndex] = temp;

         //KeyCode tempKey = endKeyCode[i];
         //endKeyCode[i] = endKeyCode[swapIndex];
         //endKeyCode[swapIndex] = tempKey;
     }

     float elapsedTime = 0f;

     while (elapsedTime < letterMoveTime)
     {
         // wait for next frame
         yield return null;

         // move each letter
         elapsedTime = Mathf.Min(letterMoveTime, elapsedTime + Time.deltaTime);
         float t = elapsedTime / letterMoveTime;

         for (int i = 0; i < startPos.Count; i++)
         {
             keys[i].transform.position = Vector2.Lerp(startPos[i], endPos[i], t);
             //startKeyCode[i] = endKeyCode[i];
         }
     }

     // allow shuffling to occur again
     shuffleCoroutine = null;
 }


Does anyone have a good way to do this?

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

155 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A keybinding system in unity 1 Answer

how to activate a power up by pressing down a key? 0 Answers

Identify custom user input 0 Answers

Mysterious Extra KeyUp Event (5.3.3f1) 2 Answers

input.getkey keycode scrollwheel - Is this even possible? 0 Answers


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