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
2
Question by 2dkot · Sep 08, 2011 at 09:34 PM · charactercontrollerquaternioncharacter.move

How to combine Quaternions?

I try to rotate object according normal rotation of terrain. More over this object should move. And I need to combine z and x from normal of terrain and y which rotates to target. I tried this but result is quiet weird:

 function getTerrainRot(myTarget:Vector3,terrPos: Vector3,modelRot:Quaternion){
 var terrHit: RaycastHit;
 var dirQuat: Quaternion;
 var newPos: Vector3;
 if(Physics.Raycast (terrPos, -Vector3.up,terrHit, 10.0))
 {    
         newPos = getPosAndTerrainY(myTarget) - terrPos;
         normal_q = Quaternion.FromToRotation(Vector3.up, terrHit.normal);
         terr_q = normal_q * Quaternion.AngleAxis(modelRot.eulerAngles.x, terrHit.normal);

         dirQuat = Quaternion.LookRotation(newPos);
         terr_q = Quaternion(terr_q.x,dirQuat.y,terr_q.z,dirQuat.w);

 }
 return Quaternion.Slerp(modelRot, terr_q, Time.deltaTime);
 }

Thanks

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 aldonaletto · Sep 09, 2011 at 04:31 AM

You're having weird results because you've messed with the components of a Quaternion - despite their names, they have nothing to do with the axes x, y and z.
If you want the character to keep looking to newPos direction while following the surface normal, you can use LookRotation(newPos, normal) - the second parameter indicates the up direction. About the surface normal: it's better to keep a curNormal variable and Lerp it to the surface normal (or Vector3.up, if no terrain hit) to smooth the character orientation:

var curNormal = Vector3.up;

function getTerrainRot(myTarget:Vector3,terrPos: Vector3): Quaternion; { var terrHit: RaycastHit; var newPos: Vector3; var normal = Vector3.up; if(Physics.Raycast (terrPos, -curNormal, terrHit, 10.0)){ normal = terrHit.normal; } curNormal = Vector3.Lerp(curNormal, normal, 2*Time.deltaTime);
newPos = getPosAndTerrainY(myTarget) - terrPos; return Quaternion.LookRotation(newPos, curNormal); }

EDITED: You're right: LookRotation in this case doesn't work as expected - it prefers to look at the target direction and completely ignores the normal!
Let's go back to the basics: calculate a rotation from Vector3.up to curNormal (as you already did), than multiply by the horizontal rotation from Vector3.forward to the target direction (multiplication in the Quaternion world actually combines the rotations):

var curNormal = Vector3.up;

function getTerrainRot(myTarget:Vector3,terrPos: Vector3): Quaternion { var terrHit: RaycastHit; var newPos: Vector3; var normal = Vector3.up; // default normal = up if (Physics.Raycast (terrPos, -curNormal, terrHit, 10.0)){ normal = terrHit.normal; // use the surface normal, if any } // smoothly follow the surface normal curNormal = Vector3.Lerp(curNormal, normal, 2*Time.deltaTime);
newPos = myTarget - terrPos; // get the direction to look at newPos.y = 0; // zero y to keep only the horizontal direction // calculate rotation to normal var rot = Quaternion.FromToRotation(Vector3.up, curNormal); // combine with the horizontal rotation rot = rot * Quaternion.FromToRotation(Vector3.forward, newPos); return rot; }

Comment
Add comment · Show 7 · 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 Meltdown · Sep 09, 2011 at 08:10 AM 1
Share

When are you writing us a 'Quaternions for Unity developers' blog Al?

avatar image 2dkot · Sep 09, 2011 at 09:29 PM 0
Share

Not work (. See here: http://img194.imageshack.us/img194/9264/movement.jpg

avatar image aldonaletto · Sep 09, 2011 at 11:38 PM 0
Share

I'll check this script and return asap.

avatar image aldonaletto · Sep 10, 2011 at 04:38 AM 0
Share

The problem is that LookAt really prefers to look at the target, completely ignoring the normal. I edited my answer to do things in the traditional quaternion way.

avatar image aldonaletto · Sep 10, 2011 at 05:29 PM 0
Share

@$$anonymous$$eltdown, that's a tempting idea - but I still have to learn a lot of secrets of these four headed monsters before doing that!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Third person Character Control 0 Answers

Why my framerate fall down to 5-6FPS once I use CharacterController.Move() 6 Answers

why does character controller accelerate off ledges? 1 Answer

Pushing a character controller (physics interaction) 0 Answers

Resetting rotation with Quaternion.Euler 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