Sunday, June 8, 2014

Tech Blog: Unity 2D Tank Controls - Part 1

After an excruciatingly painful introduction to Unity, I have finally managed to get my Jellyfish rotating the way it is supposed to be rotating! The internet is both helpful and not helpful. It's only helpful if you understand what is being said to you, but I've managed, experimented, and succeeded.

Let's get right to business, shall we?

transform.eulerAngles = new Vector3(0,0,Input.getAxis("Horizontal")) 

If you're reading this, THANK YOU FOR BEING COURTEOUS!!!! The above code is just to rotate your object and not for forward motion. You'll have to do that on your own.

Okay so my incredible journey started with doing the obvious thing and trying to manipulate the object's Z-axis rotation value directly.

this.transform.Rotate(0,0, angle)

This code works.....kind of......

The problem is that the rotation is continuously compounded by the value of Angle, which was set to Input.getAxis("Horizontal") earlier in my code. This translates to an object spinning faster than a balled up Sonic the Hedgehog on your screen, with the speed moving in a parabolic pattern (meaning if you press the opposite direction, the spinning will slow down and then repeat in the opposite direction).

That was not what I wanted. It looked like the Jellyfish was placed in a front opening washing machine with a window inside of it. It was a blur.

So I did more research and found that I also had the option of using transform.eulerAngles to achieve my desired effect. I wasn't sure if there was a difference between euler angles or regular angles, so i tried it out. My next line of code was this.

transform.eulerAngles = new Vector2(0,Input.getAxis("Horizontal"))

This being done in Unity 2D (with a perspective camera), I assumed that you would go with Vector 2 where everything is supposed to be flat. Well it worked, kind of again (most compiled code will work. It just won't always work the way you want it to work). What happened is that my Jellyfish started spinning in the wrong dimension. Do you now the effect when you hold something up on a string and it spins around like the Earth does? Yeah, it was doing that. So I switched the Y and X axis to experiment with this.

transform.eulerAngles = new Vector2(Input.getAxis("Horizontal",0))

You can guess what happened here based on the code I presented so far. Are you ready? My Jellyfish started flipping out! Literally! So that's when I was getting pissed, and went against what I thought would work to use something that wasn't so obvious: Vector 3. I stepped it up a level, and placed Horizontal on the Z axis to be greeted with a jellyfish gently swimming the way it should.

It was swimming using the rotation point declared when you first input a sprite (because you get the option to change the rotation point in the inspector and I overlooked that) but I liked the effect that it had on my Jelly. 


In a nutshell, that first line of red code is how you get something to rotate pressing left/right. I hope this saves you some time in the future. Make sure to put it in Update or else it won't work, obviously. Next time, I will be talking about a couple of useful inspector tools to make using specific variables easy, and then afterwards I will be talking about using one object for many objects! Jaa ne!

No comments:

Post a Comment