Link object rotation to its parent

I am reasonably new to programming in unity and I have been stuck with this issue for a couple of days now…

What I want is for an object that is instantiated on the press of a button then set to the child of the FPS Controller to always be facing the camera when you look around.

My current code is this (Its probably very messy):

var objectOut = false;

var prefabs : GameObject[];

var maxDistance = 5;
var minDistance = 1;

var obj;

function Update () {
 	Screen.lockCursor = true;
	if(Input.GetKeyUp("1") || Input.GetKeyUp("2") || Input.GetKeyUp("3") || Input.GetKeyUp("4")){
		if(objectOut == false){
			objectOut = true;
			
			if(Input.GetKeyUp("1")){
				obj = Instantiate (prefabs[0], Vector3(0, 0, 0), Quaternion.identity);
			}else if(Input.GetKeyUp("2")){
				obj = Instantiate (prefabs[1], Vector3(0, 0, 0), Quaternion.identity);
			}else if(Input.GetKeyUp("3")){
				obj = Instantiate (prefabs[2], Vector3(0, 0, 0), Quaternion.identity);
			}else if(Input.GetKeyUp("4")){
				obj = Instantiate (prefabs[3], Vector3(0, 0, 0), Quaternion.identity);
			}
			
			obj.transform.collider.enabled = true;
			obj.layer = 2;
			obj.transform.parent = gameObject.transform;
			obj.transform.rotation = gameObject.transform.rotation;
			obj.renderer.material.color.a = 0.5;
			
			var Hit : RaycastHit;
			Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), Hit);
			
			obj.transform.localPosition = Vector3(0,2,1);
		}else{
			Destroy(obj);
			objectOut = false;
		}
    }
    
    if(objectOut){
    	if(Input.GetAxis("Fire1")){
    		objectOut = false;
    		obj.transform.parent = null;
    		obj.layer = 0;
    		obj.transform.collider.enabled = true;
    	}else{
	    	
	    	var rcHit : RaycastHit;
			var theRay : Vector3 = obj.transform.TransformDirection(Vector3.down);
			
			if (Physics.Raycast(obj.transform.position, theRay, rcHit)){
				var GroundDis = rcHit.distance;
			    obj.transform.rotation = Quaternion.FromToRotation(Vector3.up, rcHit.normal);
			    obj.transform.localPosition.y = (obj.transform.localPosition.y - GroundDis) + obj.collider.bounds.size.y / 2;
		    }
	    	
	    	if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), Hit, maxDistance)){
	    		if(Hit.distance >= minDistance){
					obj.transform.position.x = Hit.point.x;
					obj.transform.position.z = Hit.point.z;
				}else{
					obj.transform.localPosition.z = minDistance;
				}
			}else{
				obj.transform.localPosition.z = maxDistance;
			}
		}	
    }
}

When I add this:

obj.transform.localRotation.w = 0;

which I would expect to work, it results in some weird rotation of the object and even the object sinking through the ground when I move it around on slopes…

Any help would be appreciated!

Inside LateUpdate (so you’re sure it’s executed before camera render but after the parent transform has changed - if he uses Update), just write:

myFacingObject.transform.LookAt(myCamera.transform, Vector3.up);

Also, since you would be using your object’s and your camera’s transforms at every update, I’d suggest caching them in a couple of variables.