0
votes

So, The code for my horizontal movement was alright and it worked perfectly. The problem is i added some code for vertical movement and my keys are pretty much doing the opposite of what i want to accomplish.

My right key makes my character go up, my up key makes my character go right etc.

Any help is appreciated.

using UnityEngine; using System.Collections;

public class CharacterMovement : MonoBehaviour { public float MaxSpeed = 10f;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void FixedUpdate () 
{
    float move = Input.GetAxis ("Horizontal");
    rigidbody2D.velocity = new Vector2 (move * MaxSpeed, rigidbody2D.velocity.y);

    float moveV = Input.GetAxis ("Vertical");
    rigidbody2D.velocity = new Vector2 (moveV * MaxSpeed, rigidbody2D.velocity.x);


}
1

1 Answers

1
votes

Y is the vertical co-ordinate but you refer to it in the horizontal call. You just need to swap the .x and the .y around.

edit: Sorry rushed my answer, you need to swap the x and why, and also swap your Vector parameters, so:

void FixedUpdate () 
{
    float move = Input.GetAxis ("Vertical");
    rigidbody2D.velocity = new Vector2 (moveV * MaxSpeed, rigidbody2D.velocity.y);

    float moveV = Input.GetAxis ("Horizontal");
    rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x, move * MaxSpeed);
}