x


How to point in direction of arrow keys?

Hello everyone I'm wondering how to make my character face the direction of the arrow keys for example when I press the right arrow key I want my character to face 90 degrees on the Y axis and when I press the down down arrow key he faces 180 degrees but when I press both I want him to face 135 degrees. How would I do this?

more ▼

asked Oct 21 '11 at 08:34 AM

Aydan gravatar image

Aydan
180 28 37 47

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

To get the desired effect, you need to do two things-

first, in your script-

Vector3 movementDirection = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
Vector3 realDirection = Camera.main.transform.TransformDirection(movementDirection);
// this line checks whether the player is making inputs.
if(realDirection.magnitude > 0.1f)
{
    Quaternion newRotation = Quaternion.LookRotation(realDirection);
    transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
}

EDIT: Code fixed- now with 100% more sanity.

EDIT EDIT: Code augmented- now doesn't reset angles.

Now, doing it that way will cause your player to smoothly rotate all the way around the axis- which is good under most circumstances, but sometimes you don't want this (retro-inspired movement style, 8-directions). To make it rotate sharply, change the transform.rotation line to

transform.rotation = newRotation;

And then instead of using

Input.GetAxis("whatever");

use

Input.GetAxisRaw();

which returns a 1 or 0 value.

more ▼

answered Oct 21 '11 at 10:59 AM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

It's rotating like crazy on random angles not what I'm looking for.

Oct 21 '11 at 11:18 AM Aydan

What axes are you working on? It is possible that the LookRotation part doesn't know what your 'up' is supposed to be. Seriously, we can't know exactly what your circumstances are, you need to be able to adapt things people tell you to do for your specific problems!

Try replacing Quaternion.LookRotation(realDirection) with

Quaternion.LookRotation(realDirection, Vector3.forward);

It all depends on where your character is supposed to be looking!

Oct 21 '11 at 11:50 AM syclamoth

Sorry I should be more specific I the camera is looking down on the player so it a top down game and whenever I am pressing a key like right arrow key I want him to look to the right and when I press the down arrow key I want him to look back.

Oct 21 '11 at 11:55 AM Aydan

Oh, I just realised what the problem was. Let me fix that...

I was making two mistakes there- the first one was using TransformPoint instead of TransformDirection, the second one was using the transform of the player, instead of the camera- this has the unfortunate result of making the 'realRotation' change every time the character turns, which is obviously not what you want.

Oct 21 '11 at 12:22 PM syclamoth

Thanks it's working great but is there some way of making it so when i let go of the arrow key it stays at that direction instead of going back to zero?

Oct 21 '11 at 12:35 PM Aydan
(comments are locked)
10|3000 characters needed characters left
more ▼

answered Oct 21 '11 at 08:54 AM

DaveA gravatar image

DaveA
26.5k 151 171 256

Thanks but not what I'm looking for I've tried this already but when I press two keys at once it glitches between rotations.

Oct 21 '11 at 09:14 AM Aydan

What would you like to happen if your press two keys at once?

Oct 21 '11 at 10:27 AM timsk

if I press two keys at once for example the left and up arrow I want my character to move on the angle in between the two angles so it would be 315 degrees.

Oct 21 '11 at 10:48 AM Aydan
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2159
x38
x18

asked: Oct 21 '11 at 08:34 AM

Seen: 851 times

Last Updated: Oct 21 '11 at 12:53 PM