The blue circles should be only able to move to the squared next to them (diagonally aswell)
This is my code so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ClickToMove : MonoBehaviour
{
public static List moveableObjects = new List();
public float speed = 5f;
private Vector3 target;
private bool selected;
// Start is called before the first frame update
void Start()
{
moveableObjects.Add(this);
target = transform.position;
}
// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0) && selected)
{
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
target.z = transform.position.z;
}
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
}
private void OnMouseDown()
{
selected = true;
gameObject.GetComponent<SpriteRenderer>().color = Color.green;
foreach(ClickToMove obj in moveableObjects)
{
if(obj != this)
{
obj.selected = false;
obj.gameObject.GetComponent<SpriteRenderer>().color = Color.white;
}
}
}
}