- Home /
object translate issue
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[i]); //Be careful with this function "Destroy" it is asynchronous
}
}
}
On first glance i cannot see any issue with your movement code but to make this easier please please simplify your code.
this:
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;
}
can be written as this:
if (transform.position.x + i < 20)
{
Instantiate(movePlate, new Vector3(transform.position.x + i, transform.position.y, -2) , Quaternion.identity).GetComponent<MovePlate>().SetReference(this.gameObject);
}
If you should ever do more with the move plates on instantiation you can ofc always cache that locally again but there is no need for all those member variables mp1-8
.
Most imprtantly though: From what i can see you instantiate the movementplates every frame since you never reset selected
. Those plates must be piling up pretty high in your scene... Make sure that you get rid of this and only instantiate them once. Best to do that when you set selected = true
. This way you know that this only happens once on left-click.
Answer by unity_pFb9mvTB5gzvDA · Sep 29, 2021 at 09:52 AM
I fixed it!!!! In the moveplate code I changed "reference.transform.Translate(x,y,-1);" to "reference.transform.SetPositionAndRotation(new Vector3(x, y, -1.0f), Quaternion.identity);"
Your answer
Follow this Question
Related Questions
Limit & Scaling Translation Rate. 0 Answers
Move to a location while rotating. 0 Answers
Moving an object towards a clicked object 1 Answer
How to smooth out movement in a 3D space? 1 Answer
transform.Translate not working after exporting to Android 2 Answers