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 olas72 · Mar 27, 2013 at 07:53 AM · rotationquaternionnormalsnappingeuler

Problem snapping object to different normals, but maintaining x axis orientation

I'm working on an editor, where the player can move an object around the editor, snapping it to surface normal. The player can also rotate the object around its X axis. Whenever I try and snap it to the surface normal it matchs all the axis of the normal and I lose the facing that it had before the snap. I've tried a bunch of different things and I've had limited success. I'm obviously missing something.

 tempX=targetObject.transform.localRotation.eulerAngles.x;
 targetObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitNormal);
 targetObject.transform.Rotate(Vector3.up * -tempX);

 targetObject.transform.position    = mInputManager.hit.point;
Comment
Add comment · Show 22
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 Seth-Bergman · Mar 27, 2013 at 07:58 AM 0
Share

I fixed your code, next time be sure and format the code, by selecting it and click the "010101" button, or just indent it four spaces...

avatar image Seth-Bergman · Mar 27, 2013 at 08:00 AM 0
Share

I'm confused.. what is it you WANT to happen?

avatar image sparkzbarca · Mar 27, 2013 at 08:17 AM 0
Share

so you do you not want to randomly rotate but rotate the nearest face of the object then?

(his problem is basically I THIN$$anonymous$$ that he ends up with an object snapping to have it with a face parallel to another. but to do so he is basically just mimicking the object and so he completely flips objects all over. He wants the $$anonymous$$imum rotation needed to make an object have a face parallel to another) I THIN$$anonymous$$.

avatar image olas72 · Mar 27, 2013 at 08:18 AM 0
Share

alt text

So if I click on each of these points in turn, I want to snap the object to the ground and align the local y and z of the object to the normal of the surface and leave the X axis the same as it was.

rotation_example.jpg (134.0 kB)
avatar image sparkzbarca · Mar 27, 2013 at 08:26 AM 0
Share

is this for like a character to run up walls and stuff?

basically i'm thinking your actually wanting to keep the y axis the same not the other one.

x axis rotation would literally rotation the tip of the arrow through the ground or wall or whatever. rotating left and right is actually a rotaiton of the Y axis not the X.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by whydoidoit · Mar 27, 2013 at 09:13 AM

How about this?

   targetObject.transform.rotation *= Quaternion.FromToRotation(targetObject.transform.up, hitNormal);
Comment
Add comment · Show 14 · 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 Seth-Bergman · Mar 27, 2013 at 09:30 AM 0
Share

NICE! (it will need to be targetObject.transform.up though..)

  targetObject.transform.rotation *= Quaternion.FromToRotation(targetObject.transform.up, hitNormal);
avatar image whydoidoit · Mar 27, 2013 at 09:31 AM 0
Share

Good point - fixed.

avatar image sparkzbarca · Mar 27, 2013 at 09:46 AM 0
Share

yep it works as long as you never want to knock an object over on it's side and have it work. (I was trying to help him get a more robust solution that doesnt force you to always have objects upright, but it is quite a bit more work to do so, so perhaps just having multiple models with different "up" vectors is easier for him.

avatar image Seth-Bergman · Mar 27, 2013 at 09:51 AM 0
Share

you have a good idea, although I discounted that possibility off the bat, since it wasn't actually in the question

an easier way to account for an off-kilter object would be to parent it to an empty object that has the correct orientation :)

avatar image Seth-Bergman · Mar 27, 2013 at 10:10 AM 2
Share

let me suggest again, this will be $$anonymous$$UCH EASIER if you simply parent your object to an empty.. you can apply the x rotation to the child, then the rotation will simply stay the same, problem solved

Show more comments

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

14 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

Related Questions

Problem snapping object to different normals, but maintaining x axis orientation 0 Answers

Rotation with a slider 2 Answers

Is there no way to get reliable angles from a rigidbody? 2 Answers

I am having a weird issue with Quaternion.FromToRotation 2 Answers

How make child object rotate inverse to its parents rotation ? 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