Question by
Ketannine99 · Mar 29, 2021 at 09:16 PM ·
controlleraxisif statementif-else
So I'm very new at Unity and I tried to improvise the controllers with if statement.
I tried to use the old Input.GetAxisRaw("Horizontal") and Vertical but it didn't work quite well, my character was teleporting upwards everytime i moved it, so I used this if method and it works, but i feel like i shouldn't be doing that. just to add a note here, my project is a 3D where I'm mixing with 2D sprites, that's why I'm telling to set the directions to true or false, so it calls the right animation.
public Animator Anime;
public float speed;
public bool up;
public bool down;
public bool left;
public bool right;
// Start is called before the first frame update
void Start()
{
speed = 6;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey("w"))
{
transform.Translate(0, 0, (speed * Time.deltaTime));
up = true;
}
else
{
up = false;
}
if (Input.GetKey("s"))
{
transform.Translate(0, 0, (-speed * Time.deltaTime));
down = true;
}
else
{
down = false;
}
if (Input.GetKey("d"))
{
transform.Translate((speed * Time.deltaTime), 0, 0);
right = true;
}
else
{
right = false;
}
if (Input.GetKey("a"))
{
transform.Translate((-speed * Time.deltaTime), 0, 0);
left = true;
}
else
{
left = false;
}
Anime.SetBool("cima", up);
Anime.SetBool("baixo", down);
Anime.SetBool("esquerda", left);
Anime.SetBool("direita", right);
}
}
Comment
Your answer
Follow this Question
Related Questions
Unity Android Controlls with CrossPlatformInput 1 Answer
Using an input axis to change a float 0 Answers
Relative rotation on a custom axis? 0 Answers
Rotate car wheels with mouse position and not arrow keys 0 Answers
I need to check if a GameObject exists between two values on the y coordiante, how can I do that? 0 Answers