Cubes snap rotations

Hi,

Anyone found a way to rotate a cube snapping at 90 degree rotations like in the iPhone game “Cubes”? Just slide up or down?

Cheers,

Robert

I found a way to analyse to movement of my finger going left, right up or down with that script. 2 problems:

  1. My cube won’t rotate properly, don’t know why.

  2. I tried to limit the area of sampling because I need other parts of the screen for something else but that didn’t work either so I commented it.

Obviously it would be better to have a smooth rotation then a direct snap at 90 degree angles but for now I can live it that.

Here’s the code. Anybody?

var touch1H = 0.0;
var touch2H = 0.0;
var touch1V = 0.0;
var touch2V = 0.0;
var CubeToRotate : GameObject;

function Update () {
   
for (var evt : iPhoneTouch in iPhoneInput.touches)
   {
        if (evt.phase == iPhoneTouchPhase.Began)
      {
             touch1H = evt.position.x;
             touch1V = evt.position.y;

      }
     
      if (evt.phase == iPhoneTouchPhase.Moved)
      {
         
         touch2H = evt.position.x;
         touch2V = evt.position.y;
			}	
// Screen Coord for sampling

//       if (evt.position.y <235  evt.position.y >95  evt.position.x <300  evt.position.x >95) {

       if (evt.phase == iPhoneTouchPhase.Ended) {

			    if (touch1H-touch2H > 0  (Mathf.Abs(touch1H-touch2H)) > (Mathf.Abs(touch1V-touch2V))){
					print("Left");
						CubeToRotate.transform.rotation.y =  CubeToRotate.transform.rotation.y -90;
			}
			    if (touch1H-touch2H < 0  (Mathf.Abs(touch1H-touch2H)) > (Mathf.Abs(touch1V-touch2V))){
					print("Right");
						CubeToRotate.transform.rotation.y =  CubeToRotate.transform.rotation.y + 90;
			}
				if (touch1V-touch2V < 0  (Mathf.Abs(touch1H-touch2H)) < (Mathf.Abs(touch1V-touch2V))){
					print("Up");
						CubeToRotate.transform.rotation.x =  CubeToRotate.transform.rotation.x - 90;
			}
				if (touch1V-touch2V > 0  (Mathf.Abs(touch1H-touch2H)) < (Mathf.Abs(touch1V-touch2V))){
					print("Down");
						CubeToRotate.transform.rotation.x =  CubeToRotate.transform.rotation.x + 90;
			}		
			
       touch2H = evt.position.x;
       touch2V = evt.position.y;
          
       }
       
//}

}
}

Ok, just needed to change:

CubeToRotate.transform.rotation.y = CubeToRotate.transform.rotation.y -90;

for

CubeToRotate.transform.eulerAngles.y = CubeToRotate.transform.eulerAngles.y -90;

Still didn’t figure out how to restrict the sampling area.

I, too have a rotating object and I’ve figured out how to make it pretty clean. i tried all rounding the angles and things like that–in the end what made it work is I translate the swipes into rotations of (x, y) as -1 to 1 only, then do the rotation directly by whatever you want (in my case 45 degree increments) it means the rotation will never be off.

For added cleanliness I actually have a hidden object that I’m actually rotating by numbers. Then, the visible object just lerps the rotation of the hidden one so the visible object’s rotations are smooth.

Out of curiosity–what sort of game are you rotating a cube for?

Also, when you are rotating something, don’t forget what space you are doing it in…

transform.Rotate((-y*rotateIncrement), (x*rotateIncrement),0, Space.World);

Hi Steinhot,

Right now I’m doing R&D. The cube is just a placeholder.

I’m having a hard time because I can’t figure out how to do my rotation slowly. If I add a rotation on 90 degrees it obviously just pop to 90. I thought I could make this:

for (var loopA = 0;loopA < 90;loopA++) {
CubeToRotate.transform.eulerAngles.y = CubeToRotate.transform.eulerAngles.y -1;
}

but the loop goes so fast that I still can’t see the rotation happening. So I asked on an other post how I could slow down a loop. I got an answer and I spent all day trying to make it work but no luck.

