- Home /
How to store direction so that player faces last direction when joystick is idle
Hi all, I am a bit stuck here, as per the title, when the joysticks are idle, how can I leave the player facing in the last moved direction? Currently, he snaps back to default. Here is the code I am using to rotate the player
var angle = Mathf.Atan2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, angle, 0);
I tried this, but it didnt work:
var lastInput = transform.rotation = Quaternion.Euler(0, angle, 0);
if(inputZ == 0 || inputX == 0)
{
transform.rotation = lastInput;
}
Any ideas please?
Answer by AngryBurritoCoder · Jan 22, 2015 at 09:29 PM
you are updating the rotation every frame so ofcourse it returns to default. basically only rotate your player when input is detected, then dont do anything
if(input.GetButton("InputName") || input.GetButton("InputName"))
{
//Input can be changed in edit/project setting/ input ( i think )
// there there should be the Horizontal and Vertical premade inputs already, use the main naim of the buttons.
var angle = Mathf.Atan2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, angle, 0);
}
Ack, of course I was updating every frame! doh!
Thanks :)
Answer by team_eden · Jan 22, 2015 at 09:31 PM
var lastDirY:float=transform.eulerAngles.y;
if(inputZ == 0 || inputX == 0) { transform.rotation.y=lastDirY; }
Cheers!
Your answer
![](https://koobas.hobune.stream/wayback/20220613181217im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
player mouse rotation, 2D twin shooter. player won't rotate. 1 Answer
How to rotate the instantiated object automatically? 1 Answer
Orbital Camera for Roll a Ball Game 2 Answers
Snap the rotation 0 Answers
Adding Rotation to a Gameobject 1 Answer