What makes this code work with the Y axis but not the X axis?











up vote
0
down vote

favorite












So I am making a game with a blink function and a fixed camera so of course I don't want the player to be able to leave the camera. So my solution is to not enable the player to pass the border of the camera with the blink. The thing is that it works amazingly for the Y axis, but less so for the X axis. (The transform.lossy scale is a place holder until I find an actual sprite & the magic numbers are the position at which the camera's edges are)



if ((transform.position += (Vector3)aim * slashDistance).x <= -8.9f)
{
transform.position += (Vector3)aim * (8.9f + transform.position.x - (transform.lossyScale.x / 2));
}
else if ((transform.position += (Vector3)aim * slashDistance).x >= 8.9f)
{
transform.position += (Vector3)aim * (8.9f - transform.position.x - (transform.lossyScale.x / 2));
}

if ((transform.position += (Vector3)aim * slashDistance).y <= -5f)
{
transform.position += (Vector3)aim * (5f + transform.position.y - (transform.lossyScale.y / 2));
}
else if ((transform.position += (Vector3)aim * slashDistance).y >= 5f)
{
transform.position += (Vector3)aim * (5f - transform.position.y - (transform.lossyScale.y / 2));
}









share|improve this question




























    up vote
    0
    down vote

    favorite












    So I am making a game with a blink function and a fixed camera so of course I don't want the player to be able to leave the camera. So my solution is to not enable the player to pass the border of the camera with the blink. The thing is that it works amazingly for the Y axis, but less so for the X axis. (The transform.lossy scale is a place holder until I find an actual sprite & the magic numbers are the position at which the camera's edges are)



    if ((transform.position += (Vector3)aim * slashDistance).x <= -8.9f)
    {
    transform.position += (Vector3)aim * (8.9f + transform.position.x - (transform.lossyScale.x / 2));
    }
    else if ((transform.position += (Vector3)aim * slashDistance).x >= 8.9f)
    {
    transform.position += (Vector3)aim * (8.9f - transform.position.x - (transform.lossyScale.x / 2));
    }

    if ((transform.position += (Vector3)aim * slashDistance).y <= -5f)
    {
    transform.position += (Vector3)aim * (5f + transform.position.y - (transform.lossyScale.y / 2));
    }
    else if ((transform.position += (Vector3)aim * slashDistance).y >= 5f)
    {
    transform.position += (Vector3)aim * (5f - transform.position.y - (transform.lossyScale.y / 2));
    }









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      So I am making a game with a blink function and a fixed camera so of course I don't want the player to be able to leave the camera. So my solution is to not enable the player to pass the border of the camera with the blink. The thing is that it works amazingly for the Y axis, but less so for the X axis. (The transform.lossy scale is a place holder until I find an actual sprite & the magic numbers are the position at which the camera's edges are)



      if ((transform.position += (Vector3)aim * slashDistance).x <= -8.9f)
      {
      transform.position += (Vector3)aim * (8.9f + transform.position.x - (transform.lossyScale.x / 2));
      }
      else if ((transform.position += (Vector3)aim * slashDistance).x >= 8.9f)
      {
      transform.position += (Vector3)aim * (8.9f - transform.position.x - (transform.lossyScale.x / 2));
      }

      if ((transform.position += (Vector3)aim * slashDistance).y <= -5f)
      {
      transform.position += (Vector3)aim * (5f + transform.position.y - (transform.lossyScale.y / 2));
      }
      else if ((transform.position += (Vector3)aim * slashDistance).y >= 5f)
      {
      transform.position += (Vector3)aim * (5f - transform.position.y - (transform.lossyScale.y / 2));
      }









      share|improve this question















      So I am making a game with a blink function and a fixed camera so of course I don't want the player to be able to leave the camera. So my solution is to not enable the player to pass the border of the camera with the blink. The thing is that it works amazingly for the Y axis, but less so for the X axis. (The transform.lossy scale is a place holder until I find an actual sprite & the magic numbers are the position at which the camera's edges are)



      if ((transform.position += (Vector3)aim * slashDistance).x <= -8.9f)
      {
      transform.position += (Vector3)aim * (8.9f + transform.position.x - (transform.lossyScale.x / 2));
      }
      else if ((transform.position += (Vector3)aim * slashDistance).x >= 8.9f)
      {
      transform.position += (Vector3)aim * (8.9f - transform.position.x - (transform.lossyScale.x / 2));
      }

      if ((transform.position += (Vector3)aim * slashDistance).y <= -5f)
      {
      transform.position += (Vector3)aim * (5f + transform.position.y - (transform.lossyScale.y / 2));
      }
      else if ((transform.position += (Vector3)aim * slashDistance).y >= 5f)
      {
      transform.position += (Vector3)aim * (5f - transform.position.y - (transform.lossyScale.y / 2));
      }






      unity3d math vector position






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 22:01









      Draco18s

      10k31940




      10k31940










      asked Nov 10 at 21:50









      Hagwill

      227




      227
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          You have this statement in your if conditions:



          transform.position += (Vector3)aim * slashDistance


          This statement has a side-effect - it will move the player. Hence, you will move the player each time any of the four conditions is evaluated. Not very reasonable. So, when you move outside of the allowed x-range, the first block will move the player back inside. But then, the second block moves the player again outside of the range and the y-check will not be able to compensate this. So, simply put the effect outside of your conditions. This will also make your code much easier to read:



          transform.position += (Vector3)aim * slashDistance
          if (transform.position.x <= -8.9f)
          {
          transform.position += (Vector3)aim * (8.9f + transform.position.x - (transform.lossyScale.x / 2));
          }
          else if (transform.position.x >= 8.9f)
          {
          transform.position += (Vector3)aim * (8.9f - transform.position.x - (transform.lossyScale.x / 2));
          }

          if (transform.position.y <= -5f)
          {
          transform.position += (Vector3)aim * (5f + transform.position.y - (transform.lossyScale.y / 2));
          }
          else if (transform.position.y >= 5f)
          {
          transform.position += (Vector3)aim * (5f - transform.position.y - (transform.lossyScale.y / 2));
          }





          share|improve this answer





















          • Cheers @NicoSchertler, a year and a half into programming and I didn't know that the code in an if can affect variables. I feel like a complete novice right now. Thanks for the help mate! And also if you do know why the original code works flawlessly on the Y axis but not on the X axis, please let me know.
            – Hagwill
            Nov 11 at 9:28








          • 1




            Because the y-axis code comes last and there is nothing after it that might revert the effect.
            – Nico Schertler
            Nov 11 at 15:42











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243750%2fwhat-makes-this-code-work-with-the-y-axis-but-not-the-x-axis%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          You have this statement in your if conditions:



          transform.position += (Vector3)aim * slashDistance


          This statement has a side-effect - it will move the player. Hence, you will move the player each time any of the four conditions is evaluated. Not very reasonable. So, when you move outside of the allowed x-range, the first block will move the player back inside. But then, the second block moves the player again outside of the range and the y-check will not be able to compensate this. So, simply put the effect outside of your conditions. This will also make your code much easier to read:



          transform.position += (Vector3)aim * slashDistance
          if (transform.position.x <= -8.9f)
          {
          transform.position += (Vector3)aim * (8.9f + transform.position.x - (transform.lossyScale.x / 2));
          }
          else if (transform.position.x >= 8.9f)
          {
          transform.position += (Vector3)aim * (8.9f - transform.position.x - (transform.lossyScale.x / 2));
          }

          if (transform.position.y <= -5f)
          {
          transform.position += (Vector3)aim * (5f + transform.position.y - (transform.lossyScale.y / 2));
          }
          else if (transform.position.y >= 5f)
          {
          transform.position += (Vector3)aim * (5f - transform.position.y - (transform.lossyScale.y / 2));
          }





          share|improve this answer





















          • Cheers @NicoSchertler, a year and a half into programming and I didn't know that the code in an if can affect variables. I feel like a complete novice right now. Thanks for the help mate! And also if you do know why the original code works flawlessly on the Y axis but not on the X axis, please let me know.
            – Hagwill
            Nov 11 at 9:28








          • 1




            Because the y-axis code comes last and there is nothing after it that might revert the effect.
            – Nico Schertler
            Nov 11 at 15:42















          up vote
          2
          down vote



          accepted










          You have this statement in your if conditions:



          transform.position += (Vector3)aim * slashDistance


          This statement has a side-effect - it will move the player. Hence, you will move the player each time any of the four conditions is evaluated. Not very reasonable. So, when you move outside of the allowed x-range, the first block will move the player back inside. But then, the second block moves the player again outside of the range and the y-check will not be able to compensate this. So, simply put the effect outside of your conditions. This will also make your code much easier to read:



          transform.position += (Vector3)aim * slashDistance
          if (transform.position.x <= -8.9f)
          {
          transform.position += (Vector3)aim * (8.9f + transform.position.x - (transform.lossyScale.x / 2));
          }
          else if (transform.position.x >= 8.9f)
          {
          transform.position += (Vector3)aim * (8.9f - transform.position.x - (transform.lossyScale.x / 2));
          }

          if (transform.position.y <= -5f)
          {
          transform.position += (Vector3)aim * (5f + transform.position.y - (transform.lossyScale.y / 2));
          }
          else if (transform.position.y >= 5f)
          {
          transform.position += (Vector3)aim * (5f - transform.position.y - (transform.lossyScale.y / 2));
          }





          share|improve this answer





















          • Cheers @NicoSchertler, a year and a half into programming and I didn't know that the code in an if can affect variables. I feel like a complete novice right now. Thanks for the help mate! And also if you do know why the original code works flawlessly on the Y axis but not on the X axis, please let me know.
            – Hagwill
            Nov 11 at 9:28








          • 1




            Because the y-axis code comes last and there is nothing after it that might revert the effect.
            – Nico Schertler
            Nov 11 at 15:42













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          You have this statement in your if conditions:



          transform.position += (Vector3)aim * slashDistance


          This statement has a side-effect - it will move the player. Hence, you will move the player each time any of the four conditions is evaluated. Not very reasonable. So, when you move outside of the allowed x-range, the first block will move the player back inside. But then, the second block moves the player again outside of the range and the y-check will not be able to compensate this. So, simply put the effect outside of your conditions. This will also make your code much easier to read:



          transform.position += (Vector3)aim * slashDistance
          if (transform.position.x <= -8.9f)
          {
          transform.position += (Vector3)aim * (8.9f + transform.position.x - (transform.lossyScale.x / 2));
          }
          else if (transform.position.x >= 8.9f)
          {
          transform.position += (Vector3)aim * (8.9f - transform.position.x - (transform.lossyScale.x / 2));
          }

          if (transform.position.y <= -5f)
          {
          transform.position += (Vector3)aim * (5f + transform.position.y - (transform.lossyScale.y / 2));
          }
          else if (transform.position.y >= 5f)
          {
          transform.position += (Vector3)aim * (5f - transform.position.y - (transform.lossyScale.y / 2));
          }





          share|improve this answer












          You have this statement in your if conditions:



          transform.position += (Vector3)aim * slashDistance


          This statement has a side-effect - it will move the player. Hence, you will move the player each time any of the four conditions is evaluated. Not very reasonable. So, when you move outside of the allowed x-range, the first block will move the player back inside. But then, the second block moves the player again outside of the range and the y-check will not be able to compensate this. So, simply put the effect outside of your conditions. This will also make your code much easier to read:



          transform.position += (Vector3)aim * slashDistance
          if (transform.position.x <= -8.9f)
          {
          transform.position += (Vector3)aim * (8.9f + transform.position.x - (transform.lossyScale.x / 2));
          }
          else if (transform.position.x >= 8.9f)
          {
          transform.position += (Vector3)aim * (8.9f - transform.position.x - (transform.lossyScale.x / 2));
          }

          if (transform.position.y <= -5f)
          {
          transform.position += (Vector3)aim * (5f + transform.position.y - (transform.lossyScale.y / 2));
          }
          else if (transform.position.y >= 5f)
          {
          transform.position += (Vector3)aim * (5f - transform.position.y - (transform.lossyScale.y / 2));
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 8:16









          Nico Schertler

          25k42350




          25k42350












          • Cheers @NicoSchertler, a year and a half into programming and I didn't know that the code in an if can affect variables. I feel like a complete novice right now. Thanks for the help mate! And also if you do know why the original code works flawlessly on the Y axis but not on the X axis, please let me know.
            – Hagwill
            Nov 11 at 9:28








          • 1




            Because the y-axis code comes last and there is nothing after it that might revert the effect.
            – Nico Schertler
            Nov 11 at 15:42


















          • Cheers @NicoSchertler, a year and a half into programming and I didn't know that the code in an if can affect variables. I feel like a complete novice right now. Thanks for the help mate! And also if you do know why the original code works flawlessly on the Y axis but not on the X axis, please let me know.
            – Hagwill
            Nov 11 at 9:28








          • 1




            Because the y-axis code comes last and there is nothing after it that might revert the effect.
            – Nico Schertler
            Nov 11 at 15:42
















          Cheers @NicoSchertler, a year and a half into programming and I didn't know that the code in an if can affect variables. I feel like a complete novice right now. Thanks for the help mate! And also if you do know why the original code works flawlessly on the Y axis but not on the X axis, please let me know.
          – Hagwill
          Nov 11 at 9:28






          Cheers @NicoSchertler, a year and a half into programming and I didn't know that the code in an if can affect variables. I feel like a complete novice right now. Thanks for the help mate! And also if you do know why the original code works flawlessly on the Y axis but not on the X axis, please let me know.
          – Hagwill
          Nov 11 at 9:28






          1




          1




          Because the y-axis code comes last and there is nothing after it that might revert the effect.
          – Nico Schertler
          Nov 11 at 15:42




          Because the y-axis code comes last and there is nothing after it that might revert the effect.
          – Nico Schertler
          Nov 11 at 15:42


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243750%2fwhat-makes-this-code-work-with-the-y-axis-but-not-the-x-axis%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues