Box Pick up and movement

Hey,
I have figured out picking up the box and moving it around however I still need a good way to detect if I am looking at the box. right now I have a trigger added to my camera and I am using

Function OnTriggerStay()

however it is not always detecting when the trigger is in a box

here is the script that I am using to interact with the boxes

function Update(){

	if(Input.GetKeyDown(KeyCode.F)){

		boxaction = true;

	}else if(Input.GetKeyUp(KeyCode.F)){

		boxaction = false;

	}

}



function OnTriggerStay(other : Collider){

	Debug.Log("collider is in box");

	Debug.Log(other.tag);

	Debug.Log(this.tag);

	var clone : GameObject;

	cube = other.collider.gameObject;

	if (other.collider.tag == "Cube" && boxaction && cubehold == false){

		cubehold = true;

		clone = Instantiate(cube, cube.transform.position, cube.transform.rotation);

		Destroy(cube);

		clone.transform.parent = Camera.main.transform;

		clone.rigidbody.isKinematic = true;
			

	}else if(other.collider.tag == "Cube" && boxaction == false){

		cubehold = false;

		other.transform.parent = null;

		other.rigidbody.isKinematic = false;

	}

}

Why is the trigger attached to the camera not always detecting the box?

Also I am having problems that when I pick my boxes up the mesh tends to deform, does anyone know why this is and how I can stop it?

Any help would be appreciated. Thanks

Be aware that OnTriggerStay only registers colliders that have entered it (and have a rigidbody attached); for example if the box starts in the trigger OnTriggerStay will not be called, I would imagine similar things would happen if you teleported the box in without crossing the threshold so to speak.

The way you’re going about this seems a bit weird, e.g.

clone = Instantiate(cube, cube.transform.position, cube.transform.rotation);
Destroy(cube);

is meaningless as you’re creating a copy of the cube at the exact same position/rotation and then destroying the original, effectively doing nothing; parenting the cube is fine but since you don’t move it anywhere ( e.g. to the position of the player’s “hands”) it might stay out of your trigger’s range anyway since it’s not getting any close to the player.

In terms of telling what you’re looking at you can use a raycast with the Camera’s position+forward and then get the gameObject from that. I suggest looking up a full tutorial for this rather than trying to mash together pieces of different people’s code (imo).