Home > OS >  Disable Diagonal Movement in Unity 2D (but fluctuates between the two)
Disable Diagonal Movement in Unity 2D (but fluctuates between the two)

Time:06-05

Okay. So I understand how to disable diagonal movement, but what I want to do is switch between them for every step. So If I hold Left and Up, the character with go left one, then up, then left again. Kinda like a staircase movement. Here's what I've got so far:

private void Update()
{
    if (!isMoving)
    {
        input.x = Input.GetAxisRaw("Horizontal");
        input.y = Input.GetAxisRaw("Vertical");

        if (input.y != 0) input.x = 0;

        if (input != Vector2.zero)
        {
            var targetPos = transform.position;
            targetPos.x  = input.x;
            targetPos.y  = input.y;

            StartCoroutine(Move(targetPos));
        }
    }
}

IEnumerator Move(Vector3 targetPos)
{
    isMoving = true;

    while ((targetPos - transform.position).sqrMagnitude > Mathf.Epsilon)
    {
        transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
        yield return null;
    }
    transform.position = targetPos;

    isMoving = false;
}

}

CodePudding user response:

You can toggle Booleans by setting them to "!" and you can also set variables based on booleans, like so: boolean ? trueValue : falseValue;

int isOneIfTrue = (1 == 0) ? 1 : 0;

bool toggle = false; //true = moveUp | false = moveLeft
IEnumerator Move(Vector3 targetPos)
{
     isMoving = true;
     
     var targetPosX = targetPos;//<avoid the "new" keyword
     var targetPosY = targetPos;
     targetPosX.y = transform.position.y;//<reset the y for X 
     targetPosY.x = transform.position.x;// and x for Y
     targetPosY.y = Mathf.Abs(targetPosY.y);// ensure it's one direction by converting to positive
     targetPosX.x = -Mathf.Abs(targetPosX.x);// same thing but negative
         
     while ((targetPos - transform.position).sqrMagnitude > Mathf.Epsilon)
     {
         transform.position = Vector3.MoveTowards(transform.position, toggle ? targetPosY : targetPosX, moveSpeed * Time.deltaTime);
         yield return null;
     }
     transform.position = targetPos;

     isMoving = false;
}

then it's just a matter of flipping the toggle, I would start a separate Coroutine and the same while loop conditions just do:

yield return new WaitForSeconds(*secondsAsFloat*);
toggle = !toggle;

I need points to comment and upvote so if you found this answer at all helpful please accept it or do whatever you can to halp me get rep!

Goodluck!

CodePudding user response:

Replacing this code snippet will solve the problem completely.

public bool phase;
private void Update()
{
    if (!isMoving)
    {
        input.x = Input.GetAxisRaw("Horizontal");
        input.y = Input.GetAxisRaw("Vertical");
        
        if (input != Vector2.zero)
        {
            var targetPos = transform.position;

            if (phase) targetPos.x  = input.x;
            else targetPos.y  = input.y;

            StartCoroutine(Move(targetPos));

            phase = !phase;
        }
    }
}

enter image description here

  • Related