// this function calcuates the initial position of the model in relation to the target's local space. // it assuems that the target's transform is in world coordination. public Vector3 CalcInitialLocalPosOfModelToTarget(NCARModelInfo modelInfo, global::NCARTargetInfo targetInfo) { //this part is inverse of local to global (inverse function of TransformPoint) Quaternion rotTargetInit = targetInfo.rotTargetInit; Vector3 scaleTargetInit = targetInfo.scaleTargetInit; Vector3 posTargetInit = targetInfo.posTargetInit; Vector3 posModelInit = modelInfo.posInit; Vector3 t = Quaternion.Inverse(rotTargetInit) * (posModelInit - posTargetInit); t = Vector3.Scale(new Vector3(1 / scaleTargetInit.x, 1 / scaleTargetInit.y, 1 / scaleTargetInit.z), t); return(t); }
//based on the function above, this function moves the model onto the target setting it as the model's parent public void MoveModelOnTarget(NCARModelInfo modelInfo, global::NCARTargetInfo targetInfo) { Transform modelTransfrom = modelInfo.transform; Vector3 initLocal = CalcInitialLocalPosOfModelToTarget(modelInfo, targetInfo); modelTransfrom.position = targetInfo.transform.TransformPoint(initLocal); modelTransfrom.rotation = (modelInfo.rotInit * targetInfo.transform.rotation) * Quaternion.Inverse(targetInfo.rotTargetInit); modelTransfrom.SetParent(targetInfo.transform); if (modelInfo.GetComponent <NCARTouchController>() != null) { modelInfo.GetComponent <NCARTouchController>().setInitialLocalPosition(); } modelInfo.currentTarget = targetInfo; //show model in case it was hidden; modelInfo.ShowModel(); }