Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by DOOManiac · Apr 15, 2017 at 08:14 AM · transform.positioneuleranglestransform.rotation

How to align & rotate objects to each other at run time?

I'm working on my first Unity project, and it is a simple pipe-based puzzle game. The idea is that you pick up sections of pipe to build a pipeline. If you have a curved piece, the player can rotate it to make the pipe go left, right, up, or down. To accomplish this, I have two child objects of my pipe that act as simple colliders - one for each end.

This snapping and rotation is the part I'm having trouble with. If the two object's natural origin aligns, then snapping works fine. If the origins do NOT align, however, things go crazy and the pipe will snap backwards, or inside the previous pipe, or sometimes even vertically.

Here is the code I'm using for snapping:

 void OnTriggerEnter(Collider other)
 {
     // NOTE: rb is the transform.parent's RigidBody component, set in Start() (not shown)
     rb.isKinematic = true;
 
 
     // This is doesn't work.
     //parent.transform.position = other.transform.parent.position;
     //parent.transform.rotation = other.transform.parent.rotation;
   
 
     // Figure out our rotation offset.
 // TODO: What if we want to rotate our pipe UP/LEFT/RIGHT/DOWN?
     Vector3 rotvector = other.transform.eulerAngles;
 
     // I think I need to do something with these, but can't figure it out
     //float direction = Mathf.Sign(transform.forward.z);
     //float pitch = Mathf.Sign(transform.forward.y);
     //float spin = Mathf.Sign(transform.forward.x);
 //Debug.Log("Direction: " + direction + " | Spin: " + spin);
 //Debug.Log("Forward: " + transform.forward);
     //Quaternion rotoffset = Quaternion.Euler(rotvector.x * direction, rotvector.y * pitch, rotvector.z * spin);
 
     Quaternion rotoffset = Quaternion.Euler(rotvector.x, rotvector.y, rotvector.z);
     parent.transform.rotation = rotoffset;
 
     // To figure out how much we need to move ourselves, we calculate the offset between our two snap points!
     // Always do this *after* our rotation, because otherwise our endpoints may move!
     Vector3 offset = transform.position - other.transform.position;
     parent.transform.position -= offset;
 }

I've tried several different combinations of using forward vectors and other things, but I just can't seem to figure this one out.

If you connect them AND their natural origins happen to line up, everything is fine: Snapping working correctly

In other cases, the snapping goes bad: Snapping behaving badly

Can anyone point me in the right direction as to how I am handling this wrong?

snap-good.png (114.6 kB)
snap-bad.png (184.0 kB)
Comment
Add comment
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

1 Reply

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

Answer by DOOManiac · Apr 16, 2017 at 08:11 PM

I ended up figuring out what I did wrong and fixing it! Mostly, I just threw away everything I had before and used a different approach. Instead of taking the player's current rotation and trying to make it fit, I just don't snap it unless it is already valid; it is up to the player to make any fine adjustments. (I do still snap to within 45 degrees, which feels about right and gives a healthy margin of error).

Here's the new and improved code:

     void OnTriggerEnter(Collider other)
     {
         // Snap our rotation to SNAP_AMOUNT degree increments
         Vector3 snapVector = Snap(parent.transform.rotation.eulerAngles, SNAP_AMOUNT);
         Quaternion snapRotate = Quaternion.Euler(snapVector);
         parent.transform.rotation = snapRotate;
 
         // To figure out how much we need to move ourselves, we calculate the offset between our two snap points!
         // Always do this *after* our rotation, because otherwise our endpoints may move!
         Vector3 offset = transform.position - other.transform.position;
         parent.transform.position -= offset;
 
         // If we are okay to make our snap permanent, highlight it in green and SNAP IT!
         Vector3 validSnapVector = Snap(parent.transform.rotation.eulerAngles, 90.0f);
         if (snapVector == validSnapVector)
         {
             Debug.Log("TODO: GREEN");
             pc.isSnapped = true;
         }
         else
         {
             Debug.Log("TODO: RED");
             return;
         }

     // More game specific logic here...
     }
 
     float Snap(float angle, float snapAmount)
     {
         return Mathf.Round(angle / snapAmount) * snapAmount;
     }
 
     Vector3 Snap(Vector3 vector, float snapAmount)
     {
         vector.x = Snap(vector.x, snapAmount);
         vector.y = Snap(vector.y, snapAmount);
         vector.z = Snap(vector.z, snapAmount);
 
         return vector;
     }
Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

trying to take my door script and apply to a treasure chest 0 Answers

Why the transfrom.eulerAngles keep reset to 0? 2 Answers

my prefab is null woe 2 Answers

Car rotations not being set properly 1 Answer

Resetting Ball Object to the original parent and position 2 Answers


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