rectTransform.position = new Vector3 (0, 0, 0); not working [SOLVED]

yo, I try to make a seamless background of stars moving, and I’ve got a script which does make my two star backgrounds move. Now the last problem is just to get the images to return back to a position outside of the camera view that will make the movement seamless and eternal. The code I’m using doesn’t work, however.

rectTransform.position = new Vector3 (1150, 0, 0);

When I test it in game, the x-values just continues to grow in the x-negatives without returning home.

If you want to see the script, here it is, but please note that the RectTransform is given the appropriate reference in the Unity editor, and the “end” and “speed” values are given their values there as well.

I’ve set the “end” value to -1150, and speed is normally -1, but for quick testing purposes I often use -10 or -15.

using UnityEngine;
using System.Collections;

public class outer_space_movement : MonoBehaviour {

    public Vector3 speed;
    public RectTransform outer_space;

    public Vector3 end;

    // Update is called once per frame
    void Update ()
    {
        outer_space.position = outer_space.position + speed;
        return_to_start();
    }

    void return_to_start()
    {
       if (outer_space.position.x <= end.x)
        {
            outer_space.position = new Vector3 (1150, 0, 0);
        }

    }
}

localPosition maybe?

nope, still doesn’t work

You’re going to have a bad time with this:

if(outer_space.position== end)

Why not just check to see if the x value goes below the minimum value? You should basically never use == in this kind of situation.

Already changed it, and not working :frowning:

Add some debug statements in then? Also, since it’s a RectTransform you might want to set the anchoredPosition.

1 Like

So, okay, it works, but weird things happen.

With .position, it jumps in Y-value, which makes no sense what-so-ever. If I use .localPosition or .anchoredposition3D or ancoredposition, it doesn’t take it to 1150 x-value before way closer to ~-1700 x-value instead of at -1150 x-value, so somehow a value from god-knows-where is interfering.

NOTE: the value seems to be strangely close to the sum of 1150 + the height which is 540 (1150+540 = 1690). That connection is so illogical however I have to assume it’s a coincidence.

If you mean bug reports from Mono then there are none.

No, I mean using Debug.Log to see what the values are, there’s no point in guessing.

The reason why your values are changing is because the canvas scale changes how positions are handled. If this is just for a starfield then you really shouldn’t be using the canvas system, and you should use normal SpriteRenderers, as those aren’t subject to special positions.

I’m not guessing, if you click on the gameobjects while in game mode you see their values as they change.

But I worked it out, turned out that Unity didn’t like me having anchor preset in the center and pivot set to 0.5, so I had to put them in the top left and pivot to 0.