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 oneslyfox · Apr 13, 2012 at 10:57 PM · normalsurfacefloatinghover

Fixed distance from object surface normal

I'm trying to get an object to follow in front of a player that moves along a track. The player can travel along the ground, up inclines, and up walls. Now, I need this object in front to rotate with and hover above the track by about 2 units.

I have something like this:

obj.transform.position = obj.transform.position + (obj.transform.up (hit.point.y 2));

This works while on horizontal ground but when going up a ramp onto a wall, not so well. Obviously, I can't just use my raycast hit.point.y (which is being cast down from the object's bottom) if the object is rotating and traveling vertically up a wall.

Any ideas how to incorporate the surface normal and angle of the ground into this? Or some entirely different solution?

Thanks!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Atrius · Apr 14, 2012 at 02:11 AM

I am not quite sure how to work this into your existing math given what you've provided. However here is a solution that may help you. This is in C#.

Since you say you are casting a ray down to the surface below you, then you can use the normal of surface point you hit to rotate your object.

     RaycastHit ray;
     if (Physics.Raycast(transform.position,Vector3.down,out ray,5.0f)) {
         transform.rotation = Quaternion.LookRotation(ray.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 rutter · Apr 14, 2012 at 07:37 AM 0
Share

Pretty good solution, given the complexity of other fixes that come to $$anonymous$$d. $$anonymous$$ight be useful to quickly rotate, rather than snapping (can cause some nasty effects, especially if we're snapping a character with an attached camera). Various lerp functions could allow direct control of that, or there's always `Quaternion.RotateTowards()`.

avatar image
0

Answer by oneslyfox · Apr 14, 2012 at 06:25 PM

I actually have the rotation (smooth) figured out:

var targetRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);

camTarget.transform.rotation = Quaternion.Slerp(camTarget.transform.rotation, targetRotation, Time.deltaTime * 8); I wasn't really explaining my problem very well, so I'll include some pictures. I want the object (the box) to always be 2 units away from the floor, no matter what angle the floor is at. Ex #1: It's sitting nicely above the ground at 2 units and 4 units in front of the player. ![alt text][1] Ex #2: As it approaches the incline it's rotating nicely based on the normal but is not 2 units away from the floor. ![alt text][2] Ex #3: Now it's beginning to intersect the wall because it's 4 units away from the player and 2 units above the player. ![alt text][3] There must be away to use hit.normal and hit.point in combination to have this object always 2 units away from the floor or wall; I'm just stumped as to how... [1]: http://s17.postimage.org/8g3o7lwxb/ex1.jpg [2]: http://s15.postimage.org/rirovozh7/ex2.jpg [3]: http://s13.postimage.org/441adde47/ex3.jpg
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 schkorpio · Apr 21, 2017 at 09:31 AM

To always keep an object a set distance away from a surface (this works on curves etc too), you have to:

1) Shoot a Physics.Raycast onto any surface so that Unity can give you the direction of which way the surface is facing aka the direction perpendicular to the surface (called the normal).

2) Multiply the surface normal (RaycastHit.normal) with however far you want your object away from the surface.

3) Add the multiplied surface normal to where the raycast ended up hitting (RaycastHit.point).

 float distanceAwayFromSurface = .5f;
 RaycastHit hit;
 
 if(Physics.Raycast(rayOrigin, rayDirection, out hit, rayDistance))
 {
        transform.position = hit.point + hit.normal * distanceAwayFromSurface;
 }
 
 
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I obtain the surface normal for a point on a collider (can't use RaycastHit.normal)? 3 Answers

Hovering smoothly at one height 1 Answer

Mesh triagnles/surface with normals 1 Answer

Assigning vertex normal in Surface program to o.Normal brakes lighting? 0 Answers

Too many texture interpolators would be used for ForwardBase pass (again) 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