Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to prevent Vector3.SignedAngle from constantly checking angle?

I have a raycast that shoots down, and then a Vector3.SignedAngle to check for angles, and if there is an angle, do something.

But this should only be activated/checking for angles while I am standing on a non-flat surface. But it’s constantly checking so even if I am on flat ground, it will make some of my other movement mechanics very wonky and not work due to the constant angle check which will make my player do something. So it will essentially constantly overlap with any other of my other non-related code.

I have tried (if angle < 1f && > 0.1f) so that it will not check until I am on an angle, but I know a condition like that does not work, and also wrapping the entire raycast in an angle check does not work either (no errors).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

void FixedUpdate()
    {
        RaycastHit hit;

        if (Physics.Raycast(transform.position, Vector3.down, out hit, 2f))
        {
            float angle = Vector3.SignedAngle(hit.normal, Vector3.up, transform.right);
            if (angle < 0.1f)
            {
                // Do something
            }
            else if (angle > 0.1f)
            {
                // Do something
            }
        }   
    }

>Solution :

Based on your code, it seems you want to perform certain actions only when you’re standing on a non-flat surface. However, the continuous angle checking is causing issues with your other movement mechanics. To address this, you can introduce a boolean flag to track whether you’re on a non-flat surface or not. Here’s an example of how you can modify your code:

private bool isOnNonFlatSurface = false;

void FixedUpdate()
{
    RaycastHit hit;

    if (Physics.Raycast(transform.position, Vector3.down, out hit, 2f))
    {
        float angle = Vector3.SignedAngle(hit.normal, Vector3.up, transform.right);

        if (angle < 0.1f)
        {
            if (isOnNonFlatSurface)
            {
                // Do something when transitioning from non-flat to flat surface
                isOnNonFlatSurface = false;
            }
        }
        else if (angle > 0.1f)
        {
            if (!isOnNonFlatSurface)
            {
                // Do something when transitioning from flat to non-flat surface
                isOnNonFlatSurface = true;
            }

            // Do something while on a non-flat surface
        }
    }
    else
    {
        if (isOnNonFlatSurface)
        {
            // Do something when transitioning from non-flat surface to no surface
            isOnNonFlatSurface = false;
        }
    }

    // Perform other movement mechanics here
}

In this modified code, we introduce the isOnNonFlatSurface boolean flag to keep track of whether you’re currently on a non-flat surface or not. We only perform the specific actions when transitioning from flat to non-flat surface and vice versa.

Additionally, we handle the case when there is no surface detected by the raycast, and we transition from a non-flat surface to no surface.

You can adjust the conditions and actions inside each block to suit your specific needs. This way, the angle check will only be activated when transitioning between flat and non-flat surfaces, preventing interference with your other movement mechanics.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading