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 /
avatar image
0
Question by growling_egg · Aug 11, 2013 at 10:30 PM · raycastrayorigin

A little confused about scripting Raycasts

I've been working on a project for awhile involving alien ships attacking earth, and I'm currently trying to make it so that when the ships fire a laser at a building, the beam emits light onto the nearby surroundings. I'm using a script that measures the distance between the ship and the building and instantiates an appropriate number of point lights, and then places them an equal distance apart along the path of the beam.

Fairly straightforward, but I'm trying to write the script that instantiates the lights and places them along the beam's path. The beam itself is a line renderer, but the position of each light is calculated using a raycast and the Ray.GetPoint(distance) function, along with a for loop that steps along the ray placing the point lights, if that makes sense.

If anyone particularly familiar with Raycast mechanics wouldn't mind taking a look at the script I've got, the ray itself seems to want to start and stop at (0,0,0), or the world's origin, regardless of where the ship or the laser are. I have some variables that print out for debugging purposes - they all seem to show what I'd expect them to show, except for "Ray: ", which, like I said, just prints out "Origin: 0,0,0 Direction: 0,0,0".

I admit I'm not an expert at using rays, and I'm sure there are places in my script that can use some fine tuning - I'm not finished working out the details yet, I'm just trying to get the lights to show up where they're supposed to. All I need is a point in the right direction, please. Thanks!

EDIT: By the way, this isn't the full script, just the for loop that I mentioned.

 for (var q = 1; q <= noOfLightsNeeded; q++)
         {
             var pointAlongRay : Vector3;
             var ray : Ray;
             var hit : RaycastHit;
             var direction : Vector3 = laser.transform.position - target.transform.position;
             if (Physics.Raycast (ray.origin, direction, hit))
             {
                 print ("Origin: "+transform.position);
                 print ("Direction: "+direction);
                 print ("Hit: "+hit);
                 print ("Ray: "+ray);
                 Debug.DrawLine (ray.origin, hit.point);
                 pointAlongRay = ray.GetPoint(q);
                 
                 if (noOfLightsNeeded > currentNoOfLights)        // Adds lights along the beam as necessary
                     {
                         Instantiate (laserGlowLight, pointAlongRay, transform.rotation);
                         currentNoOfLights++;
                     }
                 
                 GameObject.Find("Laser Fire Glow(Clone)").transform.position = pointAlongRay;
             }
         }
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
1
Best Answer

Answer by Joyrider · Aug 11, 2013 at 10:36 PM

You created a ray variable, but you never defined it.

Set for your

ray.origin = ships.position

ray.direction =(building.position - ship.position)

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 growling_egg · Aug 11, 2013 at 11:27 PM 0
Share

I've been staring at this code too long... what a dumb thing for me to miss, haha. Thanks, I really appreciate you catching that.

avatar image growling_egg · Aug 11, 2013 at 11:28 PM 0
Share

Also, thanks for catching my answer in the wrong spot. Time to take a break from sitting at this screen, I think :)

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.

Follow this Question

Answers Answers and Comments

16 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 avatar image avatar image avatar image avatar image

Related Questions

camera.ScreenPointToRay always has same origin... 1 Answer

Ray.origin point moving from stationary object? 1 Answer

Raycast Ray ray origin problem 3 Answers

Changing position of a RayCast 1 Answer

How can I can I cast a ray from a gameobject? 1 Answer


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