Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by kidmosey · Dec 05, 2015 at 12:13 PM · physicsrigidbodyterraininitialization

Calculating the resting position of a rigidbody.

What is the easiest way to calculate the resting transformation of an object with a rigidbody attached to it?

Currently, I have to position the object just over the terrain, then when the scene starts, you see it drop into place.

I'd like to not see it drop when the scene is initialized.

My current idea is to wait for all objects to drop before showing the scene, but I'd prefer something less hacky.

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 kidmosey · Dec 07, 2015 at 10:05 AM

Okay, I've found a solution to this from http://answers.unity3d.com/questions/168097/orient-vehicle-to-ground-normal.html

It isn't the most straightforward solution, but I can use it for more than just initialization. Currently, I am using it in my character controller.

I don't actually need the rigidbody with this solution, so I've disabled it for now. Also, this could probably use a few optimizations, but it works for now.

 Vector3 GetUpVector()
 {
     // quad is an invisible rectangle denoting the four corners of the base
     MeshCollider collider = quad.GetComponent<MeshCollider>();
 
     // ensure we are above the terrain so the hit tests work
     transform.position += Vector3.up * 5;
     Vector3 backLeft = quad.transform.TransformPoint(collider.sharedMesh.vertices[3]);
     Vector3 backRight = quad.transform.TransformPoint(collider.sharedMesh.vertices[0]);
     Vector3 frontLeft = quad.transform.TransformPoint(collider.sharedMesh.vertices[1]);
     Vector3 frontRight = quad.transform.TransformPoint(collider.sharedMesh.vertices[2]);
 
     RaycastHit lr, rr, lf, rf;
     Vector3 upDir = Vector3.up;
     int layerMask = LayerMask.GetMask(new[] { "Terrain" });
 
     bool overTerrain = Physics.Raycast(backLeft + Vector3.up,
                                        Vector3.down, out lr, layerMask)
         && Physics.Raycast(backRight + Vector3.up, Vector3.down, out rr, layerMask)
         && Physics.Raycast(frontLeft + Vector3.up, Vector3.down, out lf, layerMask)
         && Physics.Raycast(frontRight + Vector3.up, Vector3.down, out rf, layerMask);
 
     if (overTerrain)
     {
         upDir = (Vector3.Cross(rr.point - Vector3.up, lr.point - Vector3.up) +
                  Vector3.Cross(lr.point - Vector3.up, lf.point - Vector3.up) +
                  Vector3.Cross(lf.point - Vector3.up, rf.point - Vector3.up) +
                  Vector3.Cross(rf.point - Vector3.up, rr.point - Vector3.up)
         ).normalized;
     }
 
     // Move back to the starting position
     transform.position += Vector3.down * 5;
 
     return upDir;
 }
 
 Quaternion GetPredictedForwardQuaternion()
 {
     // temporarily orient the object and calculate the up vector
     Quaternion origRot = transform.rotation;
     transform.rotation = Quaternion.LookRotation(camera.forward, Vector3.up);
     Vector3 up = GetUpVector();
     transform.rotation = origRot;
 
     // get the forward vector and create a new quaternion
     Vector3 forward = Vector3.ProjectOnPlane(camera.forward, up).normalized;
 
     return Quaternion.LookRotation(forward, up);
 }
 
 void DropToTerrain()
 {
     MeshCollider collider = quad.GetComponent<MeshCollider>();
 
     // Ensure we are above the terrain.
     transform.position += Vector3.up * 5;
     Vector3 backLeft = quad.transform.TransformPoint(collider.sharedMesh.vertices[3]);
     Vector3 backRight = quad.transform.TransformPoint(collider.sharedMesh.vertices[0]);
     int layerMask = LayerMask.GetMask(new[] { "Terrain" });
 
     RaycastHit lr, rr;
 
     // One of these corners will be one of the 3 that are touching the ground
     if (Physics.Raycast(backLeft, Vector3.down, out lr, layerMask)
         && Physics.Raycast(backRight, Vector3.down, out rr, layerMask))
     {
         transform.position += Vector3.down * Mathf.Min(lr.distance, rr.distance);
     }
 }
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

42 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

Related Questions

Car made from Different GameObjects and Empty GameObjects breaks on Collision with Terrain. 0 Answers

Rotating around a point using Rigidbody? 0 Answers

Sphere bouncing back on edges of aligned objects 0 Answers

Proper way to set a RigidBody's maximum velocity 1 Answer

AI detect incoming rigid body position placement to catch it 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