Jump to content

Why does raycast "hit" Kerbin when not aimed at Kerbin?


Recommended Posts

Why does Unity's Physics.Raycast keep hitting Kerbin at the wrong angles? It doesn't hit Kerbin when aimed at Kerbin, and DOES hit Kerbin when not aimed at Kerbin.

Here's some screenshots of what my laser distance measurer is reporting: (-1 is what I display when I get no hit).

Note it DOES show a hit when not aimed at Kerbin, and does NOT show a hit when aimed at Kerbin. I have checked my logic and it's not just a case of inverting the logic, because it does work when hitting nearby objects like other vessels, and it doesn't *always* claim to be hitting Kerbin - just when I'm aimed near but not at kerbin. If I aim up at the sky it goes back to showing no hit.

The distance of the hit is also rather weird, but I'm going to tackle that issue next. First I don't even understand why it's registering the wrong hit in the first place.

(Rightclick the images and pick "view image" to see them at full-res, to see the text.)

WbwDNlV.png

FP5XNqu.png

This is the call to Physics.RayCast():


if( Physics.Raycast(origin, pointing, out hit, maxDistance, mask) )

Where 'mask' is this:


mask = (1 << maskBitDefault)
+ (1 << maskBitWater)
+ (1 << maskBitPartsList)
+ (1 << maskBitScaledScenery)
+ (1 << maskBitLocalScenery)
+ (1 << maskBitKerbals)
+ (1 << maskBitDisconnectedParts)
+ (1 << maskBitPartTriggers)
+ (1 << maskBitWheelColliders)
+ (1 << maskBitTerrainCollidres) ;

and those bit fields are these:


private int maskBitDefault = 0;
private int maskBitWater = 3;
private int maskBitPartsList = 8;
private int maskBitScaledScenery = 10;
private int maskBitLocalScenery = 15;
private int maskBitKerbals = 16;
private int maskBitDisconnectedParts = 19;
private int maskBitPartTriggers= 21;
private int maskBitWheelColliders = 27;
private int maskBitTerrainCollidres = 28;

Link to comment
Share on other sites

okay so apparently it's on layer 10, which I *THINK* is the mapview layer... but....but... I'm not *on* the map view. So that's weird that it's seeing hits for it. At any rate I'll remove that from the mask list. I still can't figure out how to find a hit when the terrain polygons are unloaded for being too far away, but at least no hits is an improvement over wrong hits.

Link to comment
Share on other sites

I think that above some certain height (160km for Kerbin? that's where the performance hit from the terrain settings cuts off) it switches to scaled space, though I could just be making that up. :wink:

Link to comment
Share on other sites

I think that above some certain height (160km for Kerbin? that's where the performance hit from the terrain settings cuts off) it switches to scaled space, though I could just be making that up. :wink:

I get that. The problem is that I'm not actually aimed AT the planet and it's hitting it. that's not just a difference of scale - that's a difference in rotation, I think.

Link to comment
Share on other sites

  • 2 weeks later...

The problem was that layer 10, the map view layer, is being drawn all the time, even when not on map view. Therefore raycast is hitting the map view's version of the Kerbin planet even when not in map view. Masking off layer 10 from the raycast fixed it.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...