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 13dnizinski · Jul 22, 2014 at 11:29 PM · raycastainavigation

How can I find if a raycast has passed through two points?

alt textI am making a raycast-based navigation system (because I'm too poor for pro :/) and I need it to be dynamic (respond to new objects) and I need to see if a raycast has gone between two points (and passing through) so I can tell if I have already had my character go that way so he can choose another way to go. Is this possible? Thanks in advance - you people are the best!

If you look at my image, I would like to detect if a line from C to D passes between points A and B at the intersection where the red X is.

example.png (1.5 kB)
Comment
Add comment · Show 2
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 markedagain · Jul 23, 2014 at 12:22 AM 0
Share

having a hard time figuring out your request. are you trying to get 2 raycasts to hit each other?

avatar image Razacx · Jul 23, 2014 at 12:34 AM 0
Share

This can be calculated. But does this system work in two or three dimensions? ($$anonymous$$akes a lot of difference :p)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by smallbit · Jul 23, 2014 at 01:17 AM

You could create a mesh collider (plane) on a run between A and B with some height (must be relatively high depending on flatness of your level).

  1. Raycast C-D

  2. On Found D create mesh collider

  3. Raycast Again same direction

  4. If hit your mesh set some flag or continue with your code.

Just a theory never tried it.

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 WilliamLeu · Jul 23, 2014 at 01:28 AM

You're probably best off looking for a 3D line to line collision test off Google and porting it to Unity (unless you need a 2D test, which is faster to calculate).

See if this works for you (ported from StackOverflow)

 using UnityEngine;
 using System.Collections;
 
 public class Test : MonoBehaviour {
 
     
     bool LineLineIntersection3D(
         Vector3 LineA_Pt1,
         Vector3 LineA_Pt2,
         Vector3 LineB_Pt1,
         Vector3 LineB_Pt2, out Vector3 IntersectionPt)
     {
         Vector3 da = LineA_Pt2 - LineA_Pt1; 
         Vector3 db = LineB_Pt2 - LineB_Pt1;
         Vector3 dc = LineB_Pt1 - LineA_Pt1;
         
         if (Mathf.Abs( Vector3.Dot(dc, Vector3.Cross(da,db) ) ) > Mathf.Epsilon ) // lines are not coplanar
         {
             IntersectionPt = Vector3.zero;
             return false;
         }
         
         float s = Vector3.Dot( Vector3.Cross(dc,db),Vector3.Cross(da,db)) / Vector3.Cross(da,db).sqrMagnitude;
         if (s >= 0.0f && s <= 1.0f)
         {
             IntersectionPt = LineA_Pt1 + da * s;
             return true;
         }
         IntersectionPt = Vector3.zero;
         return false;
     }
 
     // USAGE EXAMPLE
     void Awake()
     {
         Vector3 v3Intersection;
         if( LineLineIntersection3D( 
                     new Vector3( -1.0f, 0.0f, 0.0f ),      // Point A
                 new Vector3( 5.0f, 0.0f, 0.0f ),    // Point B
 
                  new Vector3( -1.0f, 0.0f, 3.0f ),    // Point C
                    new Vector3( 5.0f, 0.0f, -7.0f ),     // Point D
                    out v3Intersection ) )
         {
             Debug.Log( "Intersection at " + v3Intersection.ToString( "F4" ) );
         }
     }
 }
 

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

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

Related Questions

AI Field of vision 1 Answer

Another Raycast Issue. 0 Answers

How do you create an AI that works like this ? 1 Answer

nav mesh agent not following properly 0 Answers

Way to find closest point in a specific area on navmesh. 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