How to get what was the previous GameObject?
I'm working on a game where one of the gameplay elements involves moving around three circles to match some other colors. An example is the picture above. I have already implemented dragging the circles around but if I were to drag red over green, green will be under red, I want that green Game Object to be where the empty space is ( so basically switch spaces ). I have set up a way on how it knows what position to put this object but it doesn't know what object. That's my question, how do I detect what gameobject to switch into that empty space. This is the script attached to those circle Game Objects
using UnityEngine;
using System.Collections;
public class selectedCircle : MonoBehaviour {
int isSelected;
Vector3 screenPoint;
Vector3 offset;
Vector3 posotionForPrev;
Collider2D posLeft, posMid, posRight;
bool leftHas, midHas, rightHas;
void Start()
{
leftHas = true;
midHas = true;
rightHas = true;
PlayerPrefs.SetInt("isSelected", 0);
isSelected = PlayerPrefs.GetInt("isSelected", 0);
posLeft = GameObject.Find("posLeft").GetComponent<Collider2D>() ;
posMid = GameObject.Find("posMid").GetComponent<Collider2D>();
posRight = GameObject.Find("posRight").GetComponent<Collider2D>();
}
void Update()
{
checkFreeSpot();
}
void OnMouseDown()
{
screenPoint = Camera.main.WorldToScreenPoint(transform.position);
offset = transform.position - Camera.main.ScreenToWorldPoint(
new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = new Vector3(curPosition.x, transform.position.y, 0);
}
void OnMouseUp()
{
//these if statements used to lock in object to position
if (posLeft.bounds.Contains(gameObject.transform.position))
{
gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, new Vector3(-1.94f, -4.11f, 0), 5) ;
}
else
{
leftHas = false;
}
if (posMid.bounds.Contains(gameObject.transform.position))
{
gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, new Vector3(0.21f, -4.11f, 0), 5);
}
else
{
midHas = false;
}
if (posRight.bounds.Contains(gameObject.transform.position))
{
gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, new Vector3(2.22f, -4.11f, 0), 5);
}
else
{
rightHas = false;
}
}
void checkFreeSpot()
{
if (leftHas == false)
{
posotionForPrev = new Vector3(-1.94f, -4.11f, 0);
}
else if (midHas == false)
{
posotionForPrev = new Vector3(0.21f, -4.11f, 0);
}
else if (rightHas == false)
{
posotionForPrev = new Vector3(2.22f, -4.11f, 0);
}
}
}
Answer by GreenCore · Sep 14, 2015 at 07:47 PM
Hi!
Are you naming your game objects anything special? Or are you just going with some generic name for all the circles (e.g. circle1, circle2...).
The simplest way I can think of right now, in case you are not using the game object's name for anything else, is to name them after their position in the game world, like this:
void Start(){
gameObject.name = transform.position.ToString();
}
Now, when the user releases the mouse button, you need to do two things:
-Find the game object named after your new position and move it to your previous position.
-Rename both objects to their new positions.
void OnMouseUp() {
GameObject other = GameObject.Find(transform.position.ToString());
if(other != null){
other.transform.position = prevPosition;
other.name = prevPosition.ToString();
}
gameObject.name = transform.position.ToString();
}
Hope it helps. Good luck!
This seems to be slightly working, when I switch the objects they don't overlap but they don't switch into the other spot but into the center of the screen. I believe this is happening because my system for getting the position of where it should be is not working correctly.Any ideas on fixing this or a better system for getting the position needed?
How did you integrate the examples I wrote into your code? $$anonymous$$aybe try setting the value for prevPosition inside On$$anonymous$$ouseDown().
Note that prevPosition (in my example) is not supposed to be the same variable as posotionForPrev. Not sure what you are using that one for, but in your original post it seems like you are only assigning it a value inside CheckFreeSpot() but never actually using it.
Thanks for the answer, I got it to work using your suggestion to set prevPosition in On$$anonymous$$ouseDown()
Your answer
