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 Qasem2014 · Dec 30, 2014 at 11:21 AM · 2draycastsprite

ray from 2d image to 3d objects !!!!!

hi

a 2d image is on my scene ( move with finger touch around the scene ) . this image is child of a canvas with "Screen Space-Overaly" render mode << important !!!

i want to know how can i get a ray from this 2d image to 3d objects !!!

i attach a image that make my question clear :)

EXCUSE ME FOR MY BAD ENGLISH

alt text

a.jpg (25.3 kB)
Comment
Add comment · Show 6
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 taxvi · Dec 30, 2014 at 11:38 AM 0
Share

i don't get your problem :/ how do you want to point the sprite? or why doesn't the normal raycast work for you?

avatar image Qasem2014 · Dec 30, 2014 at 12:10 PM 0
Share

taxvi , i just don't know how can i do this ! should i use a camera as child of sprite to get raycast from it ? what should i do ?

how can i get the game object reference that the sprite is pointing to it ?

avatar image taxvi · Dec 30, 2014 at 12:19 PM 0
Share

look at the Camera RayCasting references. shoot a ray towards the sprite but mask its layer so the ray does not hit it.

avatar image RudyTheDev · Dec 30, 2014 at 12:52 PM 0
Share

http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html

avatar image taxvi · Dec 30, 2014 at 01:00 PM 0
Share

sorry mate but you need to get started not only with raycasting but with OOP in general. and there are plenty of tutorials explaining raycasting. take your time https://www.youtube.com/results?search_query=unity+camera+ray

Show more comments

1 Reply

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

Answer by Owen-Reynolds · Dec 30, 2014 at 04:38 PM

I think the problem is you think that raycasts have to come from cameras. They don't. There's a special, complicated raycast that shoots from the camera through the mouse. You may have seen that. But most raycasts are easier than that.

You should be able to find examples with "unity raycast down." Lots of people want to check how far their player's feet are from the ground, so do a down raycast from the player.

A basic down raycast looks like this:

 RaycastHit Hit;
 if(Physics.Raycast(transform.position, Vector3.down, out Hit, 10)) {
   Debug.Log("I am over "+Hit.transform.name);
 }

Looks odd, but transform.position says to start from where you are, and Down says to fire it down. If your game is on a wall, then shoot it forward instead (whichever direction the thing under you will be.) Then Hit catches info about what you hit, and 10 is distance to shoot (using Hit requires you to use distance, just because. Can put 9999.)

You should be able to find lots of raycast examples of this general form. Yes, they are tricky.

Comment
Add comment · Show 5 · 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 Qasem2014 · Dec 30, 2014 at 06:06 PM 0
Share

Very Good Explain

i just use the raycast before without understanding :D

SO

this code is very handy and works fine but not on a 2d image that is child of a canvas with "Screen Space-Overaly" render mode !!!

i want make this script work on a 2d image that is on screen not in 3d space :D

the image move with finger touch or mouse pointer on screen

avatar image Qasem2014 · Dec 31, 2014 at 10:54 AM 0
Share

i change my question :D it should be more clear now :D

avatar image Tepei · Dec 31, 2014 at 01:17 PM 0
Share

You have also Physic2d.Raycast : http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html

But if you use the new UI that's different.. I don't know how we could use physic or raycast with UI..

avatar image Owen-Reynolds · Dec 31, 2014 at 04:50 PM 0
Share

Ah...yes, I think a modified "camera raycast" would work, to shoot from yor UI element to a world gameObject.

The trick is, raycasts are done "in the world," so they have to figure out where the mouse would be floating in the game world. They use Camera.screenToWorldPos(Input.mousePosition) to get that.

To use a sprite(?) ins$$anonymous$$d of a mouse, do something like Camera.ScreenToWorldPoint(mySprite.position) ins$$anonymous$$d. Then shoot the camera ray through there ins$$anonymous$$d of through the mouse.

I don't know Canvasses or the new 2D. So it might be in 0-1 viewport coords ins$$anonymous$$d.

avatar image Qasem2014 · Dec 31, 2014 at 05:44 PM 0
Share

I SOLVE IT :D

TNX To All For Comments :)

this is my code :

 var Point:GameObject;
 
 function Update()
 {
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     Point.transform.position.x=Input.mousePosition.x;
     Point.transform.position.y=Input.mousePosition.y;
     var hit : RaycastHit;
     if (Physics.Raycast (ray, hit, 100))
     {
         print(ray.origin + " --- " + hit.transform.name);
     }
 }

1- i get a reference to my sprite 2- i get a ray from mouse position to world space 3- i change the sprite position to mouse position :D

<< i find out that i can't get a ray from 2d sprite in new UI system >>

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

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

Related Questions

[C#] 3D raycast to sprite. Two cameras. 1 Answer

Ray effect on sprite with obstacles 0 Answers

Animator - Stoping && Playing from Specific frames. 5 Answers

Changing "Sprite Mode" from Single to Multiple breaks reference to sprite sheet. 1 Answer

2D Level Design (programmatically or not) 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