so I am trying to make a turn based strategy game. currently I can select the character and yellow squares appear to where it can move. but when I click on a yellow square the character does not go to the square it goes somewhere else. if I use the same function to move the character to a specific spot then he goes to 15x0y. if I select him again and move to a spot 5 units are added to the x value. I don’t know why this is happening. I tried a different approach that broke it even further. basically the character is not going where it is supposed to.
Attached to the yellow squares:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovePlate : MonoBehaviour
{
GameObject controller;
public GameObject reference = null;
public float x;
public float y;
// Start is called before the first frame update
void Start()
{
x = this.transform.position.x;
y = this.transform.position.y;
}
// Update is called once per frame
void Update()
{
}
public void SetReference(GameObject obj)
{
reference = obj;
}
public GameObject GetReference()
{
return reference;
}
public void OnMouseOver()
{
if(Input.GetKey(KeyCode.Mouse0))
{
reference.transform.Translate(x,y,-1);
reference.GetComponent<units>().selected = false;
}
}
}
Attached to the character
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class units : MonoBehaviour
{
public GameObject Select;
public GameObject movePlate;
public GameObject tempselect;
GameObject mp1;
GameObject mp2;
GameObject mp3;
GameObject mp4;
GameObject mp5;
GameObject mp6;
GameObject mp7;
GameObject mp8;
public int movement;
public bool selected;
private bool mouseover;
// Start is called before the first frame update
void Start()
{
selected = false;
mouseover = false;
}
// Update is called once per frame
void Update()
{
if (selected)
{
for (int i = 1; i < movement + 1; i++)
{
Debug.Log("Bacon");
if (this.gameObject.transform.position.x + i < 20)
{
mp1 = Instantiate(movePlate, new Vector3(this.gameObject.transform.position.x + i, this.gameObject.transform.position.y, -2), Quaternion.identity);
mp1.GetComponent<MovePlate>().SetReference(this.gameObject);
mp1.GetComponent<MovePlate>().x = this.gameObject.transform.position.x + i;
mp1.GetComponent<MovePlate>().y = this.gameObject.transform.position.y;
}
if (this.gameObject.transform.position.x + i < 20 && this.gameObject.transform.position.y + i < 20)
{
mp2 = Instantiate(movePlate, new Vector3(this.gameObject.transform.position.x + i, this.gameObject.transform.position.y + i, -2), Quaternion.identity);
mp2.GetComponent<MovePlate>().SetReference(this.gameObject);
mp2.GetComponent<MovePlate>().x = this.gameObject.transform.position.x + i;
mp2.GetComponent<MovePlate>().y = this.gameObject.transform.position.y + i;
}
if (this.gameObject.transform.position.y + i < 20)
{
mp3 = Instantiate(movePlate, new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y + i, -2), Quaternion.identity);
mp3.GetComponent<MovePlate>().SetReference(this.gameObject);
mp3.GetComponent<MovePlate>().x = this.gameObject.transform.position.x;
mp3.GetComponent<MovePlate>().y = this.gameObject.transform.position.y + i;
}
if (this.gameObject.transform.position.x - i >= 0 && this.gameObject.transform.position.y + i < 20)
{
mp4 = Instantiate(movePlate, new Vector3(this.gameObject.transform.position.x - i, this.gameObject.transform.position.y + i, -2), Quaternion.identity);
mp4.GetComponent<MovePlate>().SetReference(this.gameObject);
mp4.GetComponent<MovePlate>().x = this.gameObject.transform.position.x - i;
mp4.GetComponent<MovePlate>().y = this.gameObject.transform.position.y + i;
}
if (this.gameObject.transform.position.x - i >= 0)
{
mp5 = Instantiate(movePlate, new Vector3(this.gameObject.transform.position.x - i, this.gameObject.transform.position.y, -2), Quaternion.identity);
mp5.GetComponent<MovePlate>().SetReference(this.gameObject);
mp5.GetComponent<MovePlate>().x = this.gameObject.transform.position.x - i;
mp5.GetComponent<MovePlate>().y = this.gameObject.transform.position.y;
}
if (this.gameObject.transform.position.x + 1 < 20 && this.gameObject.transform.position.y - i >= 0)
{
mp6 = Instantiate(movePlate, new Vector3(this.gameObject.transform.position.x + i, this.gameObject.transform.position.y - i, -2), Quaternion.identity);
mp6.GetComponent<MovePlate>().SetReference(this.gameObject);
mp6.GetComponent<MovePlate>().x = this.gameObject.transform.position.x + i;
mp6.GetComponent<MovePlate>().y = this.gameObject.transform.position.y - i;
}
if (this.gameObject.transform.position.y - i >= 0)
{
mp7 = Instantiate(movePlate, new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y - i, -2), Quaternion.identity);
mp7.GetComponent<MovePlate>().SetReference(this.gameObject);
mp7.GetComponent<MovePlate>().x = this.gameObject.transform.position.x;
mp7.GetComponent<MovePlate>().y = this.gameObject.transform.position.y - i;
}
if (this.gameObject.transform.position.x - i >= 0 && this.gameObject.transform.position.y - i >= 0)
{
mp8 = Instantiate(movePlate, new Vector3(this.gameObject.transform.position.x - i, this.gameObject.transform.position.y - i, -2), Quaternion.identity);
mp8.GetComponent<MovePlate>().SetReference(this.gameObject);
mp8.GetComponent<MovePlate>().x = this.gameObject.transform.position.x - i;
mp8.GetComponent<MovePlate>().y = this.gameObject.transform.position.y - i;
}
}
}
if(selected && Input.GetKey(KeyCode.Mouse1))
{
selected = false;
}
if(!selected)
{
DestroyMovePlates();
}
}
void OnMouseOver()
{
if (Input.GetKey(KeyCode.Mouse0)&&!selected)
{
tempselect = Instantiate(Select, new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y, -3), Quaternion.identity);
selected = true;
}
}
public void DestroyMovePlates()
{
//Destroy old MovePlates
GameObject[] movePlates = GameObject.FindGameObjectsWithTag("MovePlate");
for (int i = 0; i < movePlates.Length; i++)
{
Destroy(movePlates*); //Be careful with this function "Destroy" it is asynchronous*
}
}
}