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 brenosilver · Oct 27, 2014 at 05:11 AM · rotationrotaterotatingroad

How to rotate an object so it always faces up?

Hi,

I have a road tool that when I click and release the mouse button it creates the mesh and rotates it so the road starts and ends where the mouse events occurred. However, when I build a road on a hill one of the axis when doing this rotation always gets messed up.
Picture:

alt text

I want the road to always face up, which as you can see on the pic it is facing to the side a bit.

I have tried everything I could gather for 1 week and nothing seems to work.

Code that im using in the picture to rotate:

 road.transform.rotation = Quaternion.FromToRotation(Vector3.right, end - origin);

I have also tried the Rotate() method:

 float angle = Vector3.Angle(Vector3.right,  end - origin);
 road.transform.Rotate(Vector3.up, angle, Space.World);

I tried to get the angle between the current up and Vector3.up but I was getting garbage don't know why.

And many more but nothing works, I'm getting frustrated with this.

Can someone assist me please?

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
0
Best Answer

Answer by robertbu · Oct 27, 2014 at 05:37 AM

The easiest solution would be to construct your road so that it runs forward rather than right, and that up is up. Then you use a Raycast to get the normal of the surface. The rotation code could be:

 transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal) * transform.rotation;
 transform.rotation = Quaternion.LookRotation(end - origin, hit.normal);

If this setup does not work for you for some reason, there are other ways to solve this problem.

Comment
Add comment · Show 6 · 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 brenosilver · Oct 27, 2014 at 06:20 AM 0
Share

Alright, trying it right now =)

avatar image brenosilver · Oct 27, 2014 at 06:39 AM 0
Share

It didn't worked as intended, the road is now facing the normals, which is an improvement I guess. alt text

avatar image robertbu · Oct 27, 2014 at 06:41 AM 0
Share

Can you show me a picture so I can see the result? I'm confused about what you mean by 'facing the normals'.

avatar image brenosilver · Oct 27, 2014 at 07:12 AM 0
Share

Yep, it is taking the slope of the hill. alt text

avatar image robertbu · Oct 27, 2014 at 07:37 AM 0
Share

Taking the slope of the hill is what my code does. Obvious from your comment, that is not what you want. Can you describe what you want? Given the curve of the hill and road, I cannot see a right solution unless you were to bend your road to the hill. If you just want the road facing up, you can do:

  transform.rotation = Quaternion.LookRotation(end - origin, Vector3.up);
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

2 People are following this question.

avatar image avatar image

Related Questions

Raycasting, rotating a ray 360 degrees 2 Answers

How to reset child object's rotation without changing parent's 1 Answer

Rotation is jumpy 2 Answers

How to flip objects that are currently rotating? 1 Answer

Delayed rotation when i use transform.rotate 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