• 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 Dec 11, 2012 at 06:02 PM by greatestprez for the following reason:

Question was answered

avatar image
Question by greatestprez · Nov 22, 2012 at 07:31 AM · raycastmeshshadowrayvertex

How to send a Ray through a vertex?

  1. I am trying to create a custom shadow solution for casting a shadow on a flat plane in unity indie. For me to do t$$anonymous$$s I need to cast a ray through each vertex of the model that is casting the shadow. T$$anonymous$$s is what I have so far, but it does not work because the rays do not pass through the vertices.


    public class Caster : MonoBehaviour {

      public GameObject meshTrans; 
         
         public Transform ground; 
         
         private float y1, y2; 
         
         Vector3[] vertices; 
         
         // Use t$$anonymous$$s for initialization
         void Start () {
             if (meshTrans != null) vertices = meshTrans.GetComponent<MeshFilter>().mesh.vertices; 
             
         }
         
         // Update is called once per frame
         void Update () {
             
             for (int i = 0; i < vertices.Length; i++) {
                 y1 = transform.position.y - vertices[0].y; 
                 y2 = vertices[0].y - ground.position.y; 
                 Vector3 v = vertices[0] - transform.position; 
                 v = v * ((y1+y2) / y1) ;
                 
                 Debug.DrawRay(transform.position, transform.TransformPoint(v) - transform.position, Color.red);  
             }
       
         }
     }
     
    
Comment
whydoidoit

People who like this

1 Show 5
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 Tim-Michels · Nov 22, 2012 at 07:52 AM 0
Share

Raycasting only detects colliders, so you could generate Sphere colliders on each vertex of your model, and then your Raycast should detect those colliders.

avatar image greatestprez · Nov 22, 2012 at 09:39 PM 0
Share

if I had a high-poly object, would that slow performance?

avatar image whydoidoit · Nov 22, 2012 at 09:43 PM 0
Share

The vertices are in model space - so you'd need to convert them to world space using transform.localToWorldMatrix

Presumably you want to project the ray onto some plane? So given that you have the equation of the plane and the two coordinates in world space it should be pretty straightforward - but is that what you are trying to do?

avatar image greatestprez · Nov 23, 2012 at 12:50 AM 0
Share

Thanks whydoidot! I got it working now. I used Transform.transformPoint() instead of Transform.localToWorldMatrix(). I am trying to cast a shadow on a flat plane by passing rays from a light source through each vertex of the model casting the shadow and then generating a mesh (for the actual rendering of the shadow) based on the place where each ray hits the plane (ground)

avatar image whydoidoit · Nov 23, 2012 at 08:45 AM 1
Share

Yeah - transformPoint is a much better idea - don't know what I was thinking last night!

0 Replies

  • Sort: 

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

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 do I check if the mouse is over a VISIBLE vertex? 1 Answer

Creating a working meshcollider in code 3 Answers

Procedurally generated, vertex colored mesh self-shadowing problem 0 Answers

Raycast on mesh deformed by shader 0 Answers

Unity disconnected/flying shadow (peter panning) 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