- Home /
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? :/ It's pretty much impossible to check wrapping for every angle there could possibly be, right?. :/
Thank you so much for your patience.
Can you tell me how the wrapping is failing? Is the issue that the cube's diagonal is longer than the width, or are you seeing something more extreme? You suggest that each asteroid is a collection of cubes. If so, where is the pivot and are you rotating the whole collection (i.e. is this code on a parent and the cubes are children)?
The wrap-around code doesn't use the angle anywhere, so probably works fine but looks odd. Try it with sphere asteriods (or a sphere with a single cube bump.) That way you can distinguish the mere fact of being rotated, from the "look" of rotation on the cubes.
Hello sir robertbu, the wrapping fails in a sense that, the object's position doesn't match the borders of my game anymore, so upon reaching them they just go through, which didn't happen until I placed some random rotation. Yes, the cubes are children of an empty game object which is located somewhere at the center of the cubes :)and the code is placed on the parent.
Hi sir owen, before using the cubes object, I was just using spheres as asteroids, and they really worked fine, didn't have much problems with it. Although I have never tried rotating them then, I would probably try that. Thank you for suggesting.
Answer by reineinmyheart · Oct 17, 2013 at 05:21 AM
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 :D
In general 3D modelling, an object has to count as being at exactly one xyz point. Say you model an apple with 000 at the bottom. If you say apple.transform = tableTopA;
then the apple will be on the table. But if you sommersault spin the apple, it will spin around the bottom (into the table.)
If you ins$$anonymous$$d make the apple with 000 in the center, you now can easily spin it around the center, but if you place it at tableTopA, it will be halfway into the table (you have to add 1/2 the height when you place it.)
By mistake, you sometimes make an apple where 000 is far below it, etc... , which is what happened to the asteroid.
Unity s$$anonymous$$ls that rule (it has to, plus everyone knows it.) To see this, place a tall thin cube at 000 (as a marker) and set the object xyz to 000. See which way it hangs. Spin it using the rotation tool. If you have parent/child stuff, try clicking the local/center/world buttons until it shows the exact position of each (ins$$anonymous$$d of an average.)