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 SomeGuy22 · Aug 03, 2013 at 02:28 AM · raycasthitnormalalignlocal axis

Raycast Normal in Local Rotation?

I'm trying to align the local x axis of an object (p) to a raycast normal from the parent object. However I ran into a ton of problems. I finally got it to work but I realized the normal direction always somehow set the x axis as if the raycast had come from the world rotation and not the local rotation of the parent object. Is there something wrong with this code?

 var bhit : RaycastHit;
 var fhit : RaycastHit;
 var bpos = transform.position - transform.forward * .4;
 var fpos = transform.position + transform.forward * .4;
         
     if (Physics.Raycast(bpos, -transform.up, bhit, 2, rotateMask))
         {
             if (Physics.Raycast(fpos, -transform.up, fhit, 2, rotateMask))
             {
                 Debug.DrawLine(fpos, fhit.point);
                 Debug.DrawLine(bpos, bhit.point);
                 
                 var averageHit = (fhit.normal + bhit.normal) / 2;
                 
                 var hitRotation = Quaternion.FromToRotation(transform.up, averageHit);
                 
                 if (fhit.point.y >= bhit.point.y)
                 p.transform.localRotation.x = Mathf.Lerp(p.transform.localRotation.x, hitRotation.x, Time.deltaTime * 3);
                 else
                 p.transform.localRotation.x =  Mathf.Lerp(p.transform.localRotation.x, -hitRotation.x, Time.deltaTime * 3);
                 
                 p.transform.localRotation.z = hitRotation.z;
                 //Debug.Log(hitRotation);
         }
             
     }

The reason for the hit.point check is because I thought I could negate the effect when it's goes the opposite direction, however I realized that's only because the raycast seems to cast in world space, in only one direction. Is there a way to cast in local space so that the normal can be used to calculate the proper local x axis?

Comment
Add comment · Show 7
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 · Aug 03, 2013 at 02:39 AM 0
Share

You cannot assign individual components of a Queternion (i.e. localRotation) independently and expect anything. They are not angles. Also your fhit.normal and bhit.normal are in world space. I'm not sure what you are trying to align with what. If it is just the 'x' axis of the current object with the averageHit normal, you can do:

 transform.rotation = Quaternion.FromToRotation(transform.up, averageHit) * transform.rotation;
avatar image SomeGuy22 · Aug 03, 2013 at 02:43 AM 0
Share

Ah okay that seemed to work however it looks like I still need to check the hit.point.y to reverse the effect but I shouldn't have to do that if the normals can already tell the direction right?

avatar image robertbu · Aug 03, 2013 at 02:46 AM 0
Share

I cannot tell you since I don't understand what you are trying to do (and that's why I put this as a comment rather than an answer). Depending on what you are doing, you may need to reverse the normal (i.e. use -averageHit).

avatar image SomeGuy22 · Aug 03, 2013 at 02:51 AM 0
Share

I'm trying to rotate my character to the floor normal but only his local x axis because his parent is the one that is rotating along the y. However when I raycast down with the two raycasts above, and average them out, I get some really weird things, even with the modified code you suggested. For some reason even with the checks he rotates or doesn't rotate at all even though both normals are clearly pointing in a direction.

avatar image SomeGuy22 · Aug 03, 2013 at 02:56 AM 0
Share

alt text

He's not rotating with the normals (red) using the code above :/

screen shot 2013-08-02 at 10.53.54 pm.png (434.3 kB)
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SomeGuy22 · Aug 03, 2013 at 03:54 AM

robertbu:

"This 'parent' and 'child' separate rotations has me confused. Are you trying to get the character to stand up? If so you should use transform.up instead of transform.right in the code I provided you. Usually if you want something to only align on a certain axis, the trick is to modify the normal before calculating the rotation. You would project the normal onto a plane. Since I'm having trouble visualizing this problem, I cannot say that is what is needed here. Here is the ProjectVectorOnPlane function borrowed from Math3d in the Wiki and translated into JS.

 function ProjectVectorOnPlane(planeNormal : Vector3, vector : Vector3) : Vector3{
     return vector - (Vector3.Dot(vector, planeNormal) * planeNormal);
 }

You would use it like this:

 averageHit = ProjectVectorOnPlane(transform.right, averageHit);

"

Still getting some artifacts but nothing too major. THis works fine

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.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

Help with getting buildings blocks to appear on hit object's normals 1 Answer

How i can a normal vectors? 3 Answers

How can i get point reletive to hit.point 0 Answers

default cylinder to indicate hit.normal 1 Answer

Normal to Quaternion (relative to origin of hit object) 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