C
C#6mo ago
anF

Tetris Block Phasing through grid

got no clue why this is happening
15 Replies
anF
anF6mo ago
here is my current code using UnityEngine; public class Tetromino : MonoBehaviour { float fall = 0; public float fallSpeed = 1; // Use this for initialization void Start() { } // Update is called once per frame void Update() { CheckUserInput(); } void CheckUserInput() { if (Input.GetKeyDown(KeyCode.RightArrow)) { SafeMove(new Vector3(1, 0, 0)); } else if (Input.GetKeyDown(KeyCode.LeftArrow)) { SafeMove(new Vector3(-1, 0, 0)); } else if (Input.GetKeyDown(KeyCode.UpArrow)) { SafeRotate(90); } else if (Input.GetKeyDown(KeyCode.DownArrow) || Time.time - fall >= fallSpeed) { SafeMove(new Vector3(0, -1, 0)); fall = Time.time; } } void SafeMove(Vector3 vec) { transform.position += vec; if (!CheckIsValidPosition()) { transform.position -= vec; } } void SafeRotate(float degree) { transform.Rotate(0, 0, degree); if (!CheckIsValidPosition()) { transform.Rotate(0, 0, -degree); } } bool CheckIsValidPosition() {
foreach (Transform mino in transform) { Vector2 pos = FindObjectOfType<Game>().Round(mino.position); if (!FindObjectOfType<Game>().CheckInsideGrid(pos)) { return false; } } return true; } }
TheRanger
TheRanger6mo ago
$code
MODiX
MODiX6mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
anF
anF6mo ago
// using UnityEngine;

public class Tetromino : MonoBehaviour
{
float fall = 0;
public float fallSpeed = 1;

// Use this for initialization
void Start()
{

}

// Update is called once per frame
void Update()
{
CheckUserInput();
}

void CheckUserInput()
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
SafeMove(new Vector3(1, 0, 0));
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
SafeMove(new Vector3(-1, 0, 0));
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
SafeRotate(90);
}
else if (Input.GetKeyDown(KeyCode.DownArrow) || Time.time - fall >= fallSpeed)
{
SafeMove(new Vector3(0, -1, 0));
fall = Time.time;
}
}

void SafeMove(Vector3 vec)
{
transform.position += vec;
if (!CheckIsValidPosition())
{
transform.position -= vec;
}
}

void SafeRotate(float degree)
{
transform.Rotate(0, 0, degree);
if (!CheckIsValidPosition())
{
transform.Rotate(0, 0, -degree);
}
}

bool CheckIsValidPosition()
{
foreach (Transform mino in transform)
{
Vector2 pos = FindObjectOfType<Game>().Round(mino.position);

if (!FindObjectOfType<Game>().CheckInsideGrid(pos))
{
return false;
}
}

return true;
}
}
// using UnityEngine;

public class Tetromino : MonoBehaviour
{
float fall = 0;
public float fallSpeed = 1;

// Use this for initialization
void Start()
{

}

// Update is called once per frame
void Update()
{
CheckUserInput();
}

void CheckUserInput()
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
SafeMove(new Vector3(1, 0, 0));
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
SafeMove(new Vector3(-1, 0, 0));
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
SafeRotate(90);
}
else if (Input.GetKeyDown(KeyCode.DownArrow) || Time.time - fall >= fallSpeed)
{
SafeMove(new Vector3(0, -1, 0));
fall = Time.time;
}
}

void SafeMove(Vector3 vec)
{
transform.position += vec;
if (!CheckIsValidPosition())
{
transform.position -= vec;
}
}

void SafeRotate(float degree)
{
transform.Rotate(0, 0, degree);
if (!CheckIsValidPosition())
{
transform.Rotate(0, 0, -degree);
}
}

bool CheckIsValidPosition()
{
foreach (Transform mino in transform)
{
Vector2 pos = FindObjectOfType<Game>().Round(mino.position);

if (!FindObjectOfType<Game>().CheckInsideGrid(pos))
{
return false;
}
}

return true;
}
}
TheRanger
TheRanger6mo ago
why is what happening exactly?
anF
anF6mo ago
so I am making a tetris game and my block is phasing through the bottom part of the grid I am trying to make boundrys that the block cant past the two sides are fine its just this bottom part
TheRanger
TheRanger6mo ago
how is checkInsideGrid coded? tbh it doesnt look like ur checking every square of the tetromino your tetromino has 4 squares each square should have its own position
anF
anF6mo ago
yeah i just started c sharp today that must be it hmmm
TheRanger
TheRanger6mo ago
oh no, ur gonna have a super hard time learn the basics first, then Unity later $helloworld
anF
anF6mo ago
my teacher is making me do a multimedia system where I gotta make a game its so dumb cuz she hasent even shown us how to use any programs its a big assighment will look at that I have been following this video serires and copying what the guy does
anF
anF6mo ago
The Weekly Coder
YouTube
How to make a game like Tetris in Unity 5 - Part 3 - Tetromino Grid...
►►► Project Source Code is available to our patrons. Head over to our Patreon page https://www.patreon.com/bePatron?u=2811576 and show support to get access to all project source code which is just one of the many benefits of being a patron.   ► Source Code http://weeklycoder.com/product/tetris-clone-bundle/ *** SPECIAL FOR YOUTUBE SUBSCRIBERS...
anF
anF6mo ago
its kinda out dated tho cuz his code aint even working
TheRanger
TheRanger6mo ago
well you need to know what ur doing, copying code wont help you
anF
anF6mo ago
yeah I ended up fixing my problem buy playing around with some stuff