I wanted to make a board game and the space platform u land on base what direction you can move. The space tiles will just move you in the direction until you hit another tile, so the platforms don’t have to be equally apart from each other. I tried many ways and failed every time trying to find methods to achieve this. I would like to know if I should just make a single script for my spaces or would I have to add different scripts to each one. This kind of movement system is what I am trying to achieve. https://www.youtube.com/watch?v=MtGAQyxW2as
Here is what I was starting with but then found out it won’t work like this.
using UnityEngine;
using System.Collections;
public class SpaceScript : MonoBehaviour {
public bool Right;
public bool Left;
public bool Up;
public bool Down;
public bool IsSpace;
public GameObject[] players;
public bool PlayerRight;
public bool PlayerLeft;
// Use this for initialization
void Start () {
players = GameObject.FindGameObjectsWithTag ("Player");
PlayerRight = false;
PlayerLeft = false;
}
// Update is called once per frame
void Update () {
Cantmove ();
Canmove ();
}
void OnTriggerEnter2D(Collider2D col){
if (col.gameObject.tag == "Player") {
if (Right != true) {
PlayerRight = false;
}
if (Left != true) {
PlayerLeft = false;
}
}
}
void OnTriggerStay2D(Collider2D col){
}
void OnTriggerExit2D(Collider2D col){
if (col.gameObject.tag == "Player") {
if (Right == true) {
PlayerRight = false;
}
if (Left == true) {
PlayerLeft = false;
}
}
}
void Cantmove(){
if (PlayerRight != true && (Input.GetKey(KeyCode.RightArrow))) {
Debug.Log ("Can't move Right buddy");
}
if (PlayerLeft != true && (Input.GetKey (KeyCode.LeftArrow))) {
Debug.Log ("Can't move Left buddy");
}
}
void Canmove(){
if (PlayerRight == true && (Input.GetKey (KeyCode.RightArrow))) {
// Get the Player from the array thats on a space where they can move right
players[0].transform.Translate(Vector3.right * Time.deltaTime);
}
if (PlayerLeft == true && (Input.GetKey (KeyCode.LeftArrow))) {
// Get the Player from the array thats on a space where they can move right
players [0].transform.Translate (Vector3.left * Time.deltaTime);
}
}
}