Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by manu_1980_ · Mar 18, 2013 at 08:12 PM · c#positionvector3swap

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;        
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

position.Set vs. position = new Vector3() 0 Answers

Script to make Camera follow the player C# 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges