wide raycast?

I want to make a raycast projecting from the camera, but with a radius (CapsuleCast), so that there’s a lot of leeway for looking and still triggering what I need to trigger.

I basically want to detect if something is visible on the screen, but I want to not trigger it if it’s behind a wall, which is why I’m using casts.

I’ve looked around, and read about CapsuleCast on the Manual, but for some reason I can’t get it working, even after watching multiple tutorials.

This is what I got so far:

if (Physics.CapsuleCast (transform.position, transform.position.x, transform.forward, Mathf.Infinity, out hit)) {

}

I converted it from a normal Raycast, so some stuff might be there that is not supposed to. As I said, I read the manual, but I just don’t understand some parts, so I just kept them the same as for the raycast.

First thing is, I’m not sure how to specify the second transform position.

And I’m generally not sure how to set the whole thing up. I want it to have one at the camera’s position, and the other away from the camera (so it would be laying down and not standing).

Also, I don’t want to use a LayerMask, so I don’t know if I can just not include it without any errors.

Could you explain (a bit more) what the cast is looking to find/discover? Just curious. May lead to “why not using layer mask” and/or maybe some other way of doing what you want to do … (only maybe:))

As for what the 2 points are: They are the position of the centre of the sphere at both ends of the capsule.

1 Like

yeah, the method you appear to be calling is this overload:

public static bool CapsuleCast(Vector3 point1, Vector3 point2, float radius, Vector3 direction, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

This means you’re passing a float (position.x) as point2, which should be a Vector3.

Then the forward as your radius… that’a Vector3 as a float.

Then Mathf.infinity as your direction… a float where you should have a Vector3.

Basically you’re calling this all wrong!

Also, you may prefer a SphereCast really, from the sounds of your description:

Check the documentation, you need to pass the appropriate values as the appropriate parameters.

1 Like

I’m using a tag to detect a specific object in the scene.

Just 1 object? Could you maybe set this up with OnBecameVisible/Invisible maybe?
@lordofduct gave some good feedback.

Lastly, if it’s 1 or a few objects, and the became visible thing is not great for you, layermask is nice, in that it cuts down how much work has to be done, that’s the only reason i mentioned it may be good. If you don’t mind or think it’s not important, that’s cool with me if it’s cool with you :slight_smile:

1 Like

Would that (the layermask) account for the object being behind another one (in which case it wouldn’t trigger)?

Ahh, wow… my brain. lol My bad… I see what you’re talking about. :slight_smile:
You’re initial thought was correct, I just wasn’t getting it at first.

1 Like

So I’ve got this for a SphereCast:

if (Physics.SphereCast(transform.position, 45f, transform.forward, out hit, Mathf.Infinity)) {

}

I’m not getting any errors but it’s not working either.

Inside this, I have a nested “if” statement, checking for the tagged object being hit.

It’s returning false or returning true but not finding your tag?

1 Like

No, the whole cast doesn’t seem to be working properly.

I can get it to trigger, but only in certain positions in relation to the object.

I think the cast might be somehow positioned incorrectly, or facing the wrong way.

For example, if I look at the object from my spawn position, nothing happens, but if I move around slightly, it does. I also walked behind the object, and slightly to its right, and it wasn’t triggering either.

Okay, I just tried this… and here’s what I discovered : If you’re hitting anything at the start, it will fail. At least this is what I think I found out. I’ve moved some cubes around and tested it :slight_smile:

Edit: Yep, pretty sure that is it. I put a radius of 2 for the cast, and put a cube at 0,0,2.4 and the one with the casting script at 0,0,0 … no hit. then moved the first one to 0,0,2.55 (centre of the cube is the start, I think), and it hit. :slight_smile:
If that’s the case and your area is huge, if it’s just 1 or a few objects, I might recommend again OnBecameVisible/Invisible (as I think this should work for “if it’s seen”). The other option, I’m guessing would be to spherecast on a layer, and if you get ‘true’, to linecast, maybe, and if that hits it, as well, you’ve got a success. I dunno, just thinking out loud :slight_smile: (the linecast wouldn’t be on a layer, but you’d have a target to aim at/direction, I think…)

1 Like

But it also doesn’t trigger at certain locations. As I said, if I walk behind the object and slightly to the right, then look at it, it won’t trigger.

That’s still helpful to know though. :slight_smile:

Hm… Right, so that’s not contradicting what I wrote in my “observations” — ie: Are you too close to the object, so that it’s inside the spherecast when it begins, thereby not producing a hit…?

1 Like

Yes. I had the radius set to 5, which caused the object to be inside the cast at the start. I reduced it to 2, and now it works.
The “back-right” situation still persists.

Well, at this point… you can imagine that the spherecast alone can’t do what you were originally wanting, I’d imagine. I mean, once you’re too close/the sphere is too big, it won’t work.
I should alter my previous response about spherecasting a layer, as I imagine the same thing would happen. I’d change that to “overlapsphere” maybe :wink:
What do you think?

1 Like

SphereCast will not pick up thing inside itself when cast.

What you should do is set the start position to be: position - (direction * radius)

This should catch everything in front… though it could also catch some things above and below yet slightly behind the camera.

Another option is to do a BoxCast:

And again, position it to start from behind the camera. Though remember, you need to include the orientation, just use the camera’s rotation for that.

This will resolve grabbing those things slight above and below.

1 Like

I think that first thing actually got it working ( “(direction * radius)” ). And I also discovered that what is causing it to stop working from certain positions, is the trees painted on the terrain. Because the SphereCast is hitting those, and stopping.

And I just realized what a problem that is. Because the trees are part of the terrain, therefore I can’t somehow exclude the terrain from the cast checking, in case the object is behind a hill or something like that. But I need to be able to trigger stuff when even the slightest part of the object is in view.

Could this be achieved by checking if it’s visible, and also projecting a raycast to see if something is in front of it?

Like make so that if it’s visible and there’s something in front of it, then trigger stuff, but it’s it’s not visible and there’s something in front of it, don’t trigger stuff.

Because there’s going to be a lot of scenarios when the object is partly obstructed, and I want it to trigger stuff in those cases. If if it’s not and it’s behind the terrain, it won’t trigger.

Since just checking for “visible” doesn’t check for objects in front of it, which would result in triggering even if the object is fully obstructed.

I think it’s worth a shot to try visible. Ya, you could cast a line after (but if it’s visible, does it have to be all visible , not partially blocked… ? otherwise, just being visible would be “done”).
At least try and see! lol

1 Like

I updated my post.

Basically I just added a statement saying, that “visible” doesn’t check for stuff in front of it. So if it was visible (rendered) but it was fully obstructed by another object, it would still trigger. That’s why I suggested a “visible” and “raycast” combo.

I will try that tomorrow, as it is getting late and I have college early in the morning. I will let you guys know how it goes and if I have any further issues.