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.
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
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.
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.
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.