The code is; private int dropSet = 1;
private int preDropSet = 0;
private bool Falling = true;
private bool Rotating = true;
void Start()
{
}
// Update is called once per frame
void Update()
{if (transform.position.y > -5)
{if (transform.rotation.x == 0)
transform.Translate(0, -0.1f, 0);}
else if (transform.position.y > -5)
{if (transform.rotation.x == 90)
transform.Translate(0.1f, 0, 0);}
else if (transform.position.y > -5)
{if (transform.position.x == 180)
transform.Translate(0, 0.1f, 0);}
else if (transform.position.y > -5)
{if (transform.position.x == -90)
transform.Translate(-0.1f, 0, 0);}
{if (Input.GetKeyDown(KeyCode.Z))
if (Rotating == true)
{transform.Rotate(0, 0, 90);}
if (Input.GetKeyDown(KeyCode.X))
if (Rotating == true)
{transform.Rotate(0, 0, -90);}}
if (Input.GetKey(KeyCode.RightArrow))
{if (transform.position.x < -9.5)
if (Falling == true)
transform.Translate(1, 0, 0);}
if (Input.GetKey(KeyCode.LeftArrow))
{if (transform.position.x > -14.5);
if (Falling == true)
transform.Translate(1, 0, 0);}}
private void OnTriggerEnter2D (Collider2D collider)
{Rotating = false;}
}
Use code tags with proper indenting. Nobody wants to read that wall of text and try to figure out all your if statements.
Can you just help me?
No, because I’m not trying to read that wall of text.
If you want help, help people help you. It’s very little effort for you to update your post with code tags.
Fixed it.
I fixed the code.
You should sign it. You’re like the Picasso of programming.
I fixed the formatting.
@FurryMaster15 you need to read about and understand if else chains, the way you use them here makes no sense.
private int dropSet = 1;
private int preDropSet = 0;
private bool Falling = true;
private bool Rotating = true;
void Update() {
if (transform.position.y > -5) {
if (transform.rotation.x == 0) transform.Translate(0, -0.1f, 0);
}
// why do you keep checking for the same thing? it will never go in here
else if (transform.position.y > -5) {
if (transform.rotation.x == 90) transform.Translate(0.1f, 0, 0);
}
else if (transform.position.y > -5) { // or here
if (transform.position.x == 180) transform.Translate(0, 0.1f, 0);
}
else if (transform.position.y > -5) { // or here
if (transform.position.x == -90) transform.Translate(-0.1f, 0, 0);
}
{ // these brackets serve no purpose?
if (Input.GetKeyDown(KeyCode.Z)) if (Rotating == true) {
transform.Rotate(0, 0, 90);
}
if (Input.GetKeyDown(KeyCode.X)) if (Rotating == true) {
transform.Rotate(0, 0, -90);
}
}
if (Input.GetKey(KeyCode.RightArrow)) {
if (transform.position.x < -9.5) if (Falling == true) transform.Translate(1, 0, 0);
}
if (Input.GetKey(KeyCode.LeftArrow)) {
if (transform.position.x > -14.5); // does this even compile?
if (Falling == true) transform.Translate(1, 0, 0);
}
}
private void OnTriggerEnter2D (Collider2D collider) {
Rotating = false;
}
Note: the above is NOT a degree, and even if it was, it will never be exactly 90.
Here’s why rotation.x is NOT a degree:
All about Euler angles and rotations, by StarManta:
https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html
and here is why it will almost never be exactly 90:
Never test floating point (float) quantities for equality / inequality. Here’s why:
https://starmanta.gitbooks.io/unitytipsredux/content/floating-point.html
https://discussions.unity.com/t/851400/4
https://discussions.unity.com/t/843503/4
“Think of [floating point] as JPEG of numbers.” - orionsyndrome on the Unity3D Forums
You might want to rethink what you’re doing.
I suggest starting from a basic Tetris tutorial to understand the principles and build up from there.
Imphenzia: How Did I Learn To Make Games:
My old problem got fixed, but now I have a new one. Whenever the z position of the code does not = 0, the object does not fall.
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using UnityEngine;
using UnityEngine.UIElements;
using Vector3 = System.Numerics.Vector3;
public class PuyoScript : MonoBehaviour
{
private int dropSet = 1;
private int preDropSet = 0;
private bool Falling = true;
private bool Rotating = true;
void Start() {
}
void Update() {
if (transform.position.x > -5.5f)
if (transform.rotation.z == 0)
transform.Translate (0, -0.1f, 0);
if (transform.rotation.z == 90)
transform.Translate (-0.1f, 0, 0);
if (transform.rotation.z == 180)
transform.Translate (0, -0.1f, 0);
if (transform.rotation.z == -90)
transform.Translate (-0.1f, 0, 0);
if (Input.GetKeyDown(KeyCode.Space)) {
if (Rotating == true) {
transform.Rotate (0, 0, 90);
}
}
if (Input.GetKey(KeyCode.RightArrow)) {
if (transform.position.x < -9.5) {
if (Falling == true) {
transform.Translate(-1, 0, 0);
}
}
}
if (Input.GetKey(KeyCode.LeftArrow)) {
if (transform.position.x > -14.5) {
if (Falling == true) {
transform.Translate(1, 0, 0);
}
}
}
void OnTriggerEnter2D (Collider2D collider) {
Falling = false;
Rotating = false;
}
}
}
Excellent. However, from these lines alone:
… ALL of what I wrote in my post above still stands.
If you can get it working with this approach, I will not actually understand how that was done.
I seriously suggest you look to some Tetris tutorials, otherwise you are blazing new ground like a test pilot.
I tried looking at tetris tutorials, but I couldn’t find anything useful about rotation.
The point that I don’t think you’ve realised yet is that the x, y and z components of transform.rotation
are not the values that you see in the inspector. It’s a quaternion, which has w, x, y and z components, ones you should only touch if you have a maths degree.
The values in the inspector are euler angles, though they also don’t represent the actual underlying values that you might get via code. It’s made to be human readable for editing purposes.
You’re also falling into the trap of comparing floating point numbers for equality. You should generally check if they are roughly equal, such as with Mathf.Approximately
.
In your case you probably want to store the rotated state of the piece locally, rather than using the rotation of the game object itself. Use an integer if you’re only ever going to be rotating it by whole numbers at a time.
What’s the code for quaternion?
transform.eulerAngles.x/y/z will get you the rotation in degrees you see in the editor. This is what you need to work with. Just be aware of floating point precision problems.
transform.rotation.x/y/z/w <= do not use them.
How do I make an array to make an object a random color?