Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.1K
Views
¿Cómo desactivo el movimiento diagonal en Unity 2D?

Me gustaría preguntar si hay una forma de desactivar el movimiento diagonal en Unity 2D. La cuestión es que cuando presiono 'W' + 'D' (tecla de movimiento) al mismo tiempo, el personaje comienza a moverse en diagonal. Entonces, en lugar de que el personaje se mueva en diagonal cuando se combinan los botones, quiero que vaya completamente recto si presiono 'd' o cualquier otra tecla para moverme, incluso si todavía estoy presionando el otro botón al mismo tiempo. Por así decir priorizar la función del último botón que presioné.

Aquí hay un video corto para explicar más mi problema.

https://youtu.be/aPZii5HfP4s

Y aquí está el código para el movimiento de mi personaje.

 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; } } }

Gracias por la ayuda El juego que estoy tratando de hacer

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Una forma sencilla de hacer esto es priorizar un eje sobre el otro y simplemente envolver el otro control en una condición.

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

Sin embargo, esto puede fallar, porque dependiendo de su entrada, el eje puede devolver valores cercanos a cero al usar un controlador. En este caso, podría obtener ambos valores y buscar el más grande.

 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 Report

0

Compartiendo como respuesta ya que no puedo comentar en esta publicación, debe consultar la nueva unidad del sistema de entrada lanzada recientemente: https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/QuickStartGuide. html

Brinda un mejor control sobre las entradas, como los sistemas de eventos, para que pueda bloquear el movimiento hasta que se suelte la primera tecla.

over 4 years ago · Santiago Trujillo Report

0

Puede manejar el movimiento solo en uno de los ejes manejados cada oportunidad en el código, busque el borrador a continuación:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!