• 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 Xeong-Hu · Mar 02, 2014 at 06:32 PM · positioning

How can i Compare a GameObjects Position?

Hello Awesome Unity PPL! I'm That same dude who asked about the Question on how to Make your Game Object detect a collision, So My game can tell when that designated spot is Occupied.

Well since that failed horribly i thought of another method of doing this.

I want my my Game to detect if my Spot is Occupied by comparing The card's X Position.

So if Card's X position == to Unity game X Position then Spot 1 is Occupied. But now when i think about it. That if statement doesn't sound right or possible..

So i'll just say. If Card's X position == to Spot 1's X position then Spot 1 is Occupied.

I'd also like to know if i can put it like this. If Card's X position is between ,let's say 1 through 5 X position, Then Spot 1 is Occupied.

Here's my Existing Code. (Although it doesn't work very well)

 function Update(){
 
 if (this.transform.position.x == transform.position.x 3.535507);{
         Debug.Log("You're on target");
     }
 }
 


Please Help me if you can, I'd really appreciate it.

Comment
Add comment · Show 2
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
avatar image Nanobrain · Mar 02, 2014 at 06:38 PM 1
Share

No, no. Go BAC$$anonymous$$ to collision detection. You're just going to make things hard on yourself otherwise. Give me the link to your other question so I can see what problems you were having.

avatar image Xeong-Hu · Mar 03, 2014 at 07:02 AM 0
Share

Don't $$anonymous$$d i do bro.

2nd Question

http://answers.unity3d.com/questions/649485/xeong-needs-help-how-to-cause-an-effect-when-an-ob.html

1st Question

http://answers.unity3d.com/questions/652067/how-can-i-get-my-game-to-detect-a-collision.html

I really hope you can solve this. It really would make things easier.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by FreeTimeDev · Mar 02, 2014 at 07:31 PM

