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
0
Question by chillypacman · Jun 09, 2012 at 02:12 PM · c#vectorforwardup

Combine transform.forward and transform.up?

I am trying to set up a character which faces a particular forward direction and its up direction is direction is based on the normal of the mesh it's standing on.

So I do something like;

transform.up = raycastHitInfo.WorldNormal;

transform.forward = LookDirection;

The problem is that I can't seem to set both components simultaneously and get a desired result.

What I'm trying to do is set transform.right as a cross product between up and forward,

transform.right = Vector3.Cross(FloorNormal, ForwardLookDirection);

But the results I'm getting aren't very good for some surfaces which are angled at more than a single axis (see here: http://i.imgur.com/TQpYU.png)

Any ideas for how I can do what I'm trying to?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Jessy · Jun 09, 2012 at 02:16 PM

http://unity3d.com/support/documentation/ScriptReference/Transform.LookAt.html

http://unity3d.com/support/documentation/ScriptReference/Quaternion.LookRotation.html

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

Answer by DerDude · Jun 09, 2012 at 02:51 PM

Have you tried using Quaternion.LookRotation? http://unity3d.com/support/documentation/ScriptReference/Quaternion.LookRotation.html

You pass that method a direction the object will face with it's z-axis and an up-vector which will determine the rotation of the object around that direction. This will give you a Quaternion you can assign to transform.rotation.

So in your case that would look like this:

 transform.rotation = Quaternion.LookRotation(LookDirection, raycastHitInfo.WorldNormal);
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 Owen-Reynolds · Jun 09, 2012 at 03:11 PM 0
Share

What was wrong with this? Sure, LookAt is a typing shortcut for when you want to immediately apply the rotation to you (which is the case here,) but they both do the same math. And there's a school of program$$anonymous$$g that says to learn the general commands first (such as LookRotation) and shortcuts later.

avatar image Jessy · Jun 09, 2012 at 03:28 PM 0
Share

:-|

Thumbs down is for not reading the other answers first.

avatar image
1

Answer by Owen-Reynolds · Jun 09, 2012 at 03:04 PM

To elaborate on Jessy's fine reply, as you mentioned, you have to set forward and up simultaneously for it to work. Each transform.up= sets the entire rotation. So, it's like setting x=0.4; x=10; and hoping, since you didn't write 10.0 that it will give 10.4.

LookAt and LookRotation take a 2nd optional parm, as the up:

 transform.LookAt(LookDirection, hit.normal);

Another cute trick is you can compute the smallest rotation between two vectors and apply that to your current rotation. "face this way" ignores your current rotation, but "tilt from where I am now to this new facing" respects your current orientation:

 Quaternion RotTo = Quaternion.setFromToRotation(transform.forward, LookDir);
 transform.rotation = transform.rotation * RotTo; // apply the spin
 //   or  RotTo * transform.rotation;  (the order matters and I can never remember)
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 Jessy · Jun 09, 2012 at 03:38 PM 0
Share

The uncommented line results in a local space rotation. The commented RotTo (not a good variable name) is a world space rotation.

avatar image
0

Answer by Winterblood · Jun 09, 2012 at 02:58 PM

The two vectors you're cross-producting aren't necessarily at right angles, which will give you a skewed matrix. Try this:

  • Cross the facing vector with the normal to get the right axis

  • Cross the right axis with the facing vector to get a new "up" vector

This guarantees you an orthogonal matrix. But you'll get nasty snaps as you cross polygon boundaries (and the normal changes suddenly), so you may want to store this as a target matrix and seek the actual matrix towards it to smooth those transitions.

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 Jessy · Jun 09, 2012 at 03:06 PM 0
Share

There's no point in creating a matrix, because you can't assign it.

avatar image Winterblood · Jun 09, 2012 at 03:37 PM 0
Share

Ah, you're right. Sorry, thinking of my C++ toolset at work. I have solved a very similar problem in Unity, but I think I did use a quat as you suggested.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Bullet shoots up instead of forward? 2 Answers

Vector3.forward question 1 Answer

Find if any gameObject exists in an exact Vector3? 0 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