Turning to Face Direction of 2 Axis

(Side Note: Is that how you write the plural of “Axis”?)

In my current game I am using CNControls to handle input on Mobile devices.

Since I shouldn’t expect everyone to be using CNControls the important information is that it handles inputs in the Horizontal Axis and Vertical Axis (Pressing up will have Vertical input be 1 and down will be -1 and everything in-between, same for Horizontal).

In the game you are driving a tank that needs to turn to whatever direction is pressed. I actually need this done two ways: The tank chassis itself needs to get a “Desired Quaternion” which is where the joystick is being held then turn to it based on a variable turning speed (For example, if you push down-left while facing Up then it would turn to the left until it faces that direction). Then on the other hand, the turret (which is a separate object) needs to immediately snap to the direction being pushed. I just am not sure how to go about getting a direction out of the Vertical and Horizontal Inputs.

@Jtoomer

Hi there,

You are working with two different kinds of rotations but;

A. You can get the direction simply by turning your stick location to vector - you already have it between -1 and 1 in both axis! So you get a direction vector pointing in imaginary direction where the world (stick area) top edge is north… for example.

B. Then you can use this direction to rotate your tank body and turret.

C. For Instant rotation you can use Quaternion.LookRotation

D. For continuous, non instant rotation, you can use Quaternion.RotateTowards… just run it in update for example.

A note; you might have to think about your world direction, unless your joystick north and camera north are aligned, so you have to take this into account too.

“axes”

I spent longer than I care to admit going back and forth with myself thinking that was what it was then saying “No that cannot be right”.

I managed to work something else out that might be interesting. I added a GameObject that in Update sets its position to the position of the object it is aiming for then it adds the Horizontal and Vertical values to the X and Y axis. This sets a near-perfect aiming direction which can be used with “LookAt”. Thanks for the RotateTowards though, that is extremely helpful.