- Home /
Rotate to facing direction
Does anyone know how to get a character to rotate and move in the direction of the key they press?
Like pressing the left key makes the character look left and move that way?
I need it for a 2.5D game.
Answer by VS48 · Nov 09, 2010 at 07:48 PM
transform.forward = Vector3.Normalize(new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")));
Edit in response to comment
The transform.forward vector is the "forward" orientation of the transform (the positive Z direction in local space, aka object space). So, when we assign & change this direction, Unity will automatically rotate your object so that its Z axis is the same as our supplied forward vector.
The GetAxis() calls return a value -1 to 1 depending on where the input is positioned along that axis. On the horizontal, -1 is left and 1 is right. On the vertical, 1 is up and -1 is down. So here, new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")), the left-right keys will make the X component of the vector -1 and 1 (respectively), and similarly with the Z axis and the up-down keys.
So the code will give you 8 directions (in 45-degree increments). The default value, when all input is 0, is not defined explicitly.
I was in a rush when I typed that, but a better and much easier solution is only updating when an edge is encountered, for example:
if (Input.GetKeyDown(KeyCode.W))
transform.forward = new Vector3(0f, 0f, 1f);
else if (Input.GetKeyDown(KeyCode.S))
transform.forward = new Vector3(0f, 0f, -1f);
else if (Input.GetKeyDown(KeyCode.A))
transform.forward = new Vector3(1f, 0f, 0f);
else if (Input.GetKeyDown(KeyCode.D))
transform.forward = new Vector3(-1f, 0f, 0f);
The GetKeyDown function will return true when the key goes from being released to pressed (when the user starts pressing the key). Something like that should work for 4 directions and I think requires no explanation.
The forces it to face the Z axis when a button is not pressed and it looks in the direction I'm ai$$anonymous$$g at, but moves in one direction.
Also, can you explain how it works?
The turning direction is fixed but it still moves in only one direction.
Would changing the movement to be based off transform.position work to move it in that direction?
Hey. I also happen to find this interesting. I'm prototyping a certain Idea...It's 2D. The player moves in whichever direction key he presses but I want to orient the character in that direction. I was thinking that would be a simple task, but it's proven to be tougher than I imagined.
Answer by willeml85 · May 06, 2012 at 09:03 PM
Create a empty player object and place the below script on it, then put the playergraphics and animations in a child object of player and drag it into the inspector. This is in C#(csharp).
public class Player : MonoBehaviour {
private float speed = 1.5f;
public Transform playergraphic;
void Update (){
Movement();
}
void Movement (){
//Player object movement
float horMovement = Input.GetAxisRaw("Horizontal");
float vertMovement = Input.GetAxisRaw("Vertical");
if(horMovement != 0 && vertMovement != 0){
speed = 1.0f;
}else{
speed = 1.5f;
}
transform.Translate(transform.right * horMovement * Time.deltaTime * speed);
transform.Translate(transform.forward * vertMovement * Time.deltaTime * speed);
//Player graphic rotation
Vector3 moveDirection= new Vector3 (horMovement, 0, vertMovement);
if (moveDirection != Vector3.zero){
Quaternion newRotation = Quaternion.LookRotation(moveDirection);
playergraphic.transform.rotation = Quaternion.Slerp(playergraphic.transform.rotation, newRotation, Time.deltaTime * 8);
}
//Player graphic animation
if(moveDirection.magnitude > 0.05f){
playergraphic.animation.CrossFade("walk", 0.2f);
}else{
playergraphic.animation.CrossFade("idle", 0.2f);
}
}
}
@willeml85 has it down, this is exactly what I was looking for! THAN$$anonymous$$ YOU!!!
This was pretty much the exact code that ended up working, except I just set another var called currentRotation and set it to the newRotation after you rotate it. Then I created an else statement for the if (moveDirection != Vector3.zero) part so that whenever you stop moving, you do the same thing but you just Slerp it to the currentRotation, like this: transform.rotation = Quaternion.Slerp(transform.rotation, currentRotation, Time.deltaTime * 8.0f);
I am working on a twin stick shooter kinda like Dead Nation, and this got my movement from 0% to 98%. I ended up having 3 movement states for 2 sticks, those being: no movement, only left stick movement, only right stick movement & left and right movement which is the same as only right movement since the right stick takes over rotation if both are moving and you get a strafe effect. I just have one more thing to figure out :)
Answer by DeeDeeKaKa · Nov 01, 2013 at 03:05 AM
You could give this a try..
float faceDirection = Input.GetAxisRaw("Horizontal");
if(faceDirection != 0)
{
transform.forward = new Vector3(faceDirection, 0, 0);
}
This is just to rotate the character in the proper direction. To move him afterwards, use a
controller.Move(moveDirection * Time.deltaTime)
function. This works for me, and I'm making a 2.5D Platformer
Answer by Ben 18 · Nov 09, 2010 at 07:42 PM
I am new to unity, but I believe there is an example of this in Standard Assets ThirdPersonController script. I also think there is another example in the platformer tutorial file. http://unity3d.com/support/resources/tutorials/3d-platform-game
Answer by d.toliaferro · Oct 24, 2012 at 10:03 PM
I don't know if this is "good" code, but it seems to work:
using UnityEngine;
using System.Collections;
public class Move : MonoBehaviour {
bool facingRight;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.A)) {
transform.right = new Vector3(-1, 0, 0);
facingRight = false;
}
else if (Input.GetKeyDown (KeyCode.D)) {
transform.right = new Vector3(1, 0, 0);
facingRight = true;
}
float translate = Input.GetAxis("Horizontal");
if(!facingRight)
transform.Translate (Vector3.right * -translate * 5 * Time.deltaTime);
else
transform.Translate (Vector3.right * translate * 5 * Time.deltaTime);
}
}
Could be simplified to:
void Update()
{
float translate = Input.GetAxis("Horizontal");
transform.forward = new Vector3(translate, 0, 0);
transform.Translate (Vector3.forward * $$anonymous$$athf.Abs(translate) * 5 * Time.deltaTime);
}
But yes, it does work your way, but only when you use WASD. $$anonymous$$eep in $$anonymous$$d that "Horizontal" could be the arrow-keys or a joystick axis.
Hmm, that code makes the guy turn forward once you release the button.
How do you make it so that he stays facing the direction you move? It currently defaults to facing forward when you release the button.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Character rotation help needed. 1 Answer
Quaternion.FromToRotation misunderstanding 2 Answers
Rotating towards the mouse. 0 Answers
Erratic behaviour during rotation 1 Answer