Object Movement

I have a weird issue I’m trying to resolve. Obviously there is something I’m doing wrong.

I’m trying to setup a basic object movement script in game so I have a script attached to a gameobject that handles movement with the mouse. It does a raycast call to determine where the object should be placed while moving as well as how it show be oriented as I want it oriented to the surface we are colliding with as we drag the object around.

Now also I want to sticky objects together so that if I move object A on top of object B object A is now a child of B. I have it basically working with the below script, but for some reason while I move my object around it is scaling the object in weird ways. It seems to be caused when I attached the object to what it is colliding with, but I’m not sure why. I assume it has something to do with local vs world transform, but there should be a way to do this. Thanks for any advise on this matter.

Code in Update function for gameObject…

   if(isSticky){

		var mousePos = Input.mousePosition;
		
		var hitray : Ray = camera.main.ScreenPointToRay(mousePos);
		var hit2 : RaycastHit;
  
		if(Physics.Raycast(hitray, hit2, 1000)){
					
			transform.position = hit2.point;
			
			var rot = Quaternion.FromToRotation(transform.up, hit2.normal);
			transform.rotation *= rot;
			
			transform.parent = hit2.transform;	
		}//if
		
		if(Input.GetMouseButton(0)){
			setSticky(false);	
		}//if
			
	}//if

The scale of parents affects the scale of children. If you attach children to a parent manually in Unity with drag’n’drop, and the scales are different, it will fix the scale for you so objects don’t change size. If you do it programatically then you have to change the scale yourself. The easiest way to fix the problem, though, is to just have everything scaled at 1,1,1 in the first place, if possible (I believe it’s slightly faster this way too).

–Eric

Well that’s the strange thing. Both objects in this case have a starting scale of 1,1,1 and the scale of the object as I drag continues to shrink slowly, but increases as I move over sub objects. This only occurs when I have the transform.parent = hit2.transform; line.

If I manually set the scale after the other transforms back to 1,1,1 all is good, but I plan on being able to scale the objects as well. I suppose I could just store the scale of the object separately and re-apply it after other transformations.

Just not sure why it’s scaling like this in the first place. Thanks for the quick reply!

When you say “scaling” do you perhaps mean “skewing”? It may be rotations that are your problem.

I suppose it would be a bit of a skew as it does not uniformly scale the object, but re-applying the scale after adding the other transforms seems to resolve the issue.