Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by reineinmyheart · Oct 16, 2013 at 03:47 PM · instantiatequaternionrotate objecttransform.rotatewrapping

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Oct 16, 2013 at 04:16 PM 0
Share

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)?

avatar image Owen-Reynolds · Oct 16, 2013 at 04:38 PM 0
Share

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.

avatar image reineinmyheart · Oct 17, 2013 at 03:27 AM 0
Share

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.

avatar image reineinmyheart · Oct 17, 2013 at 03:29 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Oct 17, 2013 at 02:54 PM 0
Share

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.)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Drag to Rotate using Arcball theory ( 3 axis ) 0 Answers

Rotate object in the direction where it is going 1 Answer

How to turn a car like in real world? 1 Answer

Having trouble instantiating a gameobject with rotation 0 Answers

Rotation changing in untouched axis 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges