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
1
Question by seashell · Jun 29, 2012 at 12:18 PM · triggerpositioncheckontriggerghost

making position checker - is area empty or not (with trigger)?

I'm doing a ghost object, that checks is there any place for new object. I'm having a function:


 function Ghost(parent : GameObject)
 {
 var checker : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
 checker.AddComponent ("Rigidbody");
 checker.collider.isTrigger = true;
 checker.transform.parent = parent.transform;
 checker.transform.localPosition  =  Vector3(0,0,Distance);

need something like on collider enter inside function || that will give a true or false value on return

Destroy (checker); }


I tried to add some script, with returning value on trigger enter... But unity returned me beginning value - its like unity don't have a time to check trigger - he just immediately returns a beginning value..

Is there any way to do it from a function like I have, without a new scripts.. All I need to add to checker gameObject, something like - if (checker.trigged) return false.

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 whydoidoit · Jun 30, 2012 at 10:08 AM 0
Share

You are going to need to get a list of the things it might hit and check whether their bounding boxes overlap with your new object. I'll try to post an answer when I'm somewhere near a computer rather than on a iPad :)

avatar image whydoidoit · Jun 30, 2012 at 10:10 AM 0
Share

Or you could do a ray cast from the centre of the "to be placed" object to each of the corners of it's area, which would work in many cases but would miss objects just below the centre point.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Jun 30, 2012 at 05:27 PM

You're almost there: just wait for the next FixedUpdate before destroying your ghost object - collisions are detected in the physics cycle. But you should disable its mesh renderer, or the ghost would appear for a while - very creepy! Another point: the Ghost function must be called in a coroutine, because you must wait its end to know the result.
Put all things together, your script could become something like this:

var somethingIsThere = false;

function WarnDaddy(){ // ghost calls this function if something detected somethingIsThere = true; }

function Ghost(parent : GameObject){ var checker : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube); checker.AddComponent(GhostScript); // you need a script in your ghost! checker.AddComponent(Rigidbody); checker.renderer.enabled = false; // you don't want the ghost appearing... checker.rigidbody.isKinematic = true; // avoid gravity or other physics effects checker.collider.isTrigger = true; // define rotation and position based on "parent" object... checker.transform.rotation = parent.transform.rotation; checker.transform.position = parent.transform.TransformPoint(Vector3(0,0,Distance)); // but child the ghost to THIS object to get the result: checker.transform.parent = transform; somethingIsThere = false; // clear flag yield WaitForFixedUpdate(); // wait collision detection Destroy(checker); // exorcise ghost }

// How to use: call Ghost via yield, then read the somethingIsThere variable:

yield Ghost(someObject); if (!somethingIsThere){ // there's nothing in that place } Ghost script (GhostScript.js):

function OnTriggerEnter(other: Collider){
  // if something is there, warn your daddy:
  transform.parent.SendMessage("WarnDaddy"); // call parent function WarnDaddy
}
Comment
Add comment · Show 1 · 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 seashell · Jul 03, 2012 at 11:41 AM 0
Share

thanks man! Its working perfectly! You can not imagine how much I'm happy! Thank you! Its exactly what I wanted!

avatar image
1

Answer by irrationalistic · Jun 29, 2012 at 06:13 PM

If you are ok with checking a sphere, look at the OverlapSphere function. This will return a list of all objects that intersect that given sphere. Then you just see if that is zero and place your object.

Comment
Add comment · Show 2 · 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 seashell · Jun 30, 2012 at 04:54 AM 0
Share

I need to check area with box, that can change sizes, sphere isn't that what I needed.

avatar image irrationalistic · Jun 30, 2012 at 03:12 PM 0
Share

You could use the overlap sphere (with a radius equal to the furthest possible point on your target bounding box) to get all potential objects in the area without having to ray cast all objects in your scene. Once you have your potential list, use your given box collider and check out the intersects values compared to each potential object. This will cut back on the number of potential physics tests while still allowing you to check the box.

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

7 People are following this question.

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

Related Questions

What's the best way to check if an area is empty? 7 Answers

Help with a script 2 Answers

linking particlesystem to cube,conecting particlesystem to cube 0 Answers

kinematic trigger with static collider-no trigger message? 0 Answers

OnTriggerEnter() problem 1 Answer


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