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 MultiSpoone · Jan 04, 2015 at 10:37 PM · gameobjectgroundverticalstick

Make GameObject always vertical to Terrain

So I am working on my Animal AI and its working everything fine exept the fact that if the ground is not flat it breakes itself so is there a way to make the gameobject(animal) always vertical to the ground?

here my drawing

alt text

I am thinking about a solution for so long but i cant figure out how i should do this! thanks!

unbenannt.png (22.6 kB)
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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Mikyjax · Jan 05, 2015 at 10:15 AM

Hi, did you try try to send a ray and align the rotation of your object to the "normal" of your terrain under you raycast? Try to put your animal inside an empty game object and apply the rotation on your empty game object.

Unity Documentation

Hope this help! Mike

Comment
Add comment · Show 3 · 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 MultiSpoone · Jan 05, 2015 at 09:22 PM 0
Share

The Problem is the Rotation dont got an Rotation if i am right cause its one gameobject

avatar image MultiSpoone · Jan 05, 2015 at 11:01 PM 0
Share

i tried my best is not working

avatar image Oliver1135 · Jan 06, 2015 at 02:43 AM 0
Share

'It's not working' is far to generic to give you help the steps required should be very straight forward, at the point you are instantiating or placing your animal, do a raycast straight down that should get the hit information from the ray cast and then, then assign the rotation of the raycasthit normal to the gameobject

http://docs.unity3d.com/ScriptReference/RaycastHit.html

avatar image
0

Answer by Rainbirth · Jan 06, 2015 at 03:41 AM

as said by mikyjax, you need to raycast the terrain and get the normal. Then using your object rotate to that normal.

you have sample code in this forum post. http://forum.unity3d.com/threads/aligning-object-to-surface-normal-with-quaternion-lookrotation.63619/

Hope it helps!

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 Weblyan · Jan 08, 2017 at 09:33 AM

You don't need to raycast the terrain. You can get the normal from the terrain data.

     public static void AlignTransform(Transform transform)
     {
         Vector3 sample = SampleNormal(transform.position);
        
         Vector3 proj = transform.forward - (Vector3.Dot(transform.forward, sample)) * sample;
         transform.rotation = Quaternion.LookRotation(proj, sample);
     }

     public static Vector3 SampleNormal(Vector3 position)
     {
         Terrain terrain = Terrain.activeTerrain;
         var terrainLocalPos = position - terrain.transform.position;
         var normalizedPos = new Vector2(
             Mathf.InverseLerp(0f, terrain.terrainData.size.x, terrainLocalPos.x),
             Mathf.InverseLerp(0f, terrain.terrainData.size.z, terrainLocalPos.z)
         );
         var terrainNormal = terrain.terrainData.GetInterpolatedNormal(normalizedPos.x, normalizedPos.y);

         return terrainNormal;
     }
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 aakwewaanaqa · Jan 24, 2017 at 07:51 AM

 private GameObject root;

     void Start ()
     {
     root = transform.root.gameObject;
     }

     private void OnTriggerStay(Collider other)
     {
         if (other.tag == "Planet")
         {

         RaycastHit hit;
         if (Physics.Raycast(transform.position, -transform.up, out hit))
         {
         }

             //Makes the character to stand vertically to the ground
             float angle = Vector3.Angle(root.GetComponent<Transform>().up, hit.normal);
             Vector3 look = Vector3.Cross(root.GetComponent<Transform>().up, hit.normal);
             if (angle < 3 && angle > -3) { angle = 0; }
             root.GetComponent<Transform>().RotateAround(root.GetComponent<Transform>().position, look, angle * Time.deltaTime);
             Debug.Log(angle);
         }
     }



I discovered using the Object.GetComponent().RotateAround still can solve this problem. Just only have to find out the rotate axis and the angle. By using Vector3.Angle() and Vector3.Cross(), easily find out where to rotate. Also the cross production determines it is angle or -angle automatically.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get a object to stick another object? 1 Answer

Ground continuing down when ball collide with ground. Help? 1 Answer

Gameobject placing halfway through ground? 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

How attach one sphere to another sphere on draging of sphere 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