Here’s the post:

http://forum.unity3d.com/viewtopic.php?p=176208#176208

Can you help? Can you share some code?

Thanks,

Robert

PS: You’re right, I forgot the space argument. :slight_smile:

Here’s the key…

What I do is rotate a little, hidden object directly which snaps it to 90%, etc. Then, to that object I place a script that basically just does this in the update:

//C#
        public float damping = 6.0f;

        public void Update () 
        {
                zMaster.Face.transform.rotation = Quaternion.Slerp(zMaster. Face.transform.rotation, transform.rotation, Time.deltaTime * damping);
        }

Where zMaster is a script that has a static called “Face” that is the object that I want to rotate. Adjust the damping to suit your taste, but this will give it a nice smooth animated motion.

Also, with your For each loop, looks like you are a bit confused with Unity overall.

There is very little need for loops, especially to animate things over time as everything is one big loop.

The update function fires once every game cycle or “frame” so that is your loop. What my function does is move it a little each game cycle giving you the movement.

You want to do as little as possible in a game loop. And would really rarely do loops unless you are looping a bunch of objects to act on.

There are some great tutorials out there. The ones I got the most out of were done by Will Goldstone. http://www.learnmesilly.com/ Under unity.

Watch them in their entity. They are an amazing kick start. I am 90% done with my game and I have only been at it for 3 weeks.

Confused is an understatement… well, not that bad… but some parts are still a mystery for me. I come from a Hollywood special FX background. Lots of high end 3D experience but practically no programing experience. Market is dead these days in Montreal so I’m trying something else. Obviously it will get better once I do a javascript course (working on this…)

You can see my demo reel at jungleofsharks.com

My first game is almost done too. The only thing left to do is the scoring system
and in this game it’s pretty easy.

Thanks for the demo link.

-R

Wow! Amazing reel! Can’t wait to see what you come up with. I, too, come from a bit of a film background but all indy. Last year I even brought a Red Camera, which, once I got going with my game, sits collecting dust. I even turned down a commercial this weekend to hopefully finish my game.

Definitely watch those tutorials. I come from a more programming background (programming all of my life) so the programming was nothing for me–just a matter of learning the commands. It’s the 3D stuff that I struggle with.

I use Cinema 4D studio, but I wish I had your skills to work with it.

Good luck and let me know if there is anything I can do to help answer questions.

Glad you like my stuff.

We can always do a service exchange. I make some 3D stuff for you and you write me some code.

My coordinates are in my résumé if you need me.

Cheers,

-R

To be fair, he’s using the loop only to loop through the touches array. Maybe only the first touch should rotate the object, but it’s a minor point.

Have you tried using the Ani.mate class from the GUI manager? Seems like a simple way to do this. Just detect a swipe, figure out its direction, and rotate via Ani.mate.

I was using a loop because I couldn’t use an animation as it resets the geometry coordinates when you run it again and I couldn’t find a way to add an animation over another one, meaning first anim rotates 90 in y. The second time it will add another on to get 180. So I thought I could do a loop with a “for” to add 1 degree 90 times. But the loop was going too fast so I couldn’t see the rotation happening.

Then I tried to make my rotations as separated functions outside the “function update” so I can add a yield for a fraction of a second. That’s what I’m working on right now. The problem I have now is that it keeps sampling the screen while it does the loop so my cube turns like crazy. I think I will try to disable the sampling script while I do the loop and reactivate it once it’s done.

I’m interested to learn about that ani.mate thing but when I search the docs I find nothing. Can you tell me more please?

-R

Hello

Had exactly the same problem and got some great answers: http://forum.unity3d.com/viewtopic.php?p=185142#185142

I used the first Javascript sample: Hope it helps you too!

Btw your animations are awesome riouxr! Are you in general interested in game projects? :wink:

Greetings
Tayger

Hi guys,

If you want to know why I needed these rotating cubes the answer is Cubic Match!

iTunes link

http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=326575797&mt=8

Thanks for the help!

Robert