• 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 Fourthings · Jul 21, 2014 at 05:19 PM · normal

How to get the forward direction of a normal?

I have a Bike that travels down a race track, the track is one long mesh. I have a rectangular object, let's call him Ben, that sits under the bike that has the below script on him, the bike will eventually Lerp it's Y rotation to Ben's Y rotation.

Ben casts a red ray straight down, obtains the normal vector of the object below, and matches it's rotation. It's working fine for the X and Z values, but not for Y, which is the one I need. Ben also casts a green ray sideways to detect the edge of the road.

How do I rotate Ben to have the same Y rotation as the normal directly below him?

Or rather, how do I make Ben face down the track? I want his Y rotation perfectly aligned with the white lines on the road as it passes over them.

     function Update ()
     {
         OrientThisToObjectBelow(transform.TransformDirection(-Vector3.up));
     }
     
     function OrientThisToObjectBelow (dir : Vector3)
     {
         if (Physics.Raycast (transform.position, dir, hit, 150, LayerMask))
         {
             Debug.DrawLine(transform.position, hit.point, Color.red);
             transform.up = hit.normal;
         }
     }
 

alt text

Comment
Add comment · Show 6
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 robertbu · Jul 21, 2014 at 05:54 PM 1
Share

Normals don't have a forward direction. You are going to need to introduce more information into the system. For example, you can place a spline or even a set of waypoints down the center of your road to align with. Or you might be able to calculate the position/direction from the mesh vertices.

avatar image calebheale · Jul 21, 2014 at 11:16 PM 0
Share

Can you not just get the rotation of the track below? It should be the same, or at the very least, off by a constant offset. Is the track straight, or does it curve?

avatar image Fourthings · Jul 22, 2014 at 03:35 PM 0
Share

The track does curve, and is about 17km long, which is why I was hoping to use it's normal info to direct the bike. I did use EasyRoads 3d though which does generate a spline curve, although once you finalize the road it's deleted

avatar image Fourthings · Jul 22, 2014 at 04:05 PM 0
Share

Ah, yep this was my mistake, for some reason I thought a normal had an X and Z direction. Thank you everyone for your help anyway it's much appreciated!

avatar image calebheale · Jul 22, 2014 at 09:58 PM 0
Share

Could you store a copy of that spline? That could be sufficient to calculate the direction. Is the biker always in the middle of the track? If so , that should make it even easier.

avatar image StarWeaver · Jul 23, 2014 at 10:06 PM 0
Share

Point to remember: a normal /is/ just a single direction. 6ou can find the plane perpendicular to it but it itself dosen't imply any particular orientation of axes on that plane.

Like robertbu said, you need more informaton. For example, if you have a point/normal under each wheel and some concept of whih way up is6ou can probably derive the information you need, though don't ask me how :e

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by wibble82 · Jul 22, 2014 at 04:00 PM

There's various solutions to this kind of problem, depending on exactly the effect you want. Ultimately what you're trying to do is take your biker, who has 1 rotation, and 'constrain' it, to obey some specific rules.

Unfortunately the first problem here is what direction is 'along the track'. The normal to the track is the vector directly upwards from it, not along it. Perhaps the simplest way to do this (without using additional libraries and things) would be to add a set of invisible 'way points' to the track, each oriented so that its z axis (tranform.forwards) points along the track. Then as a first version you can simply find the closest way point and quite simply set the biker's rotation to be the same as the way point's rotation. If the biker is a rigid body you'll want to use rigidbody.MoveRotation, otherwise just set the transform.rotation value directly.

Going on from that, once you have it working you'll find the biker appears to move along fine, however when heading round curves he'll suddenly 'snap' to the orientation of the next way point. To solve this you could slerp from the rotation of the previous way point to the rotation of the next way point as the biker approaches it. If you wanted to get really smooth you could even look into catmull rom interpolation, but I'd wait and see if you need it first!

You may find with more complex tracks the simple 'what way point am I closest to' isn't the correct metric (for example, it might fail when going round a 180 degree bend). In this case, you'll probably need to handle it either by numbering the way points, or setting them up as trigger zones that cover the road, so the biker always knows exactly which way point he is in.

If you need to get cleverer and start doing things like allowing the biker's orientation to change slightly under physics or change due to leaning as he goes round corners, you'll hit the scenario where you want to manipulate the rotation in more complex ways. Once you get there, look up Quaternion.FromToRotation and its uses, which gives you a rotation that when applied will make one vector point in the same direction as another

Hope that helps :)

-Chris

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Normal problems 4 Answers

RaycastHit.normal Problem 1 Answer

Importing Blender Water to Unity 0 Answers

Fading Normal Map 1 Answer

Best way to create normal maps for unity? 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