# Vector math resulting in huge or infinite values

I’m working on an infinite horizontal scroller where the entire map is divided into Segments of a uniform width. Every frame, I want to test each segment to see if it’s outside of the viewport, and if it is I want to teleport it ahead of the player. It’s hard to describe, but seems pretty trivial to implement:

``````public float segmentWidth; //X dimensions of a single map segment
public List<MapSegment> segmentList; //list of map segments in pool

void MapSegmentUpdate()
{

//Check if each section is on-screen, if not, move it left or right by a number of units equal to its width * the number of sections in the list
for (int i = 0; i < segmentList.Count; i++)
{
Vector3 mapVec;
mapVec = Camera.main.WorldToViewportPoint(segmentList*.gameObject.transform.position);*
``````

if (mapVec.x < 0) //Segment is left of camera
{
Vector3 targetPosition = segmentList*.transform.position; //Get current position of segment*
targetPosition.x = segmentList_.transform.position.x + (segmentWidth * segmentList.Count); //Offset X so current segment becomes rightmost in map_
segmentList*.transform.Translate(targetPosition);*
} else if (mapVec.x > 1) //Segment is right of camera
{
Vector3 targetPosition = segmentList*.transform.position;*
targetPosition.x = segmentList_.transform.position.x - (segmentWidth * segmentList.Count); //Offset X so current segment becomes leftmost in map_
segmentList*.transform.Translate(targetPosition);*
}
}
}
This looks right to me: the number of segments in a list times each segment’s width tells you how long the entire chain of segments is, and adding/subtracting that value from the current segment’s X coordinate should move it to the far left/right of the segments, but instead this is producing huge values- Y is always listed as “-Infinity”, and the X value is in the octillion or nonillion range. This makes me assume I’m slipping on a banana peel somewhere and accidentally multiplying my vectors exponentially, but no matter how many times I look at it, my math looks right to me- is there an obvious issue with my calculation?

Translate doesn’t set an absolute position but moves the object by a relative offset. Since you basically pass in the object position you will double the distance from the world origin each time you call this. You treat the position as offset vector.

You should do somthing like

``````Vector3 targetPosition = segmentList*.transform.position; //Get current position of segment*
``````

targetPosition.x += (segmentWidth * segmentList.Count); //Offset X so current segment becomes rightmost in map
segmentList*.transform.position = targetPosition;*
Or just as a one-liner:
segmentList_.transform.position += Vector3.right * (segmentWidth * segmentList.Count);_