Vector3 and if() statement

Here is the part of the code I can’t figure out.
It isn’t the final version, I’m stilll trying the get the if- statement at the bottom to work.

Vector3 Bereken_Positie_Nieuwe_Cube(Vector3 HitLocRay, Vector3 PosTarCol, Vector3 ColScale)
        {

        float HitLocRay_X = HitLocRay.x;
        float HitLocRay_Y = HitLocRay.y;
        float HitLocRay_Z = HitLocRay.z;
        float PosTarCol_X = PosTarCol.x;
        float PosTarCol_Y = PosTarCol.y;
        float PosTarCol_Z = PosTarCol.z;
        float ColScale_X = ColScale.x;
        float ColScale_Y = ColScale.y;
        float ColScale_Z = ColScale.z;

        if (HitLocRay_X == 0)
            {
                return HitLocRay + PosTarCol + ColScale;
            }
           
        }

If I change the if statement to ‘if(0 == 0)’ the thing runs fine. But as soon as I’m trying to do something with some of the above floats it throws an error.

It should become something like: if(HitLocRay_X <= (PosTarCol_X + ColScale_X*0.5))

never use == with float, you aren’t guarantee it will happen, generally encase comparison with “lower bound < value < higher bound”

It was just an example. Just to point out where the error comes from. But your point is noted.

But now I like to now how I can get it to work properly.

Post the actual code that is throwing the error, and also post the error message.

using UnityEngine;




public class MousePointScript : MonoBehaviour
{
    private void Update()
    {
        if (Input.GetMouseButtonDown(0)) // 0 is linker button, 1 is rechter button, ...  etc.
        {
            RaycastHit hit;

            Ray tast_straal = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(tast_straal, out hit, 100.0f)) //tast_straal kan ook vervangen worden door "Vector3 origin, Vector3 direction", 100.0f is aftast afstand
            {

                if (hit.transform != null) // != betekend 'is niet'
                {
                    Vector3 HitLocatieRay;
                    Vector3 PositieTargetCollider;
                    Vector3 ColliderScale;
                  

                    HitLocatieRay = hit.point;
                    PositieTargetCollider = hit.transform.position;
                    ColliderScale = hit.transform.localScale;


                   // Debug.Log(Bereken_Positie_Nieuwe_Cube(HitLocatieRay, PositieTargetCollider, ColliderScale));

                }
            }
        }
    }
   
    Vector3 Bereken_Positie_Nieuwe_Cube(Vector3 HitLocRay, Vector3 PosTarCol, Vector3 ColScale)
    {

    float HitLocRay_X = HitLocRay.x;
    float HitLocRay_Y = HitLocRay.y;
    float HitLocRay_Z = HitLocRay.z;
    float PosTarCol_X = PosTarCol.x;
    float PosTarCol_Y = PosTarCol.y;
    float PosTarCol_Z = PosTarCol.z;
    float ColScale_X = ColScale.x;
    float ColScale_Y = ColScale.y;
    float ColScale_Z = ColScale.z;


    Debug.Log(HitLocRay_X);

    if (HitLocRay_X == HitLocRay_Y)
        {
            return HitLocRay + PosTarCol + ColScale;
        }

    }     
    
}

I’ve taken out a lot of Debug.Log stuff and personal notes so the (47,13) does not refer to the right line of code in the above code posted. Now it would refer to (38,13).

Unity says:
Assets\MousePointScript.cs(47,13): error CS0161: ‘MousePointScript.Bereken_Positie_Nieuwe_Cube(Vector3, Vector3, Vector3)’: not all code paths return a value.

You have a method which returns a Vector3, the only point where it returns a value is withing the If Statement. What if that statement isn’t true? The method isn’t returning anything. So after that if state, return whatever you want to return if the statement isn’t true.

Also, you should never use == with floats. Use a range instead to account for floating point errors. Something like;

if(Mathf.Abs(HitLocRay_X - HitLocRay_Y) <= 0.01f)
1 Like

WarmedxMints is using 0.01f, but you could use Mathf.Epsilon there. It’s a tiny number that is used to account for float imprecision when comparing them for equality.

You could also use Mathf.Approximately which uses epsilon behind the scenes for you. It is designed specifically for comparing floats for approximate equality.

if (Mathf.Approximately(HitLocRay_X, HitLocRay_Y))

As WarmedxMints says, it’s telling you that there are paths in your code where you don’t return something, but it’s required because you’ve marked a return type.

So you need something like

if (Mathf.Approximately(HitLocRay_X, HitLocRay_Y)) {
 return HitLocRay + PosTarCol + ColScale;
}

//By default return a vector3 in case HitLocRay_X and HitLocRay_Y aren't equal
return anotherVector3;
1 Like

else
{
return new Vector3();
}