Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.1K
Vistas
How do I disable diagonal movement in Unity 2D?

I would like to ask if there is a way to disable diagonal movement in Unity 2D. The thing is when I press 'W' + 'D'(move key) at the same time then the character starts to move diagonally. So instead of the character moving diagonal when combining button press, I want it to go completely straight instead if I press 'd' or any other key to move even if I'm still pressing the other button at the same time. So to say prioritize the function of the last button I pressed.

Here's a short video to explain my problem further .

https://youtu.be/aPZii5HfP4s

And here's the code for my character movement.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class keycontrol : MonoBehaviour
{

    public float moveSpeed = 0f;
    public Rigidbody2D rb2d;
    Vector2 movement;
    public Animator animator;


    // Update is called once per frame
    void Update()
    {
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");

        animator.SetFloat("walk_right", movement.x);
        animator.SetFloat("walk_left", -movement.x);
        animator.SetFloat("walk_down", -movement.y);
        animator.SetFloat("walk_up", movement.y);
    }

    void FixedUpdate()
    {
        rb2d.MovePosition(rb2d.position + movement * moveSpeed * Time.fixedDeltaTime);

        if(Input.GetKey("left shift"))
        {
            moveSpeed = 200 * Time.deltaTime;
            animator.speed = 1.5f;
        }
        else
        {
            moveSpeed = 110 * Time.deltaTime;
            animator.speed = 1f;
        }
    }
}

Thanks for helpThe game I'm trying to make

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

A simple way to do this is to prioritize one axis over the other and just wrap the other check in a condition.

movement.x = Input.GetAxisRaw("Horizontal");
if (movement.x != 0)
{
    movement.y = Input.GetAxisRaw("Vertical");
}

However, this can fail, because depending on your input, the axis might return values close to zero when using a controller. In this case, you could just get both values and check for the larger one.

movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");

if (Mathf.Abs(movement.x) > Mathf.Abs(movement.y))
{
    movement.y = 0;
}
else
{
    movement.x = 0;
}
over 4 years ago · Santiago Trujillo Denunciar

0

Sharing as an answer since I can't comment on this post, you should check out the new input system unity released recently: https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/QuickStartGuide.html

It gives better control over inputs such as event systems so you can lock the movement until the first key is released.

over 4 years ago · Santiago Trujillo Denunciar

0

You can handle the movement only in one of the axis handled each chance in the code, find draft below:

bool isXMoving;
bool isYMoving;
// Update is called once per frame
void Update()
{
    if (Input.GetAxisRaw("Horizontal") != 0f && !isYMoving) {
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = 0; 
        isXMoving = true;
        isYMoving = false;
    }
    if (Input.GetAxisRaw("Horizontal") != 0f && !isXMoving) {
        movement.y = Input.GetAxisRaw("Vertical");
        movement.x = 0; 
        isYMoving = true;
        isXMoving = false;
    }
    Debug.Log($"X: {movement.x} Y: {movement.y}");  //check
    
    animator.SetFloat("walk_right", movement.x);
    animator.SetFloat("walk_left", -movement.x);
    animator.SetFloat("walk_down", -movement.y);
    animator.SetFloat("walk_up", movement.y);
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda