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 /
This question was closed Jul 22, 2013 at 03:57 PM by MadJohny for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by MadJohny · Mar 07, 2013 at 09:34 PM · javascriptraycastsphere

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

Comment
Add comment · Show 1
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 MadJohny · Mar 08, 2013 at 03:46 PM 0
Share

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)

1 Reply

  • Sort: 
avatar image
1
Best Answer

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.

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 robertbu · Mar 08, 2013 at 04:41 PM 0
Share

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);
avatar image Veerababu.g · Jan 27, 2016 at 01:08 PM 0
Share

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

Answers Answers and Comments

12 People are following this question.

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

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

How, from scratch, can I detect the angle of the ground underfoot a character not controlled by a character controller. 1 Answer

Talking Code 2 Answers


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