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
1
Question by Septem · Jul 01, 2011 at 06:56 AM · rotationraycasttransformnormal

Cant understand why object rotates when setting transform.up to normal

Hello!

I am using this script to find normal on mesh under cursor:

http://unity3d.com/support/documentation/ScriptReference/RaycastHit-barycentricCoordinate.html

so it gives me interpolatedNormal

and than I use following code to align my object( cube ) with surface's normal

gameObj.transform.up = interpolatedNormal;

Problem with this is that object rotates around y-axis while I move cursor over mesh and i just want him to be aligned with normal but preserve its rotation around y-axis. How can I achieve this? Any advice appreciated

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

2 Replies

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

Answer by Owen-Reynolds · Jul 01, 2011 at 05:42 PM

As you've noticed, setting "up" to a direction doesn't say which "spin" you should have (known as a degree of freedom.) The lookAt command has an optional 2nd input to account for this -- if you don't add it, the computer assumes you want that free spin to be as "up" as possible. Using transform.up= doesn't seem to have a good way to specify that free spin, so the computer accidentally bounces it around on you. You could try aiming it first, to trick "up" into giving a consistant free-spin:

 [untested]
 transform.LookAt(whereever it should be aimed);
 transform.up = theNormal;

 I don't like to fight with setting "up", so use this:

 // facing is the 0-360 "free" y-spin I want:
 transform.rotation = Quaternion.Euler(0,facing,0);
 // We are on flat ground, and spun. Now tilt with ground:
 //   compute tilt rotation:
 Quaternion tilt = Quaternion.FromToRotation(Vector3.up, norm);
 //   apply tilt rotation:
 transform.rotation = tilt*transform.rotation;
Comment
Add comment · Show 3 · 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 Septem · Jul 07, 2011 at 11:20 AM 0
Share

Thank you for your solution but i receive same results - rotation around y-axis. I try to study quaternions so I cant understand what is happening.

avatar image Septem · Jul 10, 2011 at 09:07 AM 0
Share

O$$anonymous$$. So problem solved by putting $$anonymous$$us before norm.

Quaternion tilt = Quaternion.FromToRotation(Vector3.up, -norm);

So there is no any rotation now around y-axis happening. But i dont understand why I should put this $$anonymous$$us there before norm.

avatar image Owen-Reynolds · Jul 10, 2011 at 04:59 PM 0
Share

I have to draw a picture. V3.up is straight up, and norm (the way the ground points) should be mostly up. FromToRot(up, norm) computes the amount and angle of tilt between them. If your normals are flipped so they face down, clearly -norm is the solution. Otherwise -- I know the math but I still sometimes change random +/-'s and flip multiplies until it works, and then figure out why.

avatar image
0

Answer by Waz · Jul 01, 2011 at 12:00 PM

Maybe what you want to do is:

 gameObj.transform.rotation = Quaternion.LookRotation(interpolatedNormal);

this will make him look out from the normal, but with his head towards the sky. Some variation of that sounds like what you want, but you may need to further modify it.

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 Septem · Jul 01, 2011 at 02:42 PM 0
Share

Thanks for reply it tried it but got same result - cube that is moving along surface is rotating but only difference that this time around z-axis since Quaternion.LookRotation makes "forward" look along vector that is passed to function. So now z-axis is pointing out from surface and cube rotates around it.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rotate raycast origin with gameobject 0 Answers

Unity Rotate Raycast on Quaternion 1 Answer

Automatically rotating objects to fit on to others 0 Answers

Align navmeshagent to ramp and rotate normally,Script is aligning to slopes but no longer rotating on other axis 0 Answers

How to place an object at the centre of another and move it along. 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