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 /
avatar image
0
Question by Xeonproc · May 18, 2017 at 11:05 AM · scripting problem

Tetris Game Blocks falling too fast

I've created a simple Tetris game that is working for the most part but my blocks are falling too fast. I think it has something to do with using Time.time but I'm a beginner and am unsure how to proceed. Can you spot the error in my scripts below?

Spawner.cs using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Spawner : MonoBehaviour {
 
     public GameObject[] groups;
 
     public void spawnNext(){
         int i = Random.Range (0, groups.Length);
         //Spawn group at current position
         Instantiate(groups[i], transform.position, Quaternion.identity);
     }
 
     void Start() {
         spawnNext ();
     }
 }

Grid.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Grid : MonoBehaviour {
 
     public static int w = 10;
     public static int h = 20;
     public static Transform[,] grid = new Transform [w,h];
 
     public static Vector2 roundVec2(Vector2 v) {
         return new Vector2 (Mathf.Round (v.x), Mathf.Round (v.y));
     }
 
     public static bool insideBorder(Vector2 pos) {
         return ((int)pos.x >= 0 && (int)pos.x < w && (int)pos.y >= 0);
     }
 
     public static void deleteRow(int y) {
         for (int x = 0; x < w; ++x) {
             Destroy (grid [x, y].gameObject);
             grid [x, y] = null;
         }
     }
 
     public static void decreaseRow(int y) {
         for (int x = 0; x < w; ++x) {
             if (grid [x, y] != null) {
                 //move one towards bottom
                 grid[x,y-1] = grid[x,y];
                 grid [x, y] = null;
                 //update block position
                 grid[x, y-1].position += new Vector3(0,-1,0);
             }
         }
     }
 
     public static bool isRowFull(int y) {
         for (int x = 0; x < w; ++x)
             if (grid[x, y] == null)
                 return false;
         return true;
     }
 
     public static void decreaseRowsAbove(int y) {
         for (int i = y; i < h; ++i)
             decreaseRow (i);
     }
 
     public static void deleteFullRows() {
         for (int y = 0; y < h; ++y) {
             if (isRowFull(y)) {
                 deleteRow(y);
                 decreaseRowsAbove(y+1);
                 --y;
             }
         }
     }
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 }

Group.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Group : MonoBehaviour {
 
 
 
     bool isValidGridPos() {        
         foreach (Transform child in transform) {
             Vector2 v = Grid.roundVec2(child.position);
 
             // Not inside Border?
             if (!Grid.insideBorder(v))
                 return false;
 
             // Block in grid cell (and not part of same group)?
             if (Grid.grid[(int)v.x, (int)v.y] != null &&
                 Grid.grid[(int)v.x, (int)v.y].parent != transform)
                 return false;
         }
         return true;
     }
 
     void updateGrid(){
         for (int y = 0; y < Grid.h; ++y)
             for (int x = 0; x < Grid.w; ++x)
                 if (Grid.grid [x, y] != null)
                     if (Grid.grid [x, y].parent == transform)
                         Grid.grid [x, y] = null;
         foreach (Transform child in transform) {
             Vector2 v = Grid.roundVec2 (child.position);
             Grid.grid [(int)v.x, (int)v.y] = child;
         }
     }
 
     // Use this for initialization
     void Start () {
         if (!isValidGridPos ()) {
             Debug.Log ("GAME OVER");
             Destroy(gameObject);
         }
         
     }
     
     // Update is called once per frame
     void Update() {
 
         float lastFall = 0;
         // Move Left
         if (Input.GetKeyDown(KeyCode.LeftArrow)) {
             // Modify position
             transform.position += new Vector3(-1, 0, 0);
 
             // See if valid
             if (isValidGridPos())
                 // It's valid. Update grid.
                 updateGrid();
             else
                 // It's not valid. revert.
                 transform.position += new Vector3(1, 0, 0);
         }
 
         // Move Right
         else if (Input.GetKeyDown(KeyCode.RightArrow)) {
             // Modify position
             transform.position += new Vector3(1, 0, 0);
 
             // See if valid
             if (isValidGridPos())
                 // It's valid. Update grid.
                 updateGrid();
             else
                 // It's not valid. revert.
                 transform.position += new Vector3(-1, 0, 0);
         }
 
         // Rotate
         else if (Input.GetKeyDown(KeyCode.UpArrow)) {
             transform.Rotate(0, 0, -90);
 
             // See if valid
             if (isValidGridPos())
                 // It's valid. Update grid.
                 updateGrid();
             else
                 // It's not valid. revert.
                 transform.Rotate(0, 0, 90);
         }
 
         // Move Downwards and Fall
         else if (Input.GetKeyDown(KeyCode.DownArrow) ||
             Time.time - lastFall >= 1) {
             // Modify position
             transform.position += new Vector3(0, -1, 0);
 
             // See if valid
             if (isValidGridPos()) {
                 // It's valid. Update grid.
                 updateGrid();
             } else {
                 // It's not valid. revert.
                 transform.position += new Vector3(0, 1, 0);
 
                 // Clear filled horizontal lines
                 Grid.deleteFullRows();
 
                 // Spawn next Group
                 FindObjectOfType<Spawner>().spawnNext();
 
                 // Disable script
                 enabled = false;
             }
 
             lastFall = Time.time;
         }
     }
 }
 
 


Comment
Add comment · Show 1
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 leech54 · May 18, 2017 at 05:18 PM 0
Share

your script moves 1 unit in the selected direction per frame if the button is held down.

time.deltaTime will give you the actual time that has passed since last frame. If you set a speed variable you can control how fast you want to move over a second by saying time.deltaTime * speed

transform.position.Translate(new Vector3(time.deltaTime* xspeed, 0, 0)); for right transform.position.Translate(new Vector3(0,time.deltaTime* yspeed, 0)); for up

1 Reply

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

Answer by NoseKills · May 19, 2017 at 06:16 AM

I think your only problem regarding the fall speed might be that you have made float fallSpeed a local variable in the update method. I mean you pretty clearly re-declare and set it to 0 every time you enter Update()

  void Update() {
 
      float lastFall = 0;

So Time.time - lastFall will be a pretty big number every time you checkn it with

 Time.time - lastFall >= 1

Just make lastFall a field outside the Update merhod and remeber to reset it lastFall = Time.time when creating a new piece... At least the first piece.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Root motion or script motion to make a character like riven? and any suggestions for learning resources 0 Answers

Why wont my script detect the input of my controller? 1 Answer

How to create a script that displays a text box when the user collects an item?,How to create a script that displays a text box when the user collects an object? 1 Answer

Grab and Drop Script goes through walls. 0 Answers

Scripting relating to FIRST PERSON SHOOTER 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