Rotate to direction of keypress

Hi,

I seem to be having trouble understanding how to do movement similar to that of Crossy Road. I want the Rigidbody player to rotate via the keyboard (cursors) to the direction they are about to jump.

I used ‘add force’ to jump and move forward together as that seemed the logical method, but I don’t seem to be able to find infomation how to rotate towards a given direction when selected via keyboard. I don’t just wish the player object to flip/mirror to that angle, I want it to turn to that direction.

I’ve been using Unity for a few months now, and I’ve searched endlessly for this movement, but I can’t seem to find an exact answer. Any help would be greatly appreciated.

Cheers, Jay.

I can try to figure out a solution right now if you want

That’d be fab

Anytime you have two orthogonal axes (in this case X,Z), you can feed them into Mathf.Atan2() and get a rotation around the third orthogonal axis (in this case Y).

This angle is measured in radians (-pi to +pi).

Anytime you have an angle in radians you can convert to degrees by multiplying by Mathf.Rad2Deg

That’s it, that’s all the magic there is to turning a 2D vector back into a rotation around the third axis.

Obviously the output from Atan2() is undefined at zero total vector magnitude (generally it will return zero, even though Unity does not specify this specific edge case (both x and y equal to zero)), so usually you guard against using this rotation unless you have at least a small amount of total vector magnitude.

Thanks for the response, however the details are just a bit beyond my scope currently. I was more hoping someone could give me a little script as an example :wink:

I’ve just no idea how to implement something as this. Although I’m sure if I saw it written down I’d be able to grasp it to some degree.

You’re welcome to look at my MissileTurnTowards microgame… see attached package.

It’s just a simple one-script game. The relevant Mathf.Atan2() usage is on or about line 95.

Put some Debug.Log() calls in there and you can figure out how it all works.

6041018–652844–MissileTurnTowards.unitypackage (11.3 KB)

Thanks, I’ll check it out shortly :slight_smile:

Hi Kurt-Dekker,

I’ve just viewed you mini game/script, thanks for letting me have a look at it. However I don’t think quite what I’m looking for exactly, although please feel free to let me know if you think otherwise.

I’m looking for rotation similar to this:

It might not seem so obvious in the animated gif, bu when pressing the left arrow cursor, the object rotates to the left axis from it’s original direction - and so on for each new axis when an arrow curor is pressed. When implemented, for the movement I was thinking of adding a force in the forward direction and one in the Y axis for jump.

The built-in Character controller from Unity already does this. I wrote about it here along with corrected code example:

https://discussions.unity.com/t/811250/2

If you’re just cloning crossy birds motion, do yourself a favor and start with any one of the many tutorials already out there.

6958394--819344--Screen Shot 2021-03-21 at 12.45.40 PM.png

I thought the Unity character controller was for FPS movement, which is different to my example. I’d rather hace specific/exact code so that I can learn and understand it.

There are several Unity tutorials around, but most don’t do it the right way or are incomplete. One that seemed like it was a good example has been stolen on YouTube by someone and is fake. The one you suggested was a live stream and it is full of mistakes and is very hard to follow. This evening I’m checking out a kids implementation of the movement on YouTube:

The only worry I have is that it’s from 2015, which may mean some of the code is obselete like many tutorials more than a few years old are for Unity.

I purchased a Udemy course a while back for Crossy Road in Unity, but again it’s badly made and full of mistakes, plus it looks pants.