Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 oliver-jones · Jul 02, 2011 at 11:28 AM · raycastmultipleoffsetvectors

Multiple Raycasts

Hello,

I'm trying to get 5 raycast systems to work but I'm having difficultly trying to offset them.

What I'm trying to do is to have a raycast come from my Input.mousePosition, but also have 4 other raycasts around that, that are slightly offset. But I suck when it comes to vectors.

So basically, this is how I want my set up:

 var hit : RaycastHit;
 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 var layerMask = 1 << 14;
 
 Physics.Raycast (ray, hit, 200, layerMask); // center line
 
 Physics.Raycast (ray, hit.(z + 2) (x -2), 200, layerMask); // upper left
 
 Physics.Raycast (ray, hit.(z + 2) (x +2), 200, layerMask); // upper right
 
 Physics.Raycast (ray, hit.(z - 2) (x -2), 200, layerMask); // lower left
 
 Physics.Raycast (ray, hit.(z - 2) (x +2), 200, layerMask); // lower right


This obversely isn't going to work, but thats what I am trying to achieve. So each raycast that is being shot from the Input.mousePosition is to be offset by +2/-2. I also have a feeling that it has nothing to do with the 'hit', but is to do with the ray that I need to be adjusting?

I would appreciate any help with this.

------ UPDATE ------

Okay, so I'm making some progress. I have 5 raycasts that are shot with an offset from the camera, but towards the other end of the shot, they are angling towards each other. Here is a screen shot to help explain:

alt text

Here is my current code:

 var hit : RaycastHit;
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var layerMask = 1 << 14;
     var hitPoint : RaycastHit;
     
     if(debugClick){//chooseLocation){
         layerMask = ~layerMask;
         // center line
         Physics.Raycast (ray, hit, 200, layerMask);
         Debug.DrawLine (ray.origin, hit.point);
         
         // line 1
         var offset = Vector3(ray.origin.x + 2, ray.origin.y, ray.origin.z);
         var direction = transform.position;
         Physics.Raycast (offset, direction, hit, 200);
         Debug.DrawLine (offset, hit.point, Color.green);
         
         // line 2
         var offset2 = Vector3(ray.origin.x - 2, ray.origin.y, ray.origin.z);
         var direction2 = transform.position;
         Physics.Raycast (offset2, direction2, hit, 200);
         Debug.DrawLine (offset2, hit.point, Color.red);
         
         // line 3
         var offset3 = Vector3(ray.origin.x, ray.origin.y, ray.origin.z + 2);
         var direction3 = transform.position;
         Physics.Raycast (offset3, direction3, hit, 200);
         Debug.DrawLine (offset3, hit.point, Color.blue);
         
         // line 4
         var offset4 = Vector3(ray.origin.x, ray.origin.y, ray.origin.z - 2);
         var direction4 = transform.position;
         Physics.Raycast (offset4, direction4, hit, 200);
         Debug.DrawLine (offset4, hit.point, Color.yellow);123

123

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by oliver-jones · Jul 02, 2011 at 05:51 PM

I believe I have finally done it - I needed to also give each raycast a unique RaycastHit variable:

     var hit : RaycastHit;
     var hit1 : RaycastHit;
     var hit2 : RaycastHit;
     var hit3 : RaycastHit;
     var hit4 : RaycastHit;
     
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var layerMask = 1 << 14;
     var hitPoint : RaycastHit;
     
     if(debugClick){//chooseLocation){
         layerMask = ~layerMask;
         // center line
         var offsetCen = Vector3(ray.origin.x, ray.origin.y, ray.origin.z);
         var directionCen = transform.TransformDirection (Vector3.forward);
         Physics.Raycast (offsetCen, directionCen, hit, 200, layerMask);
         Debug.DrawLine (offsetCen, hit.point);
         
         // line 1
         var offset = Vector3(ray.origin.x + 2, ray.origin.y, ray.origin.z);
         var direction = transform.TransformDirection (Vector3.forward);
         Physics.Raycast (offset, direction, hit1, 200, layerMask);
         Debug.DrawLine (offset, hit1.point, Color.green);
         
         // line 2
         var offset2 = Vector3(ray.origin.x - 2, ray.origin.y, ray.origin.z);
         var direction2 = transform.TransformDirection (Vector3.forward);
         Physics.Raycast (offset2, direction2, hit2, 200, layerMask);
         Debug.DrawLine (offset2, hit2.point, Color.red);
         
         // line 3
         var offset3 = Vector3(ray.origin.x, ray.origin.y, ray.origin.z + 2);
         var direction3 = transform.TransformDirection (Vector3.forward);
         Physics.Raycast (offset3, direction3, hit3, 200, layerMask);
         Debug.DrawLine (offset3, hit3.point, Color.blue);
         
         // line 4
         var offset4 = Vector3(ray.origin.x, ray.origin.y, ray.origin.z - 2);
         var direction4 = transform.TransformDirection (Vector3.forward);
         Physics.Raycast (offset4, direction4, hit4, 200, layerMask);
         Debug.DrawLine (offset4, hit4.point, Color.yellow);

I'm not 100% if its working, but it certainly looks like it according to the debug lines.

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 Waz · Jul 02, 2011 at 11:38 AM

Change ray.direction, not the hit variable. Give it a try yourself, that way you'll learn how unscary vectors are. To change a vector:

 v += Vector3(1,-2,3);
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 oliver-jones · Jul 02, 2011 at 11:51 AM 0
Share

Okay, please look at my updated answer.

avatar image
0

Answer by AngryOldMan · Jul 02, 2011 at 12:10 PM

hello. I believe you may need to change these lines:

 var direction = transform.position;
 var direction2 = transform.position;
 var direction3 = transform.position;

as these are the other end of your line and they all point to the same location. I'm not 100% sure as I dont have any of my scripts to hand but that's what I would try first. Good luck

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I offset a raycast along the transform's local x/z axis? 1 Answer

Multiple linecasts question 1 Answer

Offset of texture with Raycast 1 Answer

How to get an offset of a RaycastHit? 0 Answers

How can I get back a hit? 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