- Home /
How to keep position on input.get axis even when it is pressed twice?
Hello I am making a 3d side scroller and for my character im using the input.get axis to rotate the player. The thing is when I press the left key or right key twice it will still rotate the player making the character walk backwards. May someone please help me fix this?
var walkSound : AudioClip;
var soundPlaying : boolean = false;
var playerPosition : Transform; var gunPosition : Transform;
function Start () {
}
function Update () { if (Input.GetButtonDown("Horizontal"))
{ if (Input.GetAxis("Horizontal") > 0)
{
playerPosition.transform.Rotate(0, 180, 0);
Debug.Log("right");
gunPosition.transform.Rotate(0, 180, 0);
Debug.Log("right");
}
else
{
playerPosition.transform.Rotate(0, -180, 0);
Debug.Log("left");
gunPosition.transform.Rotate(0, -180, 0);
Debug.Log("left");
}
}
if (Input.GetButtonDown ("Jump")){
audio.PlayOneShot (jumpSound);
}
if (Input.GetButtonUp ("Horizontal")){
audio.Stop ();
soundPlaying = false;
}
}
@script RequireComponent (AudioSource)
Answer by cdrandin · Sep 04, 2013 at 04:24 PM
Because you are allowing it to blindly rotate when ever you press left or right to rotate some degrees. To simply stop this action just have some variables.
enum Direction
{
Left = 0,
Right
};
Direction direction;
then...
var walkSound : AudioClip;
var soundPlaying : boolean = false;
var playerPosition : Transform; var gunPosition : Transform;
enum Direction
{
Left = 0,
Right
};
Direction direction;
function Start () {
direction = Direction.(*Left or Right, depending on where your character is facing*)
}
function Update ()
{
if (Input.GetAxis("Horizontal") > 0 && direction != Direction.Right)
{
playerPosition.transform.Rotate(0, 180, 0);
Debug.Log("right");
gunPosition.transform.Rotate(0, 180, 0);
Debug.Log("right");
}
else if (Input.GetAxis("Horizontal") < 0 && direction != Direction.Left)
{
playerPosition.transform.Rotate(0, -180, 0);
Debug.Log("left");
gunPosition.transform.Rotate(0, -180, 0);
Debug.Log("left");
}
if (Input.GetButtonDown ("Jump")){
audio.PlayOneShot (jumpSound);
}
if (Input.GetButtonUp ("Horizontal")){
audio.Stop ();
soundPlaying = false;
}
}
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Rotate against player when hit 1 Answer
Ignore Player Input 4 Answers
Best way of accurately getting mouse position in world space on same plane as player? 1 Answer
Horizontal as a NGUI button 0 Answers