- Home /
Stuck at how to code something
Hi
I have a bunch of sphere colliders spead out on the xz plane. from the top i fire a ray at the position of the mouse to intersect hit any of the colliders. This works but there is a massive performance drop with only about 30 colliders in the scene. So
Coroutines, i looked at but dont return a type that is useful to me, i want the gameobject back
Threads also are not useful because they dont allow you to use the unity api, ie. the raycast
so i dont know how to go forward from here.
Im looking for a way of getting the gameobject back without the performance hit Any help would be greatly appreciated.
thanks
Unless any physics is being calculated on the object they will be put to sleep, sounds like it could be a coding issue could you post the code you are using to cast the ray and detect the collision? What other scripts/objects do you have in your scene?
Hi
Its supposed to be a cell game so there are cells and a nucleus at the moment, The cells have a script on it which controls position via various forces and birth/death animations, the nucleus has a script in it much the similar but also with spawning of the cells.
the code below is from the cell script but only the physics bit you asked for
public class Cell_Ctrl : $$anonymous$$onoBehaviour { void FixedUpdate() {
GameObject hitGo;
if (Input.Get$$anonymous$$ouseButton(0))
{
hitGo = getHitCell();
}
}
public GameObject getHitCell()
{
RaycastHit hitInfo;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hitInfo, 10);
//Debug.Log (hitInfo.transform.gameObject.name);
return hitInfo.transform.gameObject;
}
} Thanks for looking into this :D
aaawwwwwww man, now i feel really stupid, absolutely bang on the money
it was on a instanced object so it was creating a raycast each for however many cells there are in the scene
Thanks!
jogo13`s comment is correct but i cant promote it to the answer :D
Answer by jogo13 · Feb 10, 2013 at 01:53 AM
Sphere collision is one of the cheapest collisions possible, the performance drop is probably related to your implementation at the moment.
Do you have the raycast code built into every cell? That would mean if you have 30 cells, 30 raycasts are being created. You should only have one raycast check per mouse click. Just create a single gameobject with a new class that handles the `getHitCell(); function.
Your answer
Follow this Question
Related Questions
BoxCollider vs. RaycastAll 1 Answer
[c#] Hit a object and shoot it away with raycast? 2 Answers
OnCollisionEnter2D is not working 0 Answers
Raycast isn't working as expected 1 Answer
Getting a Raycast to Classify Objects as the Same Thing 0 Answers