- Home /
physics2d.raycastall help please
hello, i need to convert my raycast2d code to raycastall, and i am unsure as to how to do it. this is the code that i have attached to the four different balloons in the game:
var pop : GameObject;
var balloontype : GameObject;
function Update () {
if (Input.GetMouseButtonDown(0))
{
var hitm : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (hitm.collider != null && hitm.collider.gameObject == balloontype)
{
Instantiate(burst, hitm.point, Quaternion.identity);
Instantiate(pop, hitm.point, Quaternion.identity);
Destroy (hitm.collider.gameObject);
}
}
}
i detect which balloon was hit by the ray and then spawn a special pop which matches in color based on which balloon was hit. i want however that when multiple balloons overlap each other and are hit at the same time, they all pop, and a combo prefab is spawned once at the hit point. i was informed that i should use physics2d.raycastall as this is a 2d game and that i would receive an array (of RayCastHit2D) for all the colliders the ray hits.
from there, first check the length of the array is greater than 1, from there check what types of balloons were hit.
finally if the balloon type is an "eye" more than once inside the array, spawn the combo prefab at the first collision location.
^^ i am having difficulty coding this however and would greatly appreciate if someone could help me in converting this code to raycastall2d. thanks in advance!
Answer by mealone · May 27, 2014 at 05:32 PM
var balloontype : GameObject;
var pop : GameObject; var burst : GameObject;
var firstHit : Vector3 = new Vector3(); var myHitList : List. = new List.();
function Update () {
if (Input.GetMouseButtonDown(0)) { //clear the list and reset the hit vector myHitList.Clear(); firstHit = new Vector3();
//this one was a mighty head scratcher, make sure you have all your colliders as 2d colliders
var hits : RaycastHit2D[];
hits = Physics2D.GetRayIntersectionAll(Camera.main.ScreenPointToRay(Input.mousePosition),100);
for(i = 0; i < hits.Length ; i++)
{
Debug.Log("clicked : " + i);
//comparing the clone to the prefab reference wont work, i personally would use a class that holds types and use get
//component, but in this case we will just check the name.
if (hits != null &&
hits[i] != null &&
hits[i].collider != null &&
hits[i].collider.name.Contains(balloontype.name))
{
myHitList.Add(hits[i].collider.gameObject);
}
}
if(myHitList.Count == 1)
{
//if only one hit, then no combo and the "balloon" is destroyed
Destroy(myHitList[0]);
}
else if(myHitList.Count > 1 )
{
//if we have multiply hits then lets nuke them and get some pretty action happening
firstHit = hits[0].point;
ComboPop(myHitList, firstHit);
}
}
}
function ComboPop (list : List., point : Vector3) { //spawn combo at first collision Instantiate(burst, point, Quaternion.identity); Instantiate(pop, point, Quaternion.identity);
//clean up balloons
for(i = 0; i < list.Count ; i++)
{
Destroy (list[i]);
}
}
answer
Your answer
Follow this Question
Related Questions
Using different paricle emitters depending on the tag of the object raycast hits? 1 Answer
Raycast Destroy(hit.collider.gameObject); (Still need help) 1 Answer
Raycast 2D with origin inside the collider 1 Answer
Trouble assigning transforms of two raycast hits to two different objects. 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers