- Home /
Can I grow a sphere collider and collect object vectors and object information from a scene and add this data to a list?
Can I grow a sphere collider and collect object vectors and object information from a scene and add this data to a list?
My idea is that I would like to grow a sphere in size over a short amount of time and each time the sphere collides with an object I want to record the vector of that object so that I can save these locations to a list. So I have a sphere collider trigger that grows in my scene now how do I get the vectors of all the objects it collides with as it grows?
Answer by aldonaletto · Jun 07, 2013 at 01:25 PM
You could use Physics.OverlapSphere, but this function isn't very precise because only the bounding boxes are taken into account - a long object rotated 45 degrees about Y may be added to the list even if its body actually isn't even being touched. A more precise solution is to instantiate the trigger object, force collision calculation, get the objects with OnTriggerEnter and then delete the trigger - an answer on this subject can be found here.
Answer by robertbu · Jun 07, 2013 at 01:54 PM
Whenever I have questions like this one I just test it. Surprisingly enough it does work with triggers (and I did not expect it to). I created a sphere, made the collider a trigger, added a rigidbody, turned off gravity, and added this script:
#pragma strict
function Update() {
var sp : SphereCollider = GetComponent(SphereCollider);
sp.radius += 0.01;
}
function OnTriggerEnter() {
Debug.Log("Triggered Something");
}
Note it also worked if the growth was done in a 'for' loop in start as well. It did not work if the rigidbody has isKenematic enabled.
I'm not sure what information you are trying to collect with this code. From an efficiency point of view, you'd might be better off to use Physics.OverlapSphere() to collect all the possible game objects, cast rays (Collider.Raycast()) to each object to find the front distance to the collider, and then sort the result.
I'm trying to make a save script and I need to collect all the objects of the scene and I'm thinking I need to first know the transform locations of all the objects in the scene to do it. I like the answers so far but I'm still trying to write a solution to this problem.
If you want to find all the active game objects, you can just do:
var gos : GameObject[] = FindObjectsOfType(GameObject) as GameObject[];
Note in most games, you will not want all the game objects, just game objects of a certain kind. The typical way to solve that problem is to tag the game objects and use GameObject.FindGameObjectsWithTag().
Answer by GodClaw · Jun 07, 2013 at 02:26 PM
Add all of your game objects to a list and use a foreach loop.
how do i know how many prefabs have been instantiated? Doesn't each prefab have a unique name when it is instantiated during gameplay?
I'm on my phone but something like this..
GameObject temp = instantiate(blah blah);
objectlist.add(temp);
foreach(GameObject gobj in objectlist)
{
//use gobj to find all the values
}
Your answer
Follow this Question
Related Questions
Saving Whole Scene ? 0 Answers
PlayerPrefs HighScore problems, it doesn't work. 4 Answers
PlayerPrefs.GetInt won't load when awake. 0 Answers
Getting any type in Unity Inspector 1 Answer
Save lightmap in prefab 3 Answers