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 gord0_ · Nov 19, 2013 at 12:51 PM · rotationraycastterrainhitsnap

rotation problems when snapping to terrain

Alright, in the project I'm working on some objects can be moved around by the user via the mouse. They snap to the terrain whenever dragging so they can't actually be pulled off the terrain. This was in place before I was put on the project. My task is to make the objects not only snap positionally but rotationally(hug slopes, etc). Currently I've made a prefab that shoots 3 rays to the ground (-up of object). I average the normals of the hits and then set the object's up vector to that value. Which at first I thought was working fine because I was testing with a cube and couldn't visually see issues. So then I tried on a truck model. Seemed fine, but then I rotated the truck then went to drag again and I found it was snapping to a y rotation that was different than before I started dragging. I THINK this is because the forward vector is auto-recalculating when I directly set the up.

Below is my update method that does the work I've specified. It would be much appreciated if anyone can tell me how I can achieve the desired effect.

[code] void Update() { if (_GOD==null) { if (_OI.GetActiveGhost()!=null) { _GOD=_OI.GetActiveGhost().GetComponent() as GhostObjectDrag; } }

     if (transform.localScale.x!=0.1f || transform.localScale.y!=0.1f || transform.localScale.z!=0.1f)
     {
         transform.localScale=new Vector3(0.1f, 0.1f, 0.1f);
     }

     //TargetObject.transform.position+=-TargetObject.transform.up*Time.deltaTime;
     //_snapChildren.ToArray()[2].transform.RotateAround(new Vector3(0.0f, 1.0f, 0.0f), Time.deltaTime);

     //Debug.LogWarning("Mouse Button 0 down: "+Input.GetMouseButton(0));

     if (_OI.GetActiveObject()==TargetObject.gameObject && _OI.GetActiveObjectDragging()) 
     {
         //Debug.LogWarning("We are the active object being dragged!!");

         //_curHitInfo=_snapChildrenHitInfos.ToArray()[0];
         if (Physics.Raycast(TargetObject.transform.position, -TargetObject.transform.up, out _hitInfo, 100.0f, 
                             _targetLayerMask))
         {
             _normals.Clear();
             _normals=new List<Vector3>();

             //Debug.LogWarning("Ding!! "+_hitInfo.transform.name);
             for (int iChild=0; iChild<_snapChildren.Count; iChild++)
             {
                 _curHitInfo=_snapChildrenHitInfos.ToArray()[iChild];
                 //Debug.LogWarning("LayerMask.NameToLayer(\"Draggable\"): "+(1 << LayerMask.NameToLayer("Draggable")));
                 Physics.Raycast(_snapChildren.ToArray()[iChild].transform.position, 
                                 -_snapChildren.ToArray()[iChild].transform.up, out _curHitInfo, 100.0f, 
                                 _targetLayerMask);

                 _normals.Add (new Vector3(_curHitInfo.normal.x, _curHitInfo.normal.y, _curHitInfo.normal.z));
                 //Debug.LogWarning("Ding!! "+iChild+' '+_curHitInfo.transform.name);
                 //Debug.LogWarning("_curHitInfo.normal: "+_curHitInfo.normal);
                 /*Debug.LogWarning("_snapChildrenHitInfos.ToArray()[iChild].normal: "+
                                  _snapChildrenHitInfos.ToArray()[iChild].normal);*/
                 //Debug.LogWarning("_normals.ToArray()[iChild]: "+_normals.ToArray()[iChild]);
             }

             Vector3 avgNormal=new Vector3(0.0f, 0.0f, 0.0f);
             for (int iNormal=0; iNormal<_normals.Count; iNormal++)
             {
                 avgNormal+=_normals[iNormal];
                 //Debug.LogWarning("_normals["+iNormal+"]: "+_normals[iNormal]);
             }
             avgNormal/=_normals.Count;
             //Debug.LogWarning("avgNormal: "+avgNormal);

             //TargetObject.transform.localRotation=Quaternion.Euler(avgNormal);
             TargetObject.transform.up=avgNormal; //MAKE A POST ON STACK OVERFLOW...if limiting rotation to x/z
                                                  //doesn't work
         }
     }
 }

[/code]

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

Answer by gord0_ · Nov 19, 2013 at 07:48 PM

Well I've got it sorted, since the y rot was always snaping back to 0 when dragged, I added the following line before and after the line in which I set the up vector: TargetObject.transform.up=avgNormal; TargetObject.transform.Rotate(new Vector3(0.0f, _yRot, 0.0f), Space.Self); I have another object call this method on mouse down: public void SetYrot(float yRot) { _yRot=yRot; }

However, if anyone can think of a better solution, I welcome it.

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

17 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

Related Questions

use the raycast with the rotation 0 Answers

Camera collision detection. 0 Answers

Following Terrain Height? 2 Answers

Making a particle effect parallel to the slope of a terrain 1 Answer

Make an instantiated object match the slope of the terrain 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