playMaker

Author Topic: Problem with Raycasts[SOLVED]  (Read 3626 times)

uberwolfe

  • Junior Playmaker
  • **
  • Posts: 59
Problem with Raycasts[SOLVED]
« on: August 30, 2013, 09:32:05 PM »
SO I have an issue with raycasts, specifically debug ray.

I have a test scene where I'm controlling a turret with the mouse. The turret is supposed to look at whatever point on a 3d object that mouse cursor is over(in this testing scenario a plane).

The rotation aspect works ok but the turret seems to not look exactly at where the mouse is on the plane. I have setup a debug ray to fire from the turret to the point of the mouse, but it's always way off.

As you can see in the picture, the mouse cursor is about where the little square graphic is and the ray is nowhere near that... what could be going on? help!!



« Last Edit: August 31, 2013, 10:05:37 PM by uberwolfe »

redikann

  • Full Member
  • ***
  • Posts: 174
Re: Problem with Raycasts
« Reply #1 on: August 31, 2013, 08:30:09 AM »
I believe the raycast is projecting form the objects local Z+. Is your Pivot point centered on the screen. Also notice that your raycast offers a position vector which could be used as an offset if you need it.
If your turret was modeled in another program try snapping your origin somewhere pointing down the barrel.
« Last Edit: August 31, 2013, 08:33:28 AM by redikann »

uberwolfe

  • Junior Playmaker
  • **
  • Posts: 59
Re: Problem with Raycasts
« Reply #2 on: August 31, 2013, 10:27:30 AM »
Thanks for the comment redikann :)

I am using an empty game object positioned at the tip of the turret as the source of the ray - this seems to work fine. What I can't understand is how those values of the mouse position are being returned as they're clearly not right. The turret is modelled from Unity primitives.

Cheers again!
« Last Edit: August 31, 2013, 07:27:31 PM by uberwolfe »

redikann

  • Full Member
  • ***
  • Posts: 174
Re: Problem with Raycasts
« Reply #3 on: August 31, 2013, 03:41:54 PM »
Ok, easy solution get rid of the debug ray and just check debug in the Look At action.

uberwolfe

  • Junior Playmaker
  • **
  • Posts: 59
Re: Problem with Raycasts
« Reply #4 on: August 31, 2013, 07:31:26 PM »
I need that Mouse Pick point 100% accurate though, as the turret will fire a projectile to that position.

redikann

  • Full Member
  • ***
  • Posts: 174
Re: Problem with Raycasts
« Reply #5 on: August 31, 2013, 08:09:53 PM »
IF you haven't created the logic for the projectile , you should start. I don't think what your seeing in the scene view while playing the game is accurate or inaccurate. The main Camera has to fire a ray to the mouse position and hit a collider to register a point in space. When you are watching the ray in scene view it kind of skews the reality of it all. I created a quick scene with all your settings and for the most part the turret looked like it tracked perfectly in game view while the ray was off in scene view.
Create your logic for the projectile and store some info in variables where your projectiles are colliding. This should confirm what is happening.

uberwolfe

  • Junior Playmaker
  • **
  • Posts: 59
Re: Problem with Raycasts
« Reply #6 on: August 31, 2013, 09:01:07 PM »
Thanks for your insight again Redikann. Though those screenshots were not from the scene view, they were from game view with the gizmos switched on.

I created some quick projectile logic and the projectile does indeed go exactly where the mouse cursor is pointed - but the ray is still way off

The picture below illustrates this. The sphere is where the projectile finished - exactly where the mouse clicked, but the ray still mysteriously shoots off somewhere else as you can see.


I will be needing the rays to gather various data before I shoot the projectile so I can't just live without the rays unfortunately :(



redikann

  • Full Member
  • ***
  • Posts: 174
Re: Problem with Raycasts
« Reply #7 on: August 31, 2013, 09:31:02 PM »
Try this, use a 'Transform Direction' from the Transform category and store the object direction in a vector3 then use that vector for the debug ray instead of the object.

uberwolfe

  • Junior Playmaker
  • **
  • Posts: 59
Re: Problem with Raycasts[SOLVED]
« Reply #8 on: August 31, 2013, 10:04:56 PM »
Ok, so I worked it out :P

The problem was my confusion between positions and directions. Seeing they were both vector3 values I mistakenly assumed them to be the same.

In my case, the proper direction of the ray is calculated by subtracting the mouse cursor position from the position of the turret point.

Here's a screenshot of how I wired it if it helps anyone in the future:


And a HUGELY useful post on vector math :)
http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/
« Last Edit: August 31, 2013, 10:09:44 PM by uberwolfe »