How can I create a 2D array that allows movement through only the lanes, but not the dark grey blocks as in the picture below ?
I tried using rigidbody and colliders and the result is plain terrible, as the sphere resembles a woodpecker drilling into a branch everytime there is a collision.
Thanks for taking a look into this.
The code that I’m using to move in a grid like fashion is given below:
using UnityEngine;
using System.Collections;
public class ControllerScript : MonoBehaviour {
public float speed = 1.0f;
private Vector3 endpos;
private bool moving = false;
void Start () {
endpos = transform.position;
}
void Update () {
if (moving && (transform.position == endpos))
moving = false;
if(!moving && Input.GetKey(KeyCode.W)){
moving = true;
endpos = transform.position + Vector3.up;
}
if(!moving && Input.GetKey(KeyCode.S)){
moving = true;
endpos = transform.position + Vector3.down;
}
if(!moving && Input.GetKey(KeyCode.A)){
moving = true;
endpos = transform.position + Vector3.left;
}
if(!moving && Input.GetKey(KeyCode.D)){
moving = true;
endpos = transform.position + Vector3.right;
}
transform.position = Vector3.MoveTowards(transform.position, endpos, Time.deltaTime * speed);
}
}