[SOLVED] Making 2D character to look at the direction of character is moving to immediately

My character is moving by axis, Input.GetAxisRaw(“Horizontal”) and Input.GetAxisRaw(“Vertical”). How can I change it’s rotation immediately by using them? I don’t want to use RotateToward. When my character is moving upwards, Rotatian x, y and z are 0.

Something simple like transform.right = movementDirection; where movementDirection is a Vector2 coming from your input axis code should be sufficient.

1 Like

I used: transform.right = new Vector2(Input.GetAxisRaw("Vertical"), -Input.GetAxisRaw("Horizontal")); and it works perfectly except when my character moves down. When I go down rotation goes upward.

Well that would be the little minus sign workin’ it’s magic!

8892390--1216080--minus.png

EDIT: looks like you got your horizontal and vertical swapped.

Instead of massively hairy long lines like that, read the input ONCE and assign it to a local variable.

Then act on it: move, rotate, whatever.

If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.

How to break down hairy lines of code:

http://plbm.com/?p=248

Break it up, practice social distancing in your code, one thing per line please.

“Programming is hard enough without making it harder for ourselves.” - angrypenguin on Unity3D forums

“Combining a bunch of stuff into one line always feels satisfying, but it’s always a PITA to debug.” - StarManta on the Unity3D forums

1 Like