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.

void Update()
	{
		if (_GOD==null) 
		{
			if (_OI.GetActiveGhost()!=null)
			{
				_GOD=_OI.GetActiveGhost().GetComponent<GhostObjectDrag>() 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
			}
		}
	}

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.