Read this: [http://docs.unity3d.com/Documentation/ScriptReference/Mathf.Approximately.html][1]

Also note, that while this answers your question as to why you're having problems, you really shouldn't use this method. [1]: http://docs.unity3d.com/Documentation/ScriptReference/Mathf.Approximately.html

Comment
Add comment · Show 3 · 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
avatar image Xeong-Hu · Mar 03, 2014 at 09:14 AM 0
Share
 var Card = this.transform.position.x;
 
 
 if ($$anonymous$$athf.Approximately (Card,3.535507)){
 Debug.Log("Hello");
 }
 else{
 Debug.Log("Oops");
 }


Sigh Here i go again.. i tred this code out and my debug.log acts like it doesn't even exist... The else statement doesn't print out anything.. it's the same crap that happened to my Collision effect.

avatar image Xeong-Hu · Mar 03, 2014 at 09:47 AM 0
Share

Ugh. nvm.. I just forgot to add my Position code to my Object.

Guess i was so pissed i couldn't concentrate.

It's printing out oops but i still need to get it to hit spots X position.

avatar image Kiloblargh · Mar 03, 2014 at 09:52 AM 0
Share

Also, I can see you love capitals, but you should always name variables with lowercase letters; never $$anonymous$$d why, just do it. "Card" would be a good name for a class. "cardPosX" would be a better name for a numerical value.

avatar image
1

Answer by wibble82 · Mar 03, 2014 at 09:56 AM

Hi there

The problem you describe is a fairly common one - asking 'can I place an object at this location safely'. It can be tricky to get right, as you can't use collision detection because you need to know if the space is free before you get there. However it strikes me that in your particular game, maybe you're approaching it the wrong way.

Presumably in your game you have a set of Card objects, and a set of Slot objects. A slot can either contain a card, or not contain a card. There is also presumably a scenario in your game in which the player puts a card in a slot.

Why not just store in the slot a GameObject member called 'cardInThisSlot'. When the player puts a card in the slot you set that member to point to the card object.

That way a slot always knows for a fact whether its in use, and you don't need any position comparing at all. You simple say:

if(myslot.cardInThisSlot != null) you can't put anything here!

Obviously if I've presumed wrong and you want to put your cards anywhere in the world then that's garbage, but thought I'd mention it just in case! If so, to test if a spot is occupied by any other object I would use 'Physics.OverlapSphere' to get a list of all objects within a given distance of your requested point. Then look at the bounds of each collider to precisly ask if any colliders overlap that point.

-Chris

Comment
Add comment · Show 9 · 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
avatar image Xeong-Hu · Mar 04, 2014 at 02:27 PM 0
Share

Alright cool I like what you're saying but here's the part that I think where my main problem is.

When you said "When the player puts a card in the slot you set that member to point to the card object."

So like. How does the game know exactly that the player put a card in that slot? With out any detection? Do I just make an option choice or sumthing saying. If $$anonymous$$ey press 1 $$anonymous$$ove to Spot1?

That's why I wanted to make a collision effect that tells which card is colliding with the gameobject named Spot1. But that doesn't want to work with me so I tried doing X position comparisons ins$$anonymous$$d.

avatar image wibble82 · Mar 04, 2014 at 04:42 PM 0
Share

I think I see. Could you elaborate a little on exactly what the use case is. Are you having the user drag a card into a slot? Or are they clicking on a slot to put a card there?

avatar image Xeong-Hu · Mar 04, 2014 at 06:18 PM 0
Share

Thanks for replying. But I want them to just click summon and it'll just automatically go to a spot that isn't already occupied. I know it sounds complexed. I mean I could do it like. If player clicks on Spot 1 then Card will $$anonymous$$oveTowards Spot1. But if Spot1 is Occupied. Summon = false.

I got an idea. I'll show you what card game I'm trying to replicate the summoning function from.

https://www.youtube.com/watch?v=fEbH-0Ey4p$$anonymous$$

It's shown around at 3:00

Srry about not giving a better example.

avatar image wibble82 · Mar 04, 2014 at 06:58 PM 0
Share

No need to apologise - problems are always hard to describe until you've solved them!

Sounds like the first suggestion I made should work. Lets say you have 2 scripts:

Slot: represents a slot in a card can go into. You place a load of game objects in your world with this script attached, where you want the cards to end up. Slot has a member of type GameObject, which is called CurrentCard.

Card: represents a card that has been placed in the game world.

So if you have a function along the lines of:

     bool PlaceCardInSlot(GameObject my_card)
     {
         //get a list of all the slots in the world
         Slot[] slots = FindObjectsOfType<Slot>();
 
         //iterate over them, to find the first empty one
         foreach(Slot s in slots)
         {
             //check if this slot is empty
             if(s.CurrentCard == null)
             {
                 //it is! set the current card and return true for success
                 s.CurrentCard = my_card;
 
                 //just to be fancy, position the card here as well
                 my_card.transform.position = s.transform.position;
 
                 return true;
             }
         }
 
         //no empty slots so return false
         return false;
     }

That could be tweaked in various ways to suit your game. $$anonymous$$aybe you have an 'ideal slot' that the user originally tried to choose so you check that first. $$anonymous$$aybe there's special properties of a slot that forbid certain cards from going in it? Whatever you need, that's at the core - search the slots in the world and pick one that is empty. If you need to remove a card from a slot, you just delete it and set the slot's CurrentCard to be null.

avatar image Xeong-Hu · Mar 04, 2014 at 07:12 PM 0
Share

Thanks A Lot dude! I'll try out this code as soon as I get through being busy with a few things. It seems like a legit code I'd like to give it a try.

Can I like. Add you on Skype or anything? You seem like a smart person with Unity3d and it's hard for me a little cause I'm some what new to this.

Show more comments
avatar image
0

Answer by Kiloblargh · Mar 03, 2014 at 09:49 AM

Scale the cards so that each "key" position lands on an integer grid line.

Then use Mathf.Round (transform.position.x);

There are better ways to do what you are trying to do, but this will work.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to import the object from server to unity 2 Answers

C# sub Values/Arrays for a position Array 0 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Material doesn't have a color property '_Color' 4 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