- Home /
compare current Vector3.position with stored initial Vector.3 position in c#
Hello guys,
I´m working on click and swap puzzle game (similar to click and slide picture game). I have trouble to get feedback when the puzzle is solved (the picture is completed).
I store the initial position in Start() and then mix up the tiles (also in Start().. maybe that´s the problem?!)
What I want to achive is.. If the tiles are in the initial position, then print Congratulations. But for some reason, it doesn´t work and I don´t know whats wrong with my code.
using UnityEngine;
using System;
public class picSwap : MonoBehaviour
{
// Vector3 variables for storing the inital positions for the cells (completed picture)
public Vector3 upperL;
public Vector3 upperM;
public Vector3 upperR;
public Vector3 middleL;
public Vector3 middleM;
public Vector3 middleR;
public Vector3 lowerL;
public Vector3 lowerM;
public Vector3 lowerR;
public GameObject[] cells;
public GameObject[] selected;
public int index = 0;
void Start()
{
// initial position for the cells (completed picture)
upperL = cells[0].transform.position;
upperM = cells[1].transform.position;
upperR = cells[2].transform.position;
middleL = cells[3].transform.position;
middleM = cells[4].transform.position;
middleR = cells[5].transform.position;
lowerL = cells[6].transform.position;
lowerM = cells[7].transform.position;
lowerR = cells[8].transform.position;
// mixing up the cells (create puzzle)
cells[0].transform.position = new Vector3(0,1,0);
cells[1].transform.position = new Vector3(-1,1,0);
}
void Update ()
{
// when cell is clicked ...
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 500))
{
//store the first cell in variable "selected"
selected[index] = hit.transform.gameObject;
// and highlight the object
selected[index].renderer.material.color = Color.white;
// index++ for storing the second cell in variable "selected"
index++;
}
}
// if cells are in initial position, print Congratulations
if (cells[0].transform.position == upperL && cells[1].transform.position == upperM)
{
print ("Congratulations!");
}
}
void OnGUI()
{
// button to swap the first and second selected cell, which are stored in variable "selected"
if (GUI.Button (new Rect (0,430,50,50),"SWAP"))
{
Vector3 temp = selected[0].transform.position;
selected[0].transform.position = selected[1].transform.position;
selected[1].transform.position = temp;
// set index back to 0 after swap
index= 0;
}
}
}
Answer by liamcary · Mar 19, 2013 at 06:13 AM
Have you tried printing all of the values in the relevant vector3s? Vector3 stores 3 floats, so you're prone to floating point error. I came across this in a game project recently and just worked around it by writing a function to see if it's approximately correct, eg:
public bool CompareApproximate(Vector3 a, Vector3 b)
{
if(!Mathf.Approximately(a.x, b.x))
return false;
if(!Mathf.Approximately(a.y, b.y))
return false;
if(!Mathf.Approximately(a.z, b.z))
return false;
return true;
}
By checking if positions are NOT approximately the same, you can exit the function early, saving processing time if you perform the calculation a lot.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Trouble setting up a vector 3. 1 Answer
How to Check if any object is at an specific Vector3? 1 Answer