- Home /
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
i don't get your problem :/ how do you want to point the sprite? or why doesn't the normal raycast work for you?
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 ?
look at the Camera RayCasting references. shoot a ray towards the sprite but mask its layer so the ray does not hit it.
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
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.
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
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..
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.
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
![](https://koobas.hobune.stream/wayback/20220613175203im_/https://answers.unity.com/themes/thub/images/avi.jpg)