unity 3d draw ray from camera to mouse click
- Spaces
- Default
- Help Room
- META
- Moderators
-
- Topics
- Questions
- Users
- Badges
- Home /
1
Raycast from Photographic camera's to the Mouse's position?
Hi.
And so I'm fairly new to Unity and javascript, and very new to Physics.Raycast and evidently, I have many, many issues.
So here is what I'm trying to achieve. I wanted to make a certain object play an animation when clicked. So I used the OnMouseOver function. After experimenting with the scene I noticed many bugs and that OnMouseOver wasn't called every bit much. I did some research and I was suggested to utilize the Physics.Raycast feature. At present I managed to grasp the bones idea of how it works, but the code confuses me a lot.
I tried to apply Physics.Raycast to click the object in gild to play the blitheness that is placed on it. So in order to do that I had to shoot a raycast frontward from the Photographic camera's position depending on where the Mouse is. You run into, what I'1000 trying to explicate here is very difficult for an ameuter like me to put into words.
Recollect of it similar an FPS game. When y'all fire the gun, the bullets fly forward depending on where the mouse is. if it's top correct, the bullets fly forward towards the superlative right. I'm trying to use the same principle in my fiddling experiment. I want to shoot out raycasts forward depending on where the mouse is. Merely make the object active when the mouse is hovered over it, which is where the mouse click comes in to play.
I attached a javascript to my Camera and this is what it says so far:
function Update () {
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition); Debug.DrawRay(transform.position, (Input.mousePosition), Color.green); if (Physics.Raycast (ray)) { Debug.Log("Hit"); } }
Notwithstanding, this lawmaking draws a line going backwards rather than forwards. It'south also only affected by the Mouse's Y position; not X. Why so?
What can I add to this lawmaking to make it work the mode I described above?
The image might not be clear but at the time of capture my cursor was in the game view hovering over the crimson role of the sphere. You tin clearly see the raycast in the scene view was pointing in a complete dissimilar direction, rather than through the carmine part of the sphere.
I apologise if I wasn't specific plenty or if I haven't explained my situation properly. This is the best I tin can do.
Any help would be greatly appreciated.
one Respond
· Add your respond
- Sort:
iv
Answer by robertbu · Feb 25, 2013 at 08:55 PM
The Input.mousePosition is in screen coordinates and transform.position is in earth coordinates. The ScreenPointToRay() makes that conversion. If you wanted to draw the ray, yous could:
Debug.DrawRay(ray.origin, ray.direction, Color.green);
Note I suspect the OnMouseOver() failed because you had other (perchance transparent) object in front? If so, the standard Raycast() will not solve your problem. Yous likely will demand to apply Physics.RaycastAll() and then cycle through all the hits. Raycast() will stop with the first object hitting merely like the code that handles OnMouseOver()
Your reply
Welcome to Unity Answers
The best place to ask and respond questions well-nigh evolution with Unity.
To help users navigate the site we take posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more data.
Make sure to check out our Knowledge Base for normally asked Unity questions.
If you are a moderator, encounter our Moderator Guidelines page.
We are making improvements to UA, see the listing of changes.
Follow this Question
Related Questions
Source: https://answers.unity.com/questions/406419/raycast-from-cameras-to-the-mouses-position.html
@robertbu Thank you for your quick reply.
I have replaced my Debug.DrawRay with your lawmaking and it seemed like it partially fixed my problem. Withal, I'm a flake confused on the PhysicsRaycastAll() characteristic that you lot accept suggested. Since the bug with the On$$anonymous$$ouseOver() I was getting is also occurring with the Raycast arroyo.
I replaced my code and added a few lines. This is attached to my Photographic camera:
var Hit : RaycastHit; var Range : float = 50;
function Update () {
}
The Tag I'm referring to is the blueish side of my sphere, which is a dissever object. Now, when I correct click that object, I only get occasional calls, in fact, I get more fails than calls.
Is in that location something I missed or is Raycast just like On$$anonymous$$ouseOver?
And past the way, I don't have an object placed in front of my camera.
Since you wrote in that location was nothing in front of your camera, before exploring RaycastAll(), I propose you brand some attempts to figure out why the Raycast() is failing. As a first, inside the Raycast(), put something similar:
This should permit you to figure out if the Raycast() is hit something that does not have a "blue" tag, or its failing to hit anything at all. Either volition tell yous something about the problem.
Ok,
later I inserted the Debug line, I noticed something very odd. Since the sphere is a combination of eight objects, each 1 with a dissimilar colour, the Raycast constantly calls the other parts of the sphere. Sometimes it even has the nerve to call a part that is no where about where I have my cursor pointed at. Sometimes the Raycast hits the object that is directly behind the blue side. Only in very rare occasions does information technology ever call the blueish tag.
I have absolute no clue of what I could possible accept washed incorrect. None of the objects on the sphere overlap each other and there is naught blocking the camera.
$$anonymous$$y best guess is that you have problems with the colliders. Raycast works with the colliders for the objects, non the mesh. Is each slice a separate mesh? What kind of collider is on each of the 8 objects? If I'm right, when you lot get the collider consequence sorted out, y'all tin can go back to On$$anonymous$$ouseOver().
robertbu,
I greatly appreciate the time you have taken out of your day to assistance someone like me. I'thousand going to look around in my project and see what I can do about the colliders.
Over again, thank you for your fourth dimension and assistance. $$anonymous$$uch appreciated.