• 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 MisterMortal · Jan 10, 2014 at 03:47 PM · errorconvert

Cannot implicitly convert type `UnityEngine.Vector2' to `UnityEngine.Ray2D'

I am new with Unity and I need rays to work in 2d, so I am trying to use Ray2D, but I got this weird error:

 Cannot implicitly convert type `UnityEngine.Vector2' to `UnityEngine.Ray2D'

Here is the code line which causes error:

 Ray2D ray = new Vector2 (Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);

And here is whole function that causes error:

 void touching2D (Vector3 touchPosition, int touchNumber)
     {
 
         Ray2D ray = new Vector2 (Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
         RaycastHit hit = new RaycastHit();
         
         if(Physics2D.Raycast(ray, out hit,130))
         {
             hit.collider.SendMessage("pressed");
             touchedSomething = "Yes!";
         }
     }
     
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
0

Answer by robertbu · Jan 10, 2014 at 03:52 PM

A Vector2 is a direction. A Ray is a position (origin) and a direction. It looks to me like you want to cast a ray from the current position of the object this script is attached to in the direction of the mouse cursor. Assuming an orthographic camera, this should work to construct your ray:

 Vector2 v = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
 Ray2D ray = new Ray(transform.position, v);
Comment
Add comment · Show 3 · 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 Norax · Aug 04, 2014 at 11:20 AM 0
Share

Looks like that solution doesn't work anymore. You need the variable v to be a Vector3. So I guess this should be the fix:

 Vector3 v = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
 Ray2D ray = new Ray(transform.position, v);

What I dont really get is, why do you substract transform.position?

avatar image robertbu · Aug 04, 2014 at 01:58 PM 0
Share

You subtract 'transform.position' to create a vector that has the direction from your objects position towards the mouse position. If you did not subtract transform.position, v would be from the origin. If your character is fixed at the origin, then you would not need to subtract transform.position.

avatar image Norax · Aug 04, 2014 at 02:16 PM 0
Share

Oh, got it. Thanks :)

By the way, shouldn't be the constructor Ray2D? Like this:

 Vector3 v = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
 Ray2D ray = new Ray2D(new Vector2(transform.position.x,transform.position.y),new Vector2(v.x,v.y));

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

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

18 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

Related Questions

"Max couldn't convert the max file to an FBX file!" error 1 Answer

a simple error (hopefully): cannot convert implicity any help appreciated 1 Answer

error BCE0022: Cannot convert error, please help 3 Answers

Exported SWF no longer loads 1 Answer

Cannot implicitly convert type string to list - Why is this? 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