Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 mysterion · Aug 13, 2021 at 07:08 AM · 3ddirectionquaternions

How do I get the direction of an objects face?

Not the direction an object is facing but say the face of a cube. I would like to instantiate an object with is rotation appropriated the the face I clicked on. My idea is to set the 'object I want to instantiate's current direction to up and have it point away from the object (in theory).

Here is what it looks like currently as an example. alt text

As you can see I would like the middle node to be facing right relative to the player.

here is my code.

     // Update is called once per frame
     void Update()
     {
         if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, toolRange))
         {
             Debug.DrawRay(transform.position, (hit.point - transform.position), Color.yellow);
             if (Input.GetMouseButtonDown(0))
             {
                 var node = Instantiate(wireNodePrefab, hit.point, quaternion.identity) as GameObject;
             }
         }
     }

Code may or may not be useful but its here if you think it will help me.

While I work through the night any help will be greatly appreciated!

get-objects-face-direction.png (78.2 kB)
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
0
Best Answer

Answer by mysterion · Aug 13, 2021 at 11:22 PM

So I finally got it. I was working too hard with tools I didn't need. Here is my solution.

                var pointToLook = Quaternion.FromToRotation(Vector3.up, hit.normal);
                var node = Instantiate(wireNodePrefab, hit.point, pointToLook) as GameObject;
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
0

Answer by Namey5 · Aug 13, 2021 at 07:33 AM

You can use the normal of the hit point to get the direction of its face;

 GameObject node = Instantiate (wireNodePrefab, hit.point, Quaternion.LookRotation (hit.normal));
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 mysterion · Aug 13, 2021 at 08:42 PM 0
Share

I spent a few hours last night playing with Quaternion.LookRation. It has two parameters. I believe it is the right idea but I need to get the math right. The second argument you would pass is to tell the function which way is up. I've even changed the normal orientation of the prefab. Edit* And I have tried hit.normal seems to be on the right track for me as well. Maybe my problem is that in not setting the forward direction in the prefab correctly.

Edit2* After hours of work I got it and it was soooo simple. Thanks for offering your help.

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

162 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 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 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 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 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 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 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 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

Slingshot 3d : Add Force to ball with Line infront 0 Answers

Relation between two 3D vectors that are in the same plane, to translate to 2D 4 Answers

Is there a way to tell which way an enemy is going? 1 Answer

How can I change player position and save direction? 2 Answers

Unity3d find angle/direction of a Ray / Raycasthit 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