Trigger diagonal rotation - Problem

Hey, guys! Remember that RPG i said i was working on? Well, i am glad to say that the basics are 99% finished. Now everything i need to do is design some quests, and change some script variables everytime i want to make something actually work, and in a few weeks maybe my RPG will be just fine!

Sort of… You probably noticed the 99%. Well, that’s because i am facing a little problem with triggering diagonal movement. You see, i am good with about anything, EXCEPT scripting characters. I have a single walking animation for my character, so i am using a really simple trick to make it’s body rotate everytime the player presses W,A,S, or D. The problem here is that i can rarely make the body rotate diagonally by pressing a combination (Ex: W+D → Diagonal Right) because i would need to press both keys at the same time. Also, what i am using is pretty stupid. As i said, i am NOT good at scripting characters, so i am kinda stuck here. Any hint?

Here is what i am (stupidly) doing:
101402-bodyr1.png

101403-bodyr2.png

The code looks fine.
I´m by no means a professional Coder myself, but I would say, when the code is easily understood and works it´s fine.

Why don´t you check for additional Keypresses?
e.g. when you already recorded the KeyDown-Event in Direction “W”, you check for KeyDownEvents for “A” and “D”, and then alter the direction acordingly?

It would be alot easier, if you could use Input.GetKey instead of Input.GetKeyDown.
Maybe check if the rotation of the body is already correct, before overriding it, if that was your reason for using GetKeyDown in the first place.

Good luck on your project.
Grizzly

Edit: misstood the question

I haven’t tried this but here is what I came up with

// get input axis and convert to angle
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");

float angle = 0; // defaults to forwards
if (horizontal > 0f) // turn right
    angle = 90f;
else if (horizontal < 0f) // turn left
    angle = -90f;
if (vertical > 0f) // add forward to angle
    angle /= 2f;
else if (vertical < 0f)
{
    if(horizontal != 0)
        angle *= 1.5f; // add back to angle
    else
        angle = 180; // back 
}

body.eulerAngles = new Vector3(0, angle, 0);