Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 joshrolloos · Dec 12, 2018 at 10:14 PM · transformtransform.positionloopkeycodegetkeydown

Change button after 'x' amount of time

So sorry but that still doesnt work for some reason, I'll show you all of the code for the gameobject! thanks so much for the help!!

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class TetrisObject : MonoBehaviour {

 float lastFall = 0f;


 // Use this for initialization
 void Start()
 {

 }

 // Update is called once per frame
 void Update()
 {


     if (Input.GetKeyDown(KeyCode.LeftArrow))
     {
         transform.position += new Vector3(-1, 0, 0);

         if (IsValidGridPosition())
         {
             UpdateMatrixGrid();
         }
         else
         {
             transform.position += new Vector3(1, 0, 0);
         }

     }
     else if (Input.GetKeyDown(KeyCode.RightArrow))
     {
         transform.position += new Vector3(1, 0, 0);

         if (IsValidGridPosition())
         {
             UpdateMatrixGrid();
         }
         else
         {
             transform.position += new Vector3(-1, 0, 0);
         }
     }
     else if (Input.GetKeyDown(KeyCode.UpArrow))
     {
         transform.Rotate(new Vector3(0, 0, -90));

         if (IsValidGridPosition())
         {
             UpdateMatrixGrid();
         }
         else
         {
             transform.Rotate(new Vector3(0, 0, 90));
         }
     }
     else if (Input.GetKeyDown(KeyCode.DownArrow) || Time.time - lastFall >= 1)
     {
         transform.position += new Vector3(0, -1, 0);

         if (IsValidGridPosition())
         {
             UpdateMatrixGrid();
         }
         else
         {
             transform.position += new Vector3(0, 1, 0);

             MatrixGrid.DeleteWholeRows();

             FindObjectOfType<Spawner>().SpawnRandom();

             enabled = false;
         }
         lastFall = Time.time;

     }

 }

 bool IsValidGridPosition()
 {
     foreach (Transform child in transform)
     {
         Vector2 v = MatrixGrid.RoundVector(child.position);

         if (!MatrixGrid.IsInsideBorder(v))
             return false;

         if (MatrixGrid.grid[(int)v.x, (int)v.y] != null && MatrixGrid.grid[(int)v.x, (int)v.y].parent != transform)
             return false;

     }
     return true;
 }

 void UpdateMatrixGrid()
 {
     for (int y = 0; y < MatrixGrid.column; ++y)
     {
         for (int x = 0; x < MatrixGrid.row; ++x)
         {
             if (MatrixGrid.grid[x, y] != null)
             {
                 if (MatrixGrid.grid[x, y].parent == transform)
                 {
                     MatrixGrid.grid[x, y] = null;
                 }
             }
         }
     }

     foreach (Transform child in transform)
     {
         Vector2 v = MatrixGrid.RoundVector(child.position);
         MatrixGrid.grid[(int)v.x, (int)v.y] = child;
     }
 }

} //tetrisobject

@Vega4Life

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Vega4Life · Dec 12, 2018 at 11:11 PM

Here is a simple example of using a delegate.


 using UnityEngine;
 
 
 public class InputChanger : MonoBehaviour
 {
     float timer = 60f;
     float elapsedTime = 0f;
 
     delegate void ThingToDo();
     ThingToDo leftInput;
     ThingToDo rightInput;
 
 
     void Awake()
     {
         leftInput = MoveLeft;
         rightInput = MoveRight;
     }
 
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.LeftArrow))
         {
             Debug.Log("Left Button Pressed");
             leftInput();
         }
 
         if (Input.GetKeyDown(KeyCode.RightArrow))
         {
             Debug.Log("Right Button Pressed");
             rightInput();
         }
 
 
         elapsedTime += Time.deltaTime;
         if (elapsedTime >= timer)
         {
             elapsedTime = 0f;
 
             ThingToDo container = leftInput;
             leftInput = rightInput;
             rightInput = container;
 
             Debug.Log("Switch");
         }
     }
 
 
     void MoveLeft()
     {
         Debug.Log("Moving Left");
     }
 
 
     void MoveRight()
     {
         Debug.Log("Moving Right");
     }
 }


The idea is the keys aren't changing, just the content it goes to does. Hitting left arrow, will say "Left Button Pressed" and "Moving Left". After it switches it will say "Left Button Pressed" and "Moving Right"


Hope it helps, good luck!


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 joshrolloos · Dec 13, 2018 at 12:22 PM 0
Share

@Vega4Life thanks! is there a way i need to link it to the game? Doesn't seem to be working! thanks again!

avatar image Vega4Life joshrolloos · Dec 13, 2018 at 01:05 PM 0
Share

If you mean just testing my script, just add it to a gameObject in the scene. Hit play and start tapping left arrow or right arrow - and you should see some logs output after each tap. 60 seconds later you should see a switch log.


It's probably best to change the timer in the script to 10 seconds, so the switching happens faster.


avatar image
0

Answer by joshrolloos · Dec 13, 2018 at 01:37 PM

The game is tetris, i currently have this in the update section of my GameObject

void Update() {

     if (Input.GetKeyDown(KeyCode.LeftArrow))
     {
         transform.position += new Vector3(-1, 0, 0);

         if (IsValidGridPosition())
         {
             UpdateMatrixGrid();
         }
         else
         {
             transform.position += new Vector3(1, 0, 0);
         }

     }
     else if (Input.GetKeyDown(KeyCode.RightArrow))
     {
         transform.position += new Vector3(1, 0, 0);

         if (IsValidGridPosition())
         {
             UpdateMatrixGrid();
         }
         else
         {
             transform.position += new Vector3(-1, 0, 0);
         }
     }
     else if (Input.GetKeyDown(KeyCode.UpArrow))
     {
         transform.Rotate(new Vector3(0, 0, -90));

         if (IsValidGridPosition())
         {
             UpdateMatrixGrid();
         }
         else
         {
             transform.Rotate(new Vector3(0, 0, 90));
         }
     }
     else if (Input.GetKeyDown(KeyCode.DownArrow) || Time.time - lastFall >= 1)
     {
         transform.position += new Vector3(0, -1, 0);

         if (IsValidGridPosition())
         {
             UpdateMatrixGrid();
         }
         else
         {
             transform.position += new Vector3(0, 1, 0);

             MatrixGrid.DeleteWholeRows();

             FindObjectOfType<Spawner>().SpawnRandom();

             enabled = false;
         }
         lastFall = Time.time;

     }

 }

but do not know where to put your code!

Comment
Add comment · Show 3 · 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 Vega4Life · Dec 13, 2018 at 02:13 PM 0
Share

Let me see if I can put things where they should go. Give me a few. :)

avatar image Vega4Life Vega4Life · Dec 14, 2018 at 09:40 AM 0
Share

For some reason I can't add code or attach .txt. It might be the site. :/

avatar image Vega4Life · Dec 14, 2018 at 09:45 AM 0
Share

In this last bunch of code you posted there was an error.


 FindObjectOfType().SpawnRandom();


You need to specify the type to find, like this:


 FindObjectOfType<Foo>().SpawnRandom();


Adding a link to the script since I can't add code or attachments:


http://www.mediafire.com/file/5pn62a180o2xtgc/Script.txt/file


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

189 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 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

How Can I Teleport a Parent Object to its Child? 0 Answers

Calculate a point in space based on a triangle 0 Answers

Instantiating gameobject creates it at second impact point 0 Answers

transform.position is not updating the object's position 1 Answer

Need 2D help - Want to have object move towards point when it touches a trigger. 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