- Home /
Question by
Xionizzy · May 26, 2017 at 01:51 PM ·
scripting problemmovementspritescripting beginnermovement script
How can I make my 2D sprite move in the direction of the most recent key pressed?
Currently my sprite will stop when pressing two keys at the same time (Ex: pressing both W & A). How can I make it so that the rigid body will move based on the most recent key pressed?
My update function:
void Update ()
{
Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
if (movement_vector == Vector2.up)
{
anim.SetBool("isWalking", true);
anim.SetFloat("input_y", movement_vector.y);
rbody.MovePosition(rbody.position + Vector2.up * Time.deltaTime);
}
else if (movement_vector == Vector2.down)
{
anim.SetBool("isWalking", true);
anim.SetFloat("input_y", movement_vector.y);
rbody.MovePosition(rbody.position + Vector2.down * Time.deltaTime);
}
else if (movement_vector == Vector2.left)
{
anim.SetBool("isWalking", true);
anim.SetFloat("input_x", movement_vector.x);
rbody.MovePosition(rbody.position + Vector2.left * Time.deltaTime);
}
else if (movement_vector == Vector2.right)
{
anim.SetBool("isWalking", true);
anim.SetFloat("input_x", movement_vector.x);
rbody.MovePosition(rbody.position + Vector2.right * Time.deltaTime);
}
else if (movement_vector == Vector2.zero)
{
anim.SetBool("isWalking", false);
}
}
}
Comment
void Update ()
{
Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
anim.SetFloat("input_x", movement_vector.x);
anim.SetFloat("input_y", movement_vector.y);
if (movement_vector != Vector2.zero)
{
anim.SetBool("isWalking", true);
rbody.$$anonymous$$ovePosition(rbody.position + new Vector3(movement_vector.x, movement_vector.y, 0f) * Time.deltaTime);
}
else
{
anim.SetBool("isWalking", false);
}
}
Answer by justinc847 · May 26, 2017 at 10:38 PM
public int a;
void Update ()
{
Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
if (movement_vector == Vector2.up)
{
a = 1;
}
else if (movement_vector == Vector2.down)
{
a = 2;
}
else if (movement_vector == Vector2.left)
{
a = 3;
}
else if (movement_vector == Vector2.right)
{
a = 4;
}
else if (movement_vector == Vector2.zero)
{
a = 0;
}
}
}
if (a == 1) {
anim.SetBool("isWalking", true);
anim.SetFloat("input_x", movement_vector.x);
rbody.MovePosition(rbody.position + Vector2.up * Time.deltaTime);
}
if (a == 2) {
anim.SetBool("isWalking", true);
anim.SetFloat("input_x", movement_vector.x);
rbody.MovePosition(rbody.position + Vector2.down * Time.deltaTime);
}
if (a == 3) {
anim.SetBool("isWalking", true);
anim.SetFloat("input_x", movement_vector.x);
rbody.MovePosition(rbody.position + Vector2.left * Time.deltaTime);
}
if (a == 4) {
anim.SetBool("isWalking", true);
anim.SetFloat("input_x", movement_vector.x);
rbody.MovePosition(rbody.position + Vector2.right * Time.deltaTime);
}
if (a == 0) {
anim.SetBool("isWalking", false);
}
I haven't tested it but that should do exactly what you need. It looks bulky but it should work. Basically just creating a new variable that will act as a switch, so that when you hit two keys at once, the most recent will be stored by the a variable, and then acted on accordingly.