- Home /
The question is answered, right answer was accepted
Sphere cast thing help please!!!!
I want to do a sphere cast like seen on this video to make a grenade: http://www.youtube.com/watch?v=Y2C2Y3PiCM8 but I have no ideia how to do that thing, someone help please, javascript prefered btw
I'm making a game like bomberman but in 1st person, so I would know how to make reays to all characters and destructable boxes in the radio, and see if they are interfered by anything in the way, then I would apply functions so the player is health-=1 and box Destroy(gameObject)
Answer by robertbu · Mar 07, 2013 at 09:52 PM
They are using Physics.OverlapSphere() to detect colliders within a certain radius of where the grenade explodes, then they are using Rigidbody.AddExplosionForce() to toss the people around. There is a example script using both in the Rigidbody.AddExplosiveForce() reference page.
The other thing they are doing on the video is to Raycast from the explosion point to the bounding box of the player to see if the player is exposed to the blast or hidden behind a wall. I suggest you get the explosions working first, then go back and add the raycasting.
In order to do the Raycast to the corners, you first find all the "enemies". You can keep some sort of list, or you can tag them and find them like this:
GameObject[] argo = GameObject.FindObjectsWithTag("enemies");
Then you are going to cycle through the array and and do a RayCast between the position your are interested in. Here is a bit of untested code. It was modified from something that did work, so it should be close. It returns an integer. A value of 8 means that the corners of the bounding box of the object being tested are completely blocked from the point of the Raycast(). A value of 0 means none of the bounding box corners is blocked.
int Blocked(Vector3 v3Pos, Bounds bounds)
{
int iBlocked = 0;
Vector3 v3Corner = Vector3.zero;
Vector3 v3Center = bounds.center;
Vector3 v3Extents = bounds.extents;
RaycastHit hit;
v3Corner.Set(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top left corner
if (Physics.Raycast (v3Corner, v3Pos, out hit))
if (hit.collider.tag != "Player") iBlocked++;
v3Corner.Set(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top right corner
if (Physics.Raycast (v3Corner, v3Pos, out hit))
if (hit.collider.tag != "Player") iBlocked++;
v3Corner.Set(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom left corner
if (Physics.Raycast (v3Corner, v3Pos, out hit))
if (hit.collider.tag != "Player") iBlocked++;
v3Corner.Set(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom right corner
if (Physics.Raycast (v3Corner, v3Pos, out hit))
if (hit.collider.tag != "Player") iBlocked++;
v3Corner.Set(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top left corner
if (Physics.Raycast (v3Corner, v3Pos, out hit))
if (hit.collider.tag != "Player") iBlocked++;
v3Corner.Set(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top right corner
if (Physics.Raycast (v3Corner, v3Pos, out hit))
if (hit.collider.tag != "Player") iBlocked++;
v3Corner.Set(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom left corner
if (Physics.Raycast (v3Corner, v3Pos, out hit))
if (hit.collider.tag != "Player") iBlocked++;
v3Corner.Set(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom right corner
if (Physics.Raycast (v3Corner, v3Pos, out hit))
if (hit.collider.tag != "Player") iBlocked++;
return iBlocked;
}
For each one of the enemies you found you will call it with something like:
int iBlocked = Blocked(grenadePos, argo[i].renderer.bounds);
one $$anonymous$$ute i too had the similar kind of issue. that i need prevent the enemy with in the gun scope (it's a sphere) ( i mean i need to detect Colliders with in scope having certain radius ). or the enemy is with in the gun scope or not
how can i do it
thank you
Follow this Question
Related Questions
How can i make the ray cast don't go through the walls 1 Answer
How to assign a Variable with a Raycast 1 Answer
Raycasting in script suddenly stopped working 1 Answer
Talking Code 2 Answers