Script not responding to “vertical” inputs.

Updated on June 15, 2017 in [A] 2D
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on June 15, 2017

Hi all,

 

 

 

After an extremely long hiatus I’m back into the whole coding thing, I’m currently fiddling about with a simple 8bit grid based rogue-like concept I have. (not an original idea by any means, but I really love the genre and think it’ll be good practice).

 

 

 

I am pretty much illiterate when it comes to unity, I’m able to construct basic top-down movement but movement on a grid was slightly beyond my prowess.

 

 

 

I found this script through my various searches for help It’s looking pretty good, I’m able to adjust grid sizes and movement is smooth. The only issue is that vertical inputs (W/S/up/down) produce no results, horizontal looks just fine. This may be a nightmare for people to look over, but if someone fancies having a quick scan to spot any major reasons as to why this may be it would be greatly appreciated!

 

 

 

 
 using System.Collections;
using UnityEngine;
class GridMove : MonoBehaviour {
    private float moveSpeed = 3f;
    private float gridSize = 1f;
    private enum Orientation {
        Horizontal,
        Vertical
    };
    private Orientation gridOrientation = Orientation.Horizontal;
    private bool allowDiagonals = false;
    private bool correctDiagonalSpeed = true;
    private Vector2 input;
    private bool isMoving = false;
    private Vector3 startPosition;
    private Vector3 endPosition;
    private float t;
    private float factor;
    public void Update() {
        if (!isMoving) {
            input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
            if (!allowDiagonals) {
                if (Mathf.Abs(input.x) > Mathf.Abs(input.y)) {
                    input.y = 0;
                } else {
                    input.x = 0;
                }
            }
            if (input != Vector2.zero) {
                StartCoroutine(move(transform));
            }
        }
    }
    public IEnumerator move(Transform transform) {
        isMoving = true;
        startPosition = transform.position;
        t = 0;
        if(gridOrientation == Orientation.Horizontal) {
            endPosition = new Vector3(startPosition.x + System.Math.Sign(input.x) * gridSize,
                startPosition.y, startPosition.z + System.Math.Sign(input.y) * gridSize);
        } else {
            endPosition = new Vector3(startPosition.x + System.Math.Sign(input.x) * gridSize,
                startPosition.y + System.Math.Sign(input.y) * gridSize, startPosition.z);
        }
        if(allowDiagonals && correctDiagonalSpeed && input.x != 0 && input.y != 0) {
            factor = 0.7071f;
        } else {
            factor = 1f;
        }
        while (t < 1f) {
            t += Time.deltaTime * (moveSpeed/gridSize) * factor;
            transform.position = Vector3.Lerp(startPosition, endPosition, t);
            yield return null;
        }
        isMoving = false;
        yield return 0;
    }
}

  • Liked by
Reply