Is there a sample project with functional RayCasting?

Apparently, I am far too dumb to make the code examples in the docs work. I’m not getting any errors, it just doesn’t seem to work for me.

I need to see a functioning example so I can reverse engineer it.

Okay, I figured out that it’s working, as in the ray is going out and hitting the camera, so now I need to do layers. In the docs, layers have numbers, but in the properties window, layers have names.

The docs show a picture of a “Layers and Tags Settings” window with no instructions of where it is or how to get to it. I clicked everything I could and gave up looking for it.

I found that you have to create a physics settings asset to see the collision matrix, but it’s still not the right “Layers and Tag Settings” window to add anything or see the numbers I need.

I tried just guessing the number, and no number between 0 and 9 worked, so I suspect that even if I did find this unicorn window, knowing the right number for the layer wouldn’t matter.

I found the icon in the Settings folder you have to click to find the unicorn window, added a layer, and then changed the layermask code to, what I though, excluded layer 4. As I suspected trying to guess the number, it actually didn’t matter. It seems like if “layerMask: ~(1U << 3)” actually means " to only check collisions with colliders from layer3" then it should have worked the first time anyway.

Yeah, you found it right to use Layers on objects and then use LayersMask for the raycast: Raycasting | Flax Documentation

Layer mask is bit-mask with each bit (out of 32 bits) used to represent a single layer. If bit is set then the physics raycast can hit that layer.

If you want to see a project using RayCasts in a real-world example, check out the FootIK script of CharacterControllerPro: FootIK.cs on GitHub

The Layers and Tags settings can be found in the Settings folder in Content:

The Layers and Tags settings are where you create layers:


And the Physics settings are where you edit the collision matrix:

Actually using a LayersMask with RayCasts can be done really easily by simply exposing a variable to the Properties window with the LayersMask type. It’s much easier to do it that way. You can then simply feed the LayersMask variable into the RayCast. Again, the FootIK script above implements this.

1 Like

Thank you again for responding! I am lowering the quota of my neediness going forward.

I ended up putting the hits in an array and just ignoring the first hit, assuming it will always be the object casting the ray.

I found the layer matrix settings, but I couldn’t make it work there either. Putting the casting object and the to be avoided objects on different layers and unchecking their interaction in the matrix resulted in them having the same interaction as when it was checked.

I will see if can find something that works for me in the script you suggested.

I feel really stupid using Flax, because the criticism that Unity sets you up for bad practices is true. So far, layers have thrown me hard, because Unity just handles it and I have no concept that just clicking the right radio buttons isn’t enough. There’s some esoteric backend to this that Unity protects people from that everyone else just knows. I just don’t know what that is. The same with gimbal locking. Unity has built in stuff that lets you not worry about it when rotating objects. Both LocalOrientation and Orientation gimbal lock immediately in Flax. Also, no mathf.Infinity got me too.

The short of it is, I’ve got tons of questions, but I’m not going to ask them because I’m realizing they’re not game dev questions, they’re Unity refugee questions, and not anyone here’s problem.

Otherwise I become that guy, and no one wants to be that guy, even those who are.

I seriously appreciate all the work that goes into this engine, and I wouldn’t have spent two solid days in search engines trying to solve my dumb problems if I didn’t love it.

1 Like
Click Here for More RayCast Help

RayCasts don’t exist on a collision layer. They simply query for other collision objects. The layer is for specifying which objects the RayCast will query for. Modifying layers in the matrix is for disallowing objects on different layers from interacting with each other, such as Enemies and Water.

If a RayCast is intersecting an object you don’t want it to, put the object on a different collision layer, and uncheck that layer from the mask you created that is exposed to the properties window from your RayCast script. Example:

using FlaxEngine;

namespace Game
{
    public class RayCastTest : Script
    {
        public LayersMask RayCastMask;

        /// <inheritdoc/>
        public override void OnFixedUpdate()
        {
            Physics.RayCast(Actor.Position, Vector3.Forward, 100, RayCastMask);
        }
    }
}

Then, in the Properties window, on your script, you can change which layers to query for. In this case, all objects that are on the Default and Ground layers will be detected, but not the Player layer:
image

Feel free to ask about any questions you have! That’s why these forums exist. Most users of this engine will be those coming from other engines, so if they have similar questions as yours, then they won’t have to ask. They’ll have already been answered. Let’s try to answer some of the ones you’ve already mentioned.

LocalOrientation and Orientation should never have gimbal locking, because they store Quaternions, not Euler Angles. If you want to use Euler rotation, use LocalEulerAngles and EulerAngles. However, those will have gimbal lock.

I do believe that Unity’s Mathf.Infinity just uses this value:

System.Single.PositiveInfinity;

You could try using that instead. It comes with the System namespace.

I hope that these answers help you. I wish you a smooth experience with Flax moving forward!

I appreciate it. I am making some pretty fast strides with Flax and I am enjoying what I’m getting out of it so far.

All of the quaternion.euler hybrids gimbal locked on me. The real issue is the low end of rotation. Any of these work fine if you’re rotating a wind turbine, but less than nine degrees per second on any axis and they gimbal lock. Such as…

SoMoved.LocalOrientation *= Quaternion.Euler(soRotX * Time.DeltaTime, soRotY * Time.DeltaTime, soRotZ * Time.DeltaTime);

The usual way is breaking the rotations apart into steps, but this behaved the same way, also gimbal locking.

SoMoved.LocalOrientation *= Quaternion.Euler(soRotX * Time.DeltaTime, 0, 0);
SoMoved.LocalOrientation *= Quaternion.Euler(0, soRotY * Time.DeltaTime, 0);
SoMoved.LocalOrientation *= Quaternion.Euler(0, 0, soRotZ * Time.DeltaTime);

Of course, the word Euler in all of the above explains it.

What I ended up going with is this.

SoMoved.LocalOrientation *= Quaternion.RotationX(soRotX * Time.DeltaTime);
SoMoved.LocalOrientation *= Quaternion.RotationY(soRotY * Time.DeltaTime);
SoMoved.LocalOrientation *= Quaternion.RotationZ(soRotZ * Time.DeltaTime);

This works fine for what I need, but will still gimbal lock if I set a rotation on any axis less than about two degrees per second. This is quite the rabbit hole for game dev, and I’m more hack than pro. This is also really off-topic. I’m sorry.

I’m sorry. I hope you find a solution. I’m simply not a rotation expert, so I can’t help you.