Transform.rotation vs Wrapping

Hi, I’m doing a remake of the 2D Asteroids game. Currently, I did a single sprite(technically just strips of cubes put together through parenting) for all of my asteroids, so all of my asteroids look the same. To make it look a little more interesting, I decided to have a random rotation on their y-axis (my objects only move in the x and z axes as it’s 2D), every time it’s instantiated.

So in my asteroids script, I have something like this:

  void Start () {
        MyTransform.rotation = Quaternion.Euler(0.0f, Random.Range(0, 360), 0.0f);
        rigidbody.AddForce(Random.insideUnitCircle * 200); //create random direction movement
    }

So, I got what I wanted, they are now facing different directions upon instantiation. However, they no longer follow my wrapping script, which has the following code:

void Update () {
		if(myTransform.position.x >= maxX){
			myTransform.position = new Vector3(minX, myTransform.position.y, myTransform.position.z);
		}
		else if(myTransform.position.x <= minX){
			myTransform.position = new Vector3(maxX, myTransform.position.y, myTransform.position.z);
		}
		else if(myTransform.position.z >= maxZ){
			myTransform.position = new Vector3(myTransform.position.x, myTransform.position.y, minZ);		
		}
		else if(myTransform.position.z <= minZ){
			myTransform.position = new Vector3(myTransform.position.x, myTransform.position.y, maxZ);
		}
	}

I’m guessing this is because, I rotated its transform, therefore, I also rotated the object’s axes, whereas the world’s orientation remained untouched. Just a wild guess though.

Is there a way that I could probably solve this? :confused: It’s pretty much impossible to check wrapping for every angle there could possibly be, right?. :confused:

Thank you so much for your patience.

I got what I needed! Thank you to sir robertbu and sir owen reynolds for giving me an idea on what I might’ve been doing wrong.

Apparently, I was doing something wrong with the parenting in the first place. I placed my empty game object at the origin position, and made the collection of cubes its children, although they weren’t at the origin as well, and somewhere far from the empty gameobject.
Upon doing that, the transform became different in a way that if my border is at x = 25, and I drag the object towards that position in the scene view, the inspector panel tells me that the object is somewhere in x = 10 or something like that, although the scene view shows that they’re actually in the same position.

Is that what this hint in the unity docs imply?
“When parenting Transforms, put the parent’s location at 0,0,0 before applying the child. This will save you many headaches later.”

Doesn’t make sense for me, or something lacks in this line, such as making sure that the center of the object is in the same place of the supposed parent. I don’t know. I’m kind of confused. Would care for some enlightenment.

And thank you for the ideas as well. Hope to get a good grasp of Unity soon, excited to make more games :smiley: