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 Daniel-Everland · Apr 25, 2014 at 02:17 PM · c#rotationaxisalign two objects

Get localScale depending on rotation

This is a folow up question to an answer given to me in the following thread - http://answers.unity3d.com/questions/683911/calculate-distance-between-any-objects-with-unknow.html

What I'm currently able to do with my game is align Object A (Transparent cube) so it's right next to Object B (Solid cube) regardless of the scale of Object A, however, this doesn't work if you rotate Object A. I don't know how to add rotation to my current calculation. Here's the code I'm using

 //Move cubeghost next to buildingblock we hit
 CubeGhost.transform.position = hit.transform.position + hit.normal;
 
 //Move cubeghost depending on scale of buildingblock we hit
 CubeGhost.transform.position -= Vector3.Scale(CubeGhost.transform.TransformPoint(hit.transform.position), hit.transform.localScale / 2);
 
 //Move cubeghost depending on scale of cubeghost
 CubeGhost.transform.position += Vector3.Scale(CubeGhost.transform.TransformPoint(hit.transform.position), CubeGhost.transform.localScale);

I'm using Vector3.Scale to get the correct axis I'm working with. So instead of returning (1, 0.5, 0.3) it will return (0, 0, 0.3) if I'm working with the Z axis.

If you still aren't quite sure what the issue is, feel free to try it out for yourself here: https://dl.dropboxusercontent.com/u/40950257/VoxelSpaceThingy/VoxelSpaceThingy.html

Here are some steps you can follow to get a visual example

  1. Move your cursor onto the three available faces of the solid cube (Object B).

  2. Notice even if Object A turns red, it will still be perfectly aligned with Object B

  3. Press Shift + Q

  4. Repeat Step 1

  5. Notice that Object A will now clip into/move away from Object B as rotation has not been considered.

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
1
Best Answer

Answer by Scribe · Apr 25, 2014 at 05:42 PM

Hey there,

You can use Transform.InverseTransformDirection to get the scale into the local sizes, something like:

 var norm = hit.normal;
 norm = norm.normalized;
 var locScale = CubeGhost.transform.InverseTransformDirection(CubeGhost.transform.localScale*0.5);
 locScale = Vector3(Mathf.Abs(locScale.x), Mathf.Abs(locScale.y), Mathf.Abs(locScale.z));
 var off = Vector3.Scale(locScale, norm);
 norm = Vector3.Scale(norm, norm);
 
 var pos = hit.transform.position + Vector3.Scale(hit.point-hit.transform.position, norm);
 CubeGhost.transform.position = pos + off;

That should position your object based on both objects rotations.

Nice skybox btw!

EDIT:

You can also do this with collider bounds as I suggested in a post on your last question, this is also less likely to fail if you start using parents and children where CubeGhost.transform.localScale can be incorrect unless you take the parent into account.

 var norm = hit.normal;
 norm = norm.normalized;
 
 var min = CubeGhost.collider.bounds.min;
 var max = CubeGhost.collider.bounds.max;
 var off = norm == Vector3.Scale(norm, norm).normalized ? min : max;
 off -= CubeGhost.transform.position;
 norm = Vector3.Scale(norm, norm).normalized;
 off = Vector3.Scale(off, norm);
 
 var pos = hit.transform.position + Vector3.Scale(hit.point-hit.transform.position, norm);
 CubeGhost.transform.position = pos - off;


Scribe

Comment
Add comment · Show 2 · 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 Daniel-Everland · Apr 26, 2014 at 01:22 PM 0
Share

Oh wow, I completely misunderstood how collider bounds works, this is amazing. Can't thank you enough :)

avatar image Scribe · Apr 26, 2014 at 02:25 PM 0
Share

Glad I could help :) thanks for marking as answered!

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

rotate axis of movement 0 Answers

Rotating a specific axis towards a object 3 Answers

How to fix rotation while moving character 0 Answers

Make object only rotate in 90 degree increments 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