• 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
1
Question by Benjaben · Jan 11, 2011 at 11:29 PM · raycast

Ray collision listener

I've been searching on the API, but haven't found anything yet. I'm looking for a way to do the following:

function OnRayCollision()
{
    //Do some stuff
}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Jessy · Jan 12, 2011 at 12:01 AM

Rays don't move at a speed. They also don't stick out from things like infinite lasers. If you want something like that, then Raycast every frame.

http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

http://unity3d.com/support/documentation/ScriptReference/Collider.Raycast.html

Comment
Add comment · 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
0

Answer by Statement · Jan 12, 2011 at 12:24 AM

See Physics.Raycast and Physics.RaycastAll.

A simple skeleton for a ray collider could be like this:

// RayCollider.cs using UnityEngine; using System.Collections;

public class RayCollider : MonoBehaviour { public LayerMask layers = -1; public float distance = float.PositiveInfinity; public bool blocking = true;

 const SendMessageOptions options = SendMessageOptions.DontRequireReceiver;
 const string callback = "OnRayCollision";

 void Update()
 {
     Ray ray = new Ray(transform.position, transform.forward);

     if (blocking)
     {
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit, distance, layers))
         {
             var info = new RayCollision(this, hit);
             SendMessage(callback, info, options);
             hit.collider.SendMessage(callback, info, options);
         }
     }
     else
     {
         var hits = Physics.RaycastAll(ray, distance, layers);
         foreach (var hit in hits)
         {
             var info = new RayCollision(this, hit);
             SendMessage(callback, info, options);
             hit.collider.SendMessage(callback, info, options);
         }
     }
 }

}

public struct RayCollision { public RayCollider ray; public RaycastHit hit;

 // For ease of use...
 public Collider collider
 {
     get { return hit.collider; }
 }

 public RayCollision(RayCollider ray, RaycastHit hit)
 {
     this.ray = ray;
     this.hit = hit;
 }

}

And an example script responding to the ray collision could be something like

// DeathByRay.cs using UnityEngine; using System.Collections;

public class DeathByRay : MonoBehaviour { void OnRayCollision(RayCollision rayCollision) { if (rayCollision.ray.CompareTag("Death Star")) Destroy(gameObject); } }

Or JS version:

// DeathByRay.js
function OnRayCollision(rayCollision : RayCollision)
{
    if (rayCollision.ray.CompareTag("Death Star"))
        Destroy(gameObject);
}

Both the RayCollider and the object having the hit collider will have OnRayCollision called. The structure passed contains the ray that cast the collision and the raycast hit information. Note that this doesn't mean it pushes things around. This is sort of a trigger, only raycasted.

Comment
Add comment · 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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Logic Required 1 Answer

Cannot cast from source to destination type 1 Answer

Attaching 2 objects using joints and raycasting 0 Answers

Simple AddForce Scirpt help 1 Answer

Using Trigonometry for Collision Detection 0 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