• 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 /
avatar image
0
Question by Doneyes · Mar 31, 2013 at 07:25 PM · raycastdistanceradiusgrenade

How to make this script find distance inside radius?

In this script I need to find the distance between the gameObject and the collider it's hitting if it's inside the radius.

 #pragma strict
 
 var wake : boolean;
 
 var power : float = 1500;
 var damage : int = 1;
 var radius : float = 5;
 
 var explodeSound : AudioClip;
 
 function Start () {
     yield WaitForSeconds (5);
     wake = true;
 }
 
 function Update () {
     if(wake == true) {
         Explode();
         wake = false;
         Destroy(gameObject);
     }
 }
 
 function Explode() {
  
     var colliders : Collider[] = Physics.OverlapSphere (transform.position, radius);
         for (var collider : Collider in colliders) {
             //if (collider.tag == "brick" || collider.tag == "player") {
                 var hit : RaycastHit;
                 if (Physics.Linecast(transform.position, collider.transform.position, hit)) {
                     if (hit.collider == collider) {
                         if (hit.rigidbody)
                             hit.rigidbody.AddExplosionForce(power, transform.position, radius, 3.0);
                 }
             }
         }
     //}
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by whydoidoit · Mar 31, 2013 at 07:43 PM

It's in hit.distance if the linecast hits the object. Note that if you are already inside the object then this may well fail. If you are inside then you have a couple of choices:

The easy one:

Get the vector from the current object to the position of the collider - start your line cast "back" away from the current object's position in that direction (the opposite direction to the ray cast).

This doesn't work well for concave objects or mesh colliders in all circumstances.

The hard one:

Calculate all of the contact points yourself by intersecting the triangles of the other collider which fall within the bounding box of your object.

If you can use OnCollisionEnter you get those contact points calculated for you.

Comment
Add comment · Show 1 · 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 whydoidoit · Mar 31, 2013 at 07:44 PM 0
Share

BTW you should really use Collider.RayCast in this instance as you already know the collider you want to hit - it's faster and techniques like moving the start of the ray away from the other object will not have any problems hitting something else by mistake.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

11 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

Related Questions

Trying to cast a ray towards an imaginary wall and get a "hit" / "end" point 2 Answers

Math - calculate position in world space from ray on infinite plane 2 Answers

Picking up rigidbody objects 2 Answers

Grenade Damage in Radius 2 Answers

Distance Variable Won't Change. What's Wrong With My Script? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges