- Home /
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:
Answer by grizzly48 · Sep 06, 2017 at 05:19 PM
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
Answer by unit_nick · Sep 06, 2017 at 06:14 PM
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);
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Boosting Character Controller Problem 0 Answers
Distribute terrain in zones 3 Answers