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 alienjesus · Feb 21, 2011 at 01:06 PM · gameobjectpositioncoordinatesfor-looplocation

Is gameObject present at coordinates (x,y,z)?

Hi, Bit of a beginner at unity and at Javascript coding in general. I'm making a 2d platformer game where objects can be controlled using a tracked fiducial marker. My main issue is that I struggle to find out what things are called in Unity in order to reference them in the code (the Unity resources are helpful, but sometimes a bit hard going).

Anyway, I've got some code which finds the x and y co-ordinates of a cube (and the rotation too) being controlled by the tracked fiducial, and now i want to use it's x & y co-ordinates in a loop to check if a gameobject exists at the co-ordinates ((myPosition), (myPosition2), z) on a loop where z = 0-50. After that i can work on making that secondary gameobject movable.

Heres my current code:

-function Update() { myPosition = transform.position.x; myPosition2 = transform.position.y; myRotation = transform.rotation.eulerAngles.z; Debug.Log(myPosition); Debug.Log(myPosition2); Debug.Log(myRotation); }

//function Update() { //For (z=0; z<50, z++){ //if (gameObject)

The code at the bottom is some early attempts at it before i realised i had no idea how to check if something was at a co-ordinate. I'm assuming my idea that i could just check if gameobject = true at co-ordinates (myPosition, myPosition2, z) is probably wrong, as I think that would end up as a vector3 instead of a location co-ordinate?

So my issues are 1. How do I check if a gameobject exists at a location? 2. can i use variables in a co-ordinate for a location to check? 3.Is a For loop the best way to check the z distance at my x,y co-ordinates, or am i doing this all wrong? 4. Oh and lastly, as a bonus question, once i find an object, i need to stop the loop. how do i do that?

Sorry for lots of questions, hopefully i'll get the hang of this quickly and bother you guys less :D

Comment
Add comment · Show 1
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 Bunny83 · Feb 21, 2011 at 02:36 PM 0
Share

Never compare exact positions. Positions are floats so a x-value of 50.3 is NOT equal to 50.30001 but that difference can't be seen. I think you want to use a BoxCollider ins$$anonymous$$d of a sphere and use what Simple said. Triggers are very common in all kinds of games. if you enter the area inside the box or sphere of your trigger OnTriggerEnter is called. There are a lot similar questions around here.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Simple · Feb 21, 2011 at 01:33 PM

In my opinion you should use OnTriggerEnter(col:Collider) and OnTriggerStay(col:Collider). http://unity3d.com/support/documentation/ScriptReference/Collider.OnTriggerEnter.html

1-3. So: Attach trigger sphere with radius 50(or what u need) to your marker position. Just create sphere, make it a trigger, set its coords equal to your marker coords. And it should be a child of your marker. Then write a script using OnTriggerEnter(col:Collider) and OnTriggerStay(col:Collider). Attach script to sphere. And when smth enters this trigger it will call function OnTriggerEnter which should send collision(better) or 'true'(not very good) to your main script.

4 To stop loop use next constructions:

for (k=0; k<50; k++)
{
//some code
//ex1t=true when obj find
//...
if (ex1t) break; //break exits loop; ex1t - any boolean var
}

OR

for (k=0; (k<50)&&(!ex1t); k++) //loop continues while ex1t is false and k<50
{
//ex1t=true when obj find
//...
}
Comment
Add comment · Show 4 · 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 alienjesus · Feb 21, 2011 at 01:56 PM 0
Share

If I'm understanding this right, the sphere will be at a fixed distance from my marker right? The problem with that method is that i want to be able to find objects at a variety of different depths (hence the loop). If multiple objects appear at the markers x,y co-ordinates I want to find only the closest one (hence wanting to leave the loop if one is found).

Is there a way of making this collider code work like that?

avatar image Simple · Feb 21, 2011 at 02:35 PM 0
Share

As I said trigger sphere center has same position as your marker. So sphere will be around your marker and cover all depths what u need(e.g. by changing trigger radius from 0 to 50)

avatar image alienjesus · Feb 21, 2011 at 02:47 PM 0
Share

Ah O$$anonymous$$, I guess i misunderstood, thanks. i'll report back soon on how it worked out.

avatar image alienjesus · Feb 21, 2011 at 03:34 PM 0
Share

Ok, this is weird. I'm using a box collider (because i needed z axis depth, but not X or y, and it's working at finding collision with only one of my gameobjects - the one WITHOUT a collider or a rigid body. all my others, which have rigidbodies and colliders, aren't activating the collision at all. I thought i was only meant to work with other colliders? I'm a bit confused.

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

No one has followed this question yet.

Related Questions

Finding position of a game object 1 Answer

Different child position when parent gameobject selected 0 Answers

Instantiating Multiple Game Objects to the Position of a Current GameObject 2 Answers

How to Position a GUI at the Top-Right of the Screen? 2 Answers

Using external GPS device unity application -1 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