- Home /
Classic Tetris like game - brick movement.
Hi community
I'm in the middle of a school project and i decided to make a Tetris game 'cause it's a game everyone can relate to, but...
I've got a problem when it comes to the movement, until now I've got a script that looks like this:
var target : Transform;
function Update () { if(Input.GetButtonDown("Fire1")) { // Vector3(x,y,z) transform.Translate(1,0,0); } else if(Input.GetButtonDown("Fire2")) transform.Translate(-1,0,0); else if(Input.GetButtonDown("Jump")) transform.RotateAround(target.position, Vector3.forward, 90);
//transform.Rotate(Vector3.right, Time.deltaTime);
}
But the problem is that i want my brick to move in the Global axis and the rotation to be the local z axis, more specific i want it to rotate around the center pivot, how is that possible? I've been looking around at other questions and tried different scripts but this one is the closest i can get.
transform.RotateAround(target.position, transform.forward, 90);
Answer by Jakob Steinn · Nov 09, 2010 at 08:18 AM
Well i stayed up late last night so i could figure this out. I want to thank cmixco and VS48 for their fast answers. but here's what I've come up with:
'var target : Transform; var hasRotated : boolean = false; var angle1 : int = 90; var angle2 : int = -90;
function Update () {
if(Input.GetButtonDown("Jump") && hasRotated == true) {
transform.RotateAround(target.position, Vector3.forward, angle1);
//transform.localRotation = Quaternion.AngleAxis(90,Vector3.forward);
//transform.RotateAround (Vector3.forward, Vector3.forward, 90);
hasRotated = false;
} else if(Input.GetButtonDown("Jump") && hasRotated == false) {
//transform.RotateAround (Vector3.forward, Vector3.forward, 270);
transform.RotateAround(target.position, Vector3.forward, angle2);
hasRotated = true;
}
if(Input.GetButtonDown("Fire1") && hasRotated == false) { // Vector3(x,y,z) transform.Translate(1,0,0); } else if(Input.GetButtonDown("Fire1") && hasRotated == true) { //Vector3(x,y,z) transform.Translate(0,1,0); }
if(Input.GetButtonDown("Fire2") && hasRotated == false) {
transform.Translate(-1,0,0);
} else if(Input.GetButtonDown("Fire2") && hasRotated == true) {
transform.Translate(0,-1,0);
}
//transform.Rotate(Vector3.right, Time.deltaTime);
}'
Please let me know if you see any way to optimize this script! Thanks again. - Jakob Steinn
looks just right the way it is, no new keywords hiding in calls which should be locals. No null references hiding around. Got more code you need checked? I'm sure the people at UnityAnswers can quickly spot some optimizations
Answer by cmixco · Nov 09, 2010 at 03:19 AM
place the actual shape into an empty game object where the shape is centered perfectly. then update the rotation/movement of the empty and voila! thats what i did for my quasi-tetris game...
myTransform.Rotate(Vector3.right, 90); //x axis
myTransform.Rotate(Vector3.up, 90); //y axis
myTransform.Rotate(Vector3.forward, 90); //z axis