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 bsbimalbose · Dec 14, 2013 at 04:56 PM · spritemouseclicklinecast

Problem with LineCast on new unity 2D

I am making a 2D Game with sprites on the new Unity3D 4.3 The linecast methord is not returning true even-though colliders are available between the points.

void Update () {

     Vector3 mousePos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, -10);
     Vector3 backEnd = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10);
     Debug.Log (mousePos+"    "+backEnd);

     if (Physics.Linecast(mousePos,backEnd)){
         Debug.Log ("Hit");
     }

 }

Any help?

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
0

Answer by robertbu · Dec 14, 2013 at 05:01 PM

You need to convert your positions into world coordinates before making the cast. The 'z' coordinate is the distance in front of the camera, so your code will be something like:

     Vector3 mousePos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane);
     mousePos = Camera.main.ScreenToWorldPoint(mousePos);
     Vector3 backEnd = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 20);
     backEnd = Camera.main.ScreenToWorldPoint(backEnd);
     
     Debug.Log (mousePos+"    "+backEnd);
      
     if (Physics.Linecast(mousePos,backEnd)){
        Debug.Log ("Hit");
     }

Note it would be simpler to use a Raycast and just set the distance of the cast to 20. Also this will not work with a 2D collider. You must have a 3D collider on the object or on a child object.

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 bsbimalbose · Dec 14, 2013 at 05:08 PM 0
Share

What should i do to make it work with a Polygon Collider 2D? Can i use a 3D collider on a 2D sprite. Forgiv me if its a stupid question.I am just learning unity

avatar image robertbu · Dec 14, 2013 at 05:27 PM 0
Share

It's not a stupid question, and it is new territory since the Unity 2D stuff is new. You cannot add a 3D collider and a 2D collider to the same game object. One solution is to add a 3D collider (such as a box collider) to a child empty game object of the 2D object. You will need to size the collider appropriately for your 2D object.

Given what I think you are doing, another solution is to use On$$anonymous$$ouseDown(). On$$anonymous$$ouseDown() works with both 3D and 2D colliders.

avatar image bsbimalbose · Dec 15, 2013 at 03:11 PM 0
Share

Thanks a lot, I added 3D colliders as children and it works fine

avatar image
0

Answer by KellyThomas · Dec 15, 2013 at 03:18 PM

Another option is to use Physics2D.Raycast, it will work with 2D colliders.

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

17 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

Related Questions

A node in a childnode? 1 Answer

Handling animations on sprites that involve axis... 1 Answer

Sprite disappears in front of background 1 Answer

SpriteManager 2 1 Answer

Pressing "T" in the Sprite Editor trims the slice. It should only work when pressing "Shift+T". Please help 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