How to use Input.GetAxis for character movement?
Hi all I have a simple character controller that moves my character in 4 directions using input.GetAxis just fine but I am having a really difficult time figuring out how to access it for use in say an if statement. I know that using GetKey I could use something like the following and then easily add more logic into it, and set up specific logic for left right up and down.
void Update () {
if (Input.GetKey(KeyCode.RightArrow)){
transform.position += Vector3.right * speed;}
How would I do this using GetAxis? By the way I decided on a controller that uses input.GetAxis so that I could use a rigidbody to be able to use the physics engine later on in case anyone was wondering. Here is my relevant code, Thank you so much in advance!
public Vector2 speed = new Vector2 (50, 50); private Vector2 movement; private Rigidbody2D playerRigid;
void Start ()
{
playerRigid = GetComponent<Rigidbody2D> ();
}
void Update ()
{
float inputX = Input.GetAxis ("Horizontal");
float inputY = Input.GetAxis ("Vertical");
movement = new Vector2 (
speed.x * inputX,
speed.y * inputY);
}
void FixedUpdate ()
{
playerRigid.velocity = movement;
}
Answer by Jessespike · Dec 10, 2015 at 08:17 PM
you mean like this?
if (movement.x < 0f) {
Debug.Log ("left");
} else if (movement.x > 0f) {
Debug.Log ("right");
}
if (movement.y < 0f) {
Debug.Log ("down");
} else if (movement.y > 0f) {
Debug.Log ("up");
}
Brilliant! Put that into my Update function and the debugs showed up immediately no errors. Can't ask for better than that, thank you so much! <3
Cool, glad it worked out. Could you accept my answer so the question can be closed. Cheers.