• 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 shopguy · Apr 29, 2014 at 06:28 AM · 2draycastraycasting

Raycast between 2 points, how to get direction and length?

With this test code (which is wrong):

 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         Vector3 p1 = Camera.main.ScreenToWorldPoint(LastMousePos);
         Vector3 p2 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         RaycastHit2D h = Physics2D.Raycast(p1, p2);
         Debug.Log(p1 + " -> " + p2 + " = " + h.collider);
         LastMousePos = Input.mousePosition;
     }
 }

What should I be using for p2? I wish Raycast() had a version that took source + destination, but instead it wants direction (and optionally distance). So starting with source + destination, how do I get direction and distance?

I want the above code to find any collider between the last 2 mouse clicks. It works (in limited test cases I've tried) going in one direction, but not the other.

This is a 2D game. I want to detect any objects hit as the user drags the mouse across the screen. I know how to check for a hit at the current mouse location, but I also want to catch times when they drag over an object really fast (before the next Update/frame). I guess I'll probably change to RaycastAll in my final code, but shouldn't matter for sake of this question.

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
2
Best Answer

Answer by robertbu · Apr 29, 2014 at 07:46 AM

To cast between two points you want Physics2D.Linecast().

 if (Input.GetMouseButtonDown(0))
 {
     Vector3 p1 = Camera.main.ScreenToWorldPoint(LastMousePos);
     Vector3 p2 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     RaycastHit2D h = Physics2D.Linecast(p1, p2);
     Debug.Log(p1 + " -> " + p2 + " = " + h.collider);
     LastMousePos = Input.mousePosition;
 }


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
2

Answer by robhuhn · Apr 29, 2014 at 07:32 AM

Both Vectors (p1 and p2) are in world space. To get a local vector pointing to p2 relative to p1 you need to subtract p1 from p2.

 Vector3 p1 = Camera.main.ScreenToWorldPoint(LastMousePos);
 Vector3 p2 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 Vector3 diff = p2-p1;
 RaycastHit2D h = Physics2D.Raycast(p1, diff, diff.magnitude);

Didn't test it but it should work.

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 shopguy · Apr 29, 2014 at 03:01 PM 0
Share

Thank you, this should help in future code. Going to accept the other answer only because I think it is optimized for this particular usage.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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

Related Questions

My Raycasts seem to sometimes miss 0 Answers

Raycasting not working as expected: rays are perpendicular to projectile's trajectory and collision doesn't occur 1 Answer

2D Raycast not working 1 Answer

Unity 2D: Visualise a field of view cone 3 Answers

Problem with raycast distance 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges