Vector3 more than one x float

Hello, I’m beginner at scripting and I have a problem. Is there any way to shrink this code? Because it looks too big. There’s just need to make Vector3 x float from 0 to 3, but I don’t know how xD Thanks for helping :slight_smile:

if(transform.position == new Vector3(0,0,0) && Input.GetButtonDown("right"))
        {
            endPos = new Vector3(transform.position.x, transform.position.y ,transform.position.z);
        }
        if(transform.position == new Vector3(1,0,0) && Input.GetButtonDown("right"))
        {
            endPos = new Vector3(transform.position.x, transform.position.y ,transform.position.z);
        }
        if(transform.position == new Vector3(2,0,0) && Input.GetButtonDown("right"))
        {
            endPos = new Vector3(transform.position.x, transform.position.y ,transform.position.z);
        }
        if(transform.position == new Vector3(3,0,0) && Input.GetButtonDown("right"))
        {
            endPos = new Vector3(transform.position.x, transform.position.y ,transform.position.z);
        }
For (int CurrX=0; CurrX<4; CurrX++)
{
if (transform.position == New Vector3(CurrX, 0.0f, 0.0f) && Input.GetbuttonDown("down"))
{
endPos =transform.position;
}
}

Before I get to the helping, a couple of forum etiquette notes:

  1. Use code tags , it makes everything more legible
  2. If you’re copying from MonoDevelop on Mac, it has a habit of removing spaces from code. Use the “Paste and Match Style” paste function instead - it’ll paste correctly.
A general note - you should almost never use == to compare Vector3's, or anything that is based on floats. Floats are imprecise, and stuff like (1f + 1f == 2f) will not always necessarily return true (it may be 1.999999f or 2.0000001f - this is expected behavior for floats. So comparing your position using == is asking for trouble down the line. Check for distance, use Mathf.Approximately, or use ranges instead, or just do math directly on the float in question.

And apparently your main question has already been answered, so I'll leave it at that.

Not sure if left as an excercise to the reader, but your code has a few errors in it: “for” should be all lowercase, as should “new”. “GetButtonDown” is written with an uppercase “B”. I believe the “Down” axis should be with an uppercase “D” (not sure on that.)

And performance wise, there should be a “break” statement at the end of the “if” block.

Oh yeah, and the thing about comparing floats, of course.

1 Like

Certainly comparing floats in that way is problematic, but his question wasn’t about fixing his code, it was about making it smaller. Glad that you guys mentioned it though, so that he is aware of it.

Performance wise a break is good, yeah. He is not using elseif though, so that would change the logic of his code. I am just trying to introduce him to a for loop without overwhelming him.

As for syntax, you are probably right, I always wing it and I need my intellisense. I don’t know any of that stuff off by heart, I use too many languages lol I’m sure he can navigate around my pseudo code.

Looking at it again, I think you might have to explicitly cast CurrX to a float though. Not sure.

if (transform.position == New Vector3((float)CurrX, 0.0f, 0.0f) && Input.GetbuttonDown("down"))

Thanks for answers guys, it helped me a lot:)