Argon Assault movement

I’m new in coding at all. My question is about challenge “Move the player to the right”, + x offset. I just added + 1f to x value

   transform.localPosition = new Vector3
         (transform.localPosition.x + 1f,
          transform.localPosition.y,
          transform.localPosition.z);

and its working the same

   float xOffset = 1f;   
   float newXPos = transform.localPosition.x + xOffset;

   transform.localPosition = new Vector3
         (newXPos,
         transform.localPosition.y,
         transform.localPosition.z);

what such clarifications brings? Is it related to “easy make changes to code”, when you don’t need to browse all code to find another similar values?

Hi Umka,

Your solution is perfectly fine but, yes, we prefer additional variables to make it easier for us to change values in our code. In larger projects, we might have many different scripts and a lot of code. Instead of looking around for hard-coded values (such as + 1f), we would rather want to change the value of xOffset, so we do not have to read our code again. That’s a matter of personal preference, though. :slight_smile:


See also:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms