0

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.

void Update()
{
   //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
      }
   }
}
gord0
  • 1
  • 1
  • Are you sure that the Up vector of the truck model is correct, and that there's no local rotation applied? – Dan Puzey Nov 19 '13 at 15:47
  • Also, the first two `if` statements and most of the comments in your code appear irrelevant to the problem at hand. It would help if you can trim your code down to the bare minimum to help people understand more easily what's going on. – Dan Puzey Nov 19 '13 at 15:51
  • Irrelevant code removed, investigating truck rotation. – gord0 Nov 19 '13 at 16:35
  • The truck's parent most object was rotated a little on y, but making it 0 didn't fix the issue. When the user holds down alt the mouse rotates the item instead of dragging it. So I rotated the truck at run time and then went to drag it. After doing so the y rot snaps back to almost zero. So all the functionality I want is there except that the rotation on y snaps back to 0 whenever the item is dragged. – gord0 Nov 19 '13 at 16:48
  • also the truck's forward and up are pointing in the correct directions, I tested with the now commented line: //TargetObject.transform.position+=-TargetObject.transform.up*Time.deltaTime; – gord0 Nov 19 '13 at 17:09
  • 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; } – gord0 Nov 19 '13 at 19:40

0 Answers0