0
Answered

Raycasting Flow Machine not working

RaHaN 4 years ago updated 4 years ago 10

Hello everyone,

So I just started using bolt as an alternative to Playmaker for prototyping stuff.

And I'm already stuck with something that should be quite straightforward, but doesn't seem to work on my end.

The FlowMachine I'm trying to do is very simple: raycast from the mouse position on screen viathe camera, return the hit gameobject transform (or name or whatever).

Here is how I set it up:



The scene itself is basic: the camera's rotated 90° to point downward at a board made of 1x1 squares, all of which have a rigidbody and box collider components.

Not hit is registered, regardless of the Max Distance specified or the layer masks. I'm kind of at a loss...

Is there some obvious mistake I did?


Thanks in advance for your help!


Bolt Version:
Unity Version:
Platform(s):
Scripting Backend:
.NET Version (API Compatibility Level):

Thanks for your answer Elin, even though it didn't help ;p

I'm trying to figure out the issue, still didn't find it. Inserted some debug units to see where in the flow something goes wrong, and it seems to point to the hit detection. The ray is indeed being cast, I can draw it and see it. I tried the following things:

  • Changing collision detection on the objects from discrete to continuous to continuous dynamic
  • moving the camera closer to the objects (to the point of seeing the actual debug ray gizmo going through them)
  • switching the rigidbodies to kinematic or not
  • putting the flow machine on the main camera instead of a gameobject in the scene

All to no avail. I'm definitely at a loss.

I just coded it by hand quickly, and it works that way perfectly.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RaycastingShit : MonoBehaviour {
    public Ray ray;
    public RaycastHit hit;
    public Camera cam;
    public Vector3 startPoint;
    // Use this for initialization
    void Start () {
        cam = GetComponent<camera>();
    }
    
    // Update is called once per frame
    void Update () {
        startPoint = Input.mousePosition;
        ray = cam.ScreenPointToRay(startPoint);
        Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
        if (Physics.Raycast(ray, out hit))
        {
            Debug.Log("hitting " + hit.transform.name);
        }
        else
        {
            Debug.Log("hitting nothing");
        }
    }
}

So, bug?

Pending Review

Are you sure this isn't related to the layer mask or the max distance?

What happens if you use the overload of Raycast without these parameters, like you did in your script?

IIRC, a layer mask of zero in Unity will never hit anything.

Hello Lazlo

Something must have been off on my end; I re-launched Unity and redid the whole thing in another scene, and it works, now.

Ok so more insight about what was going on. As suspected, it was a mistake on my part in considering the LayerMask as the Layer against which to test the raycast hit, whereas it's the layer to exclude (a noobish one, but hey!)

Now, while using bolt, how would one go to add several layer masks but the one layer you want the raycasts to register hits? Adding one unit per layer?

If I'm not mistaken you would not. You would want to the that in Unity before hand. Edit > Project Settings > Physics

@William: I don't get your point? 

The goal is to test against one layer only, registering hits only on rigidbodies that are on a specific layer, which is easy to do "by hand" with a simple if statement on the index integer of the layer you want to register.

Say, for example, you have an "1:Enemies", "2:Friendlies", and "3:Neutrals" layers, and would want the raycast to register only enemies, you would have to Mask Friendlies and Neutrals by testing > 1.

So my question is about the implementation of this in Bolt, the first impulse being to add one Physics.Raycast Unit per layer you want masked, but it seems overkill and inefficient.

Ahh, sorry.  I had not read the entire post. You want to set the layer mask at runtime, is that  correct? I thought you were referring to setting it up beforehand in Unity. 

+1
Answered

You can use a Layer Mask Literal for this:

You can then select one or more layer(s) you want to target:


Thanks ! Works like a charm :)