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));
}
unity3d math vector position
add a comment |
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));
}
unity3d math vector position
add a comment |
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));
}
unity3d math vector position
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
unity3d math vector position
edited Nov 10 at 22:01
Draco18s
10k31940
10k31940
asked Nov 10 at 21:50
Hagwill
227
227
add a comment |
add a comment |
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));
}
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
add a comment |
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));
}
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
add a comment |
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));
}
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
add a comment |
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));
}
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));
}
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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