Hello.
So I’m fairly new to Unity and javascript, and very new to Physics.Raycast
and evidently, I have many, many problems.
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 as much. I did some research and I was suggested to use the Physics.Raycast feature. Now I managed to grasp the basic idea of how it works, but the code confuses me a lot.
I tried to use Physics.Raycast to click the object in order to play the animation that is placed on it. So in order to do that I had to shoot a raycast forward from the Camera’s position depending on where the Mouse is. You see, what I’m trying to explain here is very difficult for an ameuter like me to put into words.
Think of it like an FPS game. When you fire the gun, the bullets fly forward depending on where the mouse is. if it’s top right, the bullets fly forward towards the top right. I’m trying to use the same principle in my little experiment. I want to shoot out raycasts forward depending on where the mouse is. Only 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");
}
}
However, this code draws a line going backwards rather than forwards. It’s also only affected by the Mouse’s Y position; not X. Why so?
What can I add to this code to make it work the way I described above?
[8270-screen+shot+2013-02-25+at+20.21.35.png|8270]
The image might not be clear but at the time of capture my cursor was in the game view hovering over the red part of the sphere. You can clearly see the raycast in the scene view was pointing in a complete different direction, rather than through the red part of the sphere.
I apologise if I wasn’t specific enough or if I haven’t explained my situation properly. This is the best I can do.
Any help would be greatly appreciated.
Since you wrote there was nothing in front of your camera, before exploring RaycastAll(), I suggest you make some attempts to figure out why the Raycast() is failing. As a start, inside the Raycast(), put something like: Debug.Log("Ray hit: "+Hit.collider.gameObject.name); This should allow you to figure out if the Raycast() is hitting something that does not have a "blue" tag, or its failing to hit anything at all. Either will tell you something about the problem.
– robertbuMy best guess is that you have issues with the colliders. Raycast works with the colliders for the objects, not 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 get the collider issue sorted out, you can go back to OnMouseOver().
– robertburobertbu, I greatly appreciate the time you have taken out of your day to help someone like me. I'm going to look around in my project and see what I can do about the colliders. Again, thank you for your time and help. Much appreciated.
– goldensnypa