- Home /
Transform.position assign attempt not valid, and position is infinity on an object?
I'm working on a game mechanic where to objects move with the player with one being normal, and the other being the opposite. I have the normal part functioning fine, but when I tried to do the opposite in code it's giving me this weird error as my object flies off the screen.
"transform.position assign attempt for "Blue Box" is not valid. Input position is {-Infinity, -28888778970870709809, 454534534534534534543}"
Any idea what is going wrong? Is my opposite movement code wrong?
if (moving)
{
if (isRed)
{
targetPosition = player.transform.position + startPosition; // Red Box (Normal) **These two lines work fine**
transform.position += (targetPosition - transform.position);
}
else
{
targetPosition = player.transform.position + startPosition; // Blue Box (Opposite) ** These two lines send object flying off screen with error **
transform.position += (targetPosition + transform.position);
}
}
else
{
startPosition = transform.position - player.transform.position; // Resting
transform.position = player.transform.position + startPosition;
}
}
That code is equivalent to
if (moving)
{
if (isRed)
{
targetPosition = player.transform.position + startPosition; // Red Box (Normal)
transform.position = targetPosition - transform.position + transform.position;
}
else
{
targetPosition = player.transform.position + startPosition; // Blue Box (Opposite)
transform.position = targetPosition + transform.position + transform.position;
}
}
else
{
startPosition = transform.position - player.transform.position; // Resting
transform.position = player.transform.position + startPosition;
}
Note that you're adding the transform.position to itself.
Try switching the +=
to =
That doesn't seem to work. All that happens after that is the blue square starts flashing quickly in two different places like it wants to be in both places at the same time. And the direction I walk doesn't make the square go in the opposite direction.
Anyone else know how I can achieve this?
I think the bigger problem is that you're not sure how any of this works.
If I'm reading right, I can give you that that the correct code is
if (moving)
{
if (isRed)
{
targetPosition = player.transform.position + startPosition; // Red Box (Normal)
}
else
{
targetPosition = player.transform.position - startPosition; // Blue Box (Opposite)
}
transform.position = targetPosition;
}
else
{
// Do nothing
// Ins$$anonymous$$d, set startPosition somewhere else.
}
but you need to figure out how that works
Answer by Ermiq · Aug 15, 2018 at 09:51 PM
The problem is you're trying to set a direction vector targetPosition - transform.position
as a position vector. Vectors in Unity can represent several things. They can be a position point in 3D space, and they can be a direction pointing to a specific direction from some position in 3D space. When you + or - two position vectors you'll get a direction vector. targetPosition + transform.position
returns direction from targetPosition
to transform.position
. targetPosition - transform.position
returns direction from transform.position
to targetPosition
. So, in your code for isRed
Unity is trying to set cube position to its current position and then move it in the direction from its current position to target position. It seems like it works as intended, but it's not actually. Make cube's original position farther from the target and you'll see the cube jittering between these two positions. In second part for !isRed
you place the cube on the position and set a direction which points 'from' the target, so the cube is going away from the scene. To make it work, you need to determine a distance which you want your cubes to be from the target and use something like this:
float distance = 1f;
if (moving)
{
if (isRed)
{
// posiition for a cube which is going to be in 1 unit in front of player
// player's position plus a direction vector forward from player multiplied by a distance
targetPosition = player.transform.position + player.transform.forward * distance;
transform.position = targetPosition;
}
else
{
// the same for another cube but a direction should be back from player
targetPosition = player.transform.position + player.transform.back * distance;
// or if there's no back vector
// targetPosition = player.transform.position + player.transform.forward * -distance;
// or another way
// targetPosition = player.transform.position - player.transform.forward * distance;
transform.position = targetPosition;
}
}
I'm getting an error in the !isRed section which is saying there is no .back definition for back on Transform. Tried putting .forward with - front but it just caused the cubes to crash into each other.
I also I commented out the else{} just to test the red square and no matter what it starts it right inside the player as soon as I begin to move. I even increased the distance with no luck.
Is it a 2D? Oh,yes< I see now. For 2D your forward vector will be player.transform.right
(X axis) or player.transform.up
(Y axis). Depends on your screen orientation. With using forward
cubes just going "into the screen" on Z axis which is not visible in 2d. So, just try right
or up
and it will work.
Hey I feel like your code has got me super close, but the only problem is that the blue square does not go in the opposite direction of the player. It moves in lock step with the player like the red square. Here is the code I have adjusted from before that you posted. Thanks for the help by the way, your explanations have made things a whole lot clearer.
if (moving)
{
if (isRed)
{
// posiition for a cube which is going to be in 1 unit in front of player
// player's position plus a direction vector forward from player multiplied by a distance
targetPosition = player.transform.position + player.transform.right * distance;
transform.position = targetPosition;
}
else
{
// the same for another cube but a direction should be back from player
targetPosition = player.transform.position + player.transform.right * -distance;
// or if there's no back vector
// targetPosition = player.transform.position + player.transform.forward * -distance;
// or another way
// targetPosition = player.transform.position - player.transform.forward * distance;
transform.position = targetPosition;
}
}
Only major changes were .forward and .back to .right and -distance in the else{} clause. Any idea on what to do for the blue square to go in the opposite direction of the player?