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
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!
@Vega4Life thanks! is there a way i need to link it to the game? Doesn't seem to be working! thanks again!
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.
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!
Let me see if I can put things where they should go. Give me a few. :)
For some reason I can't add code or attach .txt. It might be the site. :/
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
Follow this Question
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