/// <summary> /// 计算适口偏移值 /// </summary> /// <returns>The offset position.</returns> /// <param name="handPosition">Hand position.</param> /// <param name="grabObject">Grab object.</param> public static Vector3 GetOffsetPosition(Vector3 handPosition, GameObject grabObject) { var offset = Vector3.zero; Vector3 screenDevice = MUtility.MainWorldToScreenPoint(grabObject.transform.position); Vector3 vPos = MUtility.MainScreenToWorldPoint(new Vector3(handPosition.x, handPosition.y, screenDevice.z)); offset = vPos - grabObject.transform.position; return(offset); }
public static void SetObjectGrab(GameObject target, int handIndex = 0) { var operate = GetOperateHand(handIndex); if (operate == null) { return; } Vector3 tempPos = MUtility.MainWorldToScreenPoint(target.transform.position - new Vector3(0, 0, MUtility.MainCamera.transform.position.z)); tempPos = MUtility.MainScreenToWorldPoint(tempPos); operate.SetObjectGrab(target, tempPos.z); }
/// <summary> /// 返回是否在屏幕内 /// </summary> /// <param name="thingAttach"></param> /// <returns></returns> public static bool AttachThingPosInCamera(Transform thingAttach, Vector2 xlimits, Vector2 ylimits) { Transform camTransform = MUtility.MainCamera.transform; Vector3 dir = (thingAttach.position - camTransform.position).normalized; float dot = Vector3.Dot(camTransform.forward, dir); //判断物体是否在相机前面 Vector2 screenPos = MUtility.MainWorldToScreenPoint(thingAttach.position); if (screenPos.x < xlimits.y && screenPos.x > xlimits.x && screenPos.y < ylimits.y && screenPos.y > ylimits.x && dot > 0) { return(true); } else { return(false); } }