override public void Start() { //offset = Vector3.zero; mObjectItem.SetIsKinematic(true); mObjectItem.SetUseGravity(true); if (mObjectItem.gameObject.GetComponent <Rigidbody>() != null) { mObjectItem.gameObject.GetComponent <Rigidbody>().Sleep(); #if UNITY_2017_2_OR_NEWER #else mObjectItem.gameObject.GetComponent <Rigidbody>().WakeUp(); #endif } mObjectItem.transform.parent = hand.mPalm.transform; parentTransform = hand.mPalm.transform; parentPosition = mObjectItem.transform.localPosition; mRecord.RecordClean(); offsetDragSpeed = Vector3.zero; preConstraints = mObjectItem.GetComponent <Rigidbody>().constraints; if (mObjectItem.GetComponent <BoxCollider>() != null) { Quaternion q = Quaternion.identity; ELift_Director director = Rotation(hand.mPalm.transform, mObjectItem.transform, out q); mObjectItem.transform.rotation = q; if (hand.mPalm.mChildCollider != null) { //hand.mPalm.mChildCollider.gameObject.SetActive(true); float HPalemTemp = -hand.mPalm.mChildCollider.localPosition.y;// hand.mPalm.mChildCollider.position.y - hand.mPalm.transform.position.y; float HPalm = hand.mPalm.mChildCollider.GetComponent <BoxCollider>().size.y *hand.mPalm.mChildCollider.localScale.y; Vector3 one = mObjectItem.GetComponent <BoxCollider>().ClosestPoint(hand.mPalm.mChildCollider.transform.position); Vector3 two = hand.mPalm.mChildCollider.GetComponent <BoxCollider>().ClosestPoint(mObjectItem.transform.position); Vector3 offset = two - one; //Debug.Log("one = " + one); //Debug.Log("two = " + two); //Debug.Log("offset = "+ offset); mObjectItem.transform.position += new Vector3(0.0f, offset.y, 0.0f); switch (director) { case ELift_Director.X: case ELift_Director.XContrary: { //mObjectItem.transform.position = hand.mPalm.mChildCollider.position; float height = mObjectItem.GetComponent <BoxCollider>().size.x *mObjectItem.transform.localScale.x; float offsetTemp = hand.mPalm.mChildCollider.position.y - (mObjectItem.transform.position.y + height / 2); if (offsetTemp >= 0) { //mObjectItem.transform.position = new Vector3(0.0f, mObjectItem.transform.position.y + height / 2, 0.0f); //Debug.Log("offsetTemp X"); mObjectItem.transform.position = new Vector3(mObjectItem.transform.position.x, mObjectItem.transform.parent.transform.position.y + height / 2 - HPalm / 2 + HPalemTemp, mObjectItem.transform.position.z); //if (hand.mPalm.mChildCollider.GetComponent<BoxCollider>() != null) // mObjectItem.transform.localPosition += new Vector3(0.0f, hand.mPalm.mChildCollider.GetComponent<BoxCollider>().size.y, 0.0f); } else { //mObjectItem.transform.position = new Vector3(0.0f, mObjectItem.transform.position.y+ height / 2, 0.0f); //Debug.Log("offsetTemp X-"); mObjectItem.transform.position = new Vector3(mObjectItem.transform.position.x, mObjectItem.transform.parent.transform.position.y + height / 2 - HPalm / 2 + HPalemTemp, mObjectItem.transform.position.z); } } break; case ELift_Director.Y: case ELift_Director.YContrary: { // mObjectItem.transform.position = hand.mPalm.mChildCollider.position; float height = mObjectItem.GetComponent <BoxCollider>().size.y *mObjectItem.transform.localScale.y; float offsetTemp = hand.mPalm.mChildCollider.position.y - (mObjectItem.transform.position.y + height / 2); if (offsetTemp >= 0) { // mObjectItem.transform.position = new Vector3(0.0f, mObjectItem.transform.position.y + height / 2, 0.0f); // Debug.Log("offsetTemp Y"); mObjectItem.transform.position = new Vector3(mObjectItem.transform.position.x, mObjectItem.transform.parent.transform.position.y + height / 2 - HPalm / 2 + HPalemTemp, mObjectItem.transform.position.z); //if (hand.mPalm.mChildCollider.GetComponent<BoxCollider>() != null) // mObjectItem.transform.localPosition += new Vector3(0.0f, hand.mPalm.mChildCollider.GetComponent<BoxCollider>().size.y, 0.0f); } else { // mObjectItem.transform.position = new Vector3(0.0f, mObjectItem.transform.position.y+ height / 2, 0.0f); // Debug.Log("offsetTemp Y-"); mObjectItem.transform.position = new Vector3(mObjectItem.transform.position.x, mObjectItem.transform.parent.transform.position.y + height / 2 - HPalm / 2 + HPalemTemp, mObjectItem.transform.position.z); } } break; case ELift_Director.Z: case ELift_Director.ZContrary: { //mObjectItem.transform.position = hand.mPalm.mChildCollider.position; float height = mObjectItem.GetComponent <BoxCollider>().size.z *mObjectItem.transform.localScale.z; float offsetTemp = hand.mPalm.mChildCollider.position.y - (mObjectItem.transform.position.y + height / 2); if (offsetTemp >= 0) { //// Debug.Log("offsetTemp Z"); mObjectItem.transform.position = new Vector3(mObjectItem.transform.position.x, mObjectItem.transform.parent.transform.position.y + height / 2 - HPalm / 2 + HPalemTemp, mObjectItem.transform.position.z); //if(hand.mPalm.mChildCollider.GetComponent<BoxCollider>() != null) // mObjectItem.transform.localPosition += new Vector3(0.0f,hand.mPalm.mChildCollider.GetComponent<BoxCollider>().size.y,0.0f); } else { // Debug.Log("height = "+ offsetTemp); //mObjectItem.transform.localPosition = new Vector3(0.0f, mObjectItem.transform.localPosition.y, 0.0f); mObjectItem.transform.position = new Vector3(mObjectItem.transform.position.x, mObjectItem.transform.parent.transform.position.y + height / 2 - HPalm / 2 + HPalemTemp, mObjectItem.transform.position.z); // Debug.Log("offsetTemp Z-"); } } break; } } } else if (mObjectItem.mLifeCollider != null) { //mObjectItem.mLifeCollider.gameObject.SetActive(true); Quaternion q = Quaternion.identity; ELift_Director director = Rotation(hand.mPalm.transform, mObjectItem.mLifeCollider.transform, out q); mObjectItem.transform.rotation = q; if (hand.mPalm.mChildCollider != null) { //hand.mPalm.mChildCollider.gameObject.SetActive(true); float HPalemTemp = -hand.mPalm.mChildCollider.localPosition.y;// hand.mPalm.mChildCollider.position.y - hand.mPalm.transform.position.y; float HPalm = hand.mPalm.mChildCollider.GetComponent <BoxCollider>().size.y *hand.mPalm.mChildCollider.localScale.y; Vector3 one = mObjectItem.mLifeCollider.GetComponent <BoxCollider>().ClosestPoint(hand.mPalm.mChildCollider.transform.position); Vector3 two = hand.mPalm.mChildCollider.GetComponent <BoxCollider>().ClosestPoint(mObjectItem.mLifeCollider.transform.position); Vector3 offset = two - one; //Debug.Log("one = " + one); //Debug.Log("two = " + two); //Debug.Log("offset = "+ offset); //mObjectItem.transform.position += new Vector3(0.0f, offset.y, 0.0f); switch (director) { case ELift_Director.X: case ELift_Director.XContrary: { //mObjectItem.transform.position = hand.mPalm.mChildCollider.position; float height = mObjectItem.mLifeCollider.GetComponent <BoxCollider>().size.x *mObjectItem.mLifeCollider.transform.localScale.x; float offsetTemp = hand.mPalm.mChildCollider.position.y - (mObjectItem.mLifeCollider.transform.position.y + height / 2); if (offsetTemp >= 0) { //mObjectItem.transform.position = new Vector3(0.0f, mObjectItem.transform.position.y + height / 2, 0.0f); //Debug.Log("offsetTemp X"); mObjectItem.transform.position = new Vector3(mObjectItem.transform.position.x, mObjectItem.transform.parent.transform.position.y + height / 2 - HPalm / 2 + HPalemTemp, mObjectItem.transform.position.z); //if (hand.mPalm.mChildCollider.GetComponent<BoxCollider>() != null) // mObjectItem.transform.localPosition += new Vector3(0.0f, hand.mPalm.mChildCollider.GetComponent<BoxCollider>().size.y, 0.0f); } else { //mObjectItem.transform.position = new Vector3(0.0f, mObjectItem.transform.position.y+ height / 2, 0.0f); // Debug.Log("offsetTemp X-"); mObjectItem.transform.position = new Vector3(mObjectItem.transform.position.x, mObjectItem.transform.parent.transform.position.y + height / 2 - HPalm / 2 + HPalemTemp, mObjectItem.transform.position.z); } } break; case ELift_Director.Y: case ELift_Director.YContrary: { // mObjectItem.transform.position = hand.mPalm.mChildCollider.position; float height = mObjectItem.mLifeCollider.GetComponent <BoxCollider>().size.y *mObjectItem.mLifeCollider.transform.localScale.y; float offsetTemp = hand.mPalm.mChildCollider.position.y - (mObjectItem.mLifeCollider.transform.position.y + height / 2); if (offsetTemp >= 0) { // mObjectItem.transform.position = new Vector3(0.0f, mObjectItem.transform.position.y + height / 2, 0.0f); //Debug.Log("offsetTemp Y"); mObjectItem.transform.position = new Vector3(mObjectItem.transform.position.x, mObjectItem.transform.parent.transform.position.y + height / 2 - HPalm / 2 + HPalemTemp, mObjectItem.transform.position.z); //if (hand.mPalm.mChildCollider.GetComponent<BoxCollider>() != null) // mObjectItem.transform.localPosition += new Vector3(0.0f, hand.mPalm.mChildCollider.GetComponent<BoxCollider>().size.y, 0.0f); } else { // mObjectItem.transform.position = new Vector3(0.0f, mObjectItem.transform.position.y+ height / 2, 0.0f); //Debug.Log("offsetTemp Y-"); mObjectItem.transform.position = new Vector3(mObjectItem.transform.position.x, mObjectItem.transform.parent.transform.position.y + height / 2 - HPalm / 2 + HPalemTemp, mObjectItem.transform.position.z); } } break; case ELift_Director.Z: case ELift_Director.ZContrary: { //mObjectItem.transform.position = hand.mPalm.mChildCollider.position; float height = mObjectItem.mLifeCollider.GetComponent <BoxCollider>().size.z *mObjectItem.mLifeCollider.transform.localScale.z; float offsetTemp = hand.mPalm.mChildCollider.position.y - (mObjectItem.mLifeCollider.transform.position.y + height / 2); if (offsetTemp >= 0) { // Debug.Log("offsetTemp Z"); mObjectItem.transform.position = new Vector3(mObjectItem.transform.position.x, mObjectItem.transform.parent.transform.position.y + height / 2 - HPalm / 2 + HPalemTemp, mObjectItem.transform.position.z); //if(hand.mPalm.mChildCollider.GetComponent<BoxCollider>() != null) // mObjectItem.transform.localPosition += new Vector3(0.0f,hand.mPalm.mChildCollider.GetComponent<BoxCollider>().size.y,0.0f); } else { //mObjectItem.transform.localPosition = new Vector3(0.0f, mObjectItem.transform.localPosition.y, 0.0f); mObjectItem.transform.position = new Vector3(mObjectItem.transform.position.x, mObjectItem.transform.parent.transform.position.y + height / 2 - HPalm / 2 + HPalemTemp, mObjectItem.transform.position.z); //Debug.Log("offsetTemp Z-"); } } break; } //mObjectItem.mLifeCollider.gameObject.SetActive(false); } } // mObjectItem.SetIsLockRotation(true); hand.mPalm.OpenPhyCollider(true); }
private ELift_Director Rotation(Transform other, Transform objectItem, out Quaternion q) { ELift_Director director = ELift_Director.Y; float angle1 = Vector3.Angle(other.transform.up, objectItem.transform.up); float angle11 = 180.0f - angle1; float angle2 = Vector3.Angle(other.transform.up, objectItem.transform.right); float angle22 = 180.0f - angle2; float angle3 = Vector3.Angle(other.transform.up, objectItem.transform.forward); float angle33 = 180.0f - angle3; List <float> angles = new List <float>(); angles.Add(angle1); angles.Add(angle11); angles.Add(angle2); angles.Add(angle22); angles.Add(angle3); angles.Add(angle33); angles.Sort((x, y) => x.CompareTo(y)); if (angle1 == angles[0]) { // Debug.Log("Y +"); Vector3 normal = other.transform.up; Vector3 up = objectItem.transform.up; Vector3 YRotationNomal = Vector3.Cross(up, normal).normalized; float angle = Vector3.Angle(up, normal); Quaternion r = new Quaternion(Mathf.Sin(angle / 2 * Mathf.Deg2Rad) * YRotationNomal.x, Mathf.Sin(angle / 2 * Mathf.Deg2Rad) * YRotationNomal.y, Mathf.Sin(angle / 2 * Mathf.Deg2Rad) * YRotationNomal.z, Mathf.Cos(angle / 2 * Mathf.Deg2Rad)); q = r * objectItem.transform.rotation; director = ELift_Director.Y; // transform.Rotate(YRotationNomal, angle, Space.World); } else if (angle11 == angles[0]) { //Debug.Log("Y -"); Vector3 normal = -other.transform.up; Vector3 up = objectItem.transform.up; Vector3 YRotationNomal = Vector3.Cross(up, normal).normalized; float angle = Vector3.Angle(up, normal); //Debug.Log(angle); Quaternion r = new Quaternion(Mathf.Sin(angle / 2 * Mathf.Deg2Rad) * YRotationNomal.x, Mathf.Sin(angle / 2 * Mathf.Deg2Rad) * YRotationNomal.y, Mathf.Sin(angle / 2 * Mathf.Deg2Rad) * YRotationNomal.z, Mathf.Cos(angle / 2 * Mathf.Deg2Rad)); q = r * objectItem.transform.rotation; director = ELift_Director.YContrary; } else if (angle2 == angles[0]) { //Debug.Log("X +"); Vector3 normal = other.transform.up; Vector3 up = objectItem.transform.right; Vector3 YRotationNomal = Vector3.Cross(up, normal).normalized; float angle = Vector3.Angle(up, normal); Quaternion r = new Quaternion(Mathf.Sin(angle / 2 * Mathf.Deg2Rad) * YRotationNomal.x, Mathf.Sin(angle / 2 * Mathf.Deg2Rad) * YRotationNomal.y, Mathf.Sin(angle / 2 * Mathf.Deg2Rad) * YRotationNomal.z, Mathf.Cos(angle / 2 * Mathf.Deg2Rad)); q = r * objectItem.transform.rotation; director = ELift_Director.X; } else if (angle22 == angles[0]) { //Debug.Log("X -"); Vector3 normal = -other.transform.up; Vector3 up = objectItem.transform.right; Vector3 YRotationNomal = Vector3.Cross(up, normal).normalized; float angle = Vector3.Angle(up, normal); Quaternion r = new Quaternion(Mathf.Sin(angle / 2 * Mathf.Deg2Rad) * YRotationNomal.x, Mathf.Sin(angle / 2 * Mathf.Deg2Rad) * YRotationNomal.y, Mathf.Sin(angle / 2 * Mathf.Deg2Rad) * YRotationNomal.z, Mathf.Cos(angle / 2 * Mathf.Deg2Rad)); q = r * objectItem.transform.rotation; director = ELift_Director.XContrary; } else if (angle3 == angles[0]) { //Debug.Log("Z +"); Vector3 normal = other.transform.up; Vector3 up = objectItem.transform.forward; Vector3 YRotationNomal = Vector3.Cross(up, normal).normalized; float angle = Vector3.Angle(up, normal); Quaternion r = new Quaternion(Mathf.Sin(angle / 2 * Mathf.Deg2Rad) * YRotationNomal.x, Mathf.Sin(angle / 2 * Mathf.Deg2Rad) * YRotationNomal.y, Mathf.Sin(angle / 2 * Mathf.Deg2Rad) * YRotationNomal.z, Mathf.Cos(angle / 2 * Mathf.Deg2Rad)); q = r * objectItem.transform.rotation; director = ELift_Director.Z; } else if (angle33 == angles[0]) { //Debug.Log("Z -"); Vector3 normal = -other.transform.up; Vector3 up = objectItem.transform.forward; Vector3 YRotationNomal = Vector3.Cross(up, normal).normalized; float angle = Vector3.Angle(up, normal); Quaternion r = new Quaternion(Mathf.Sin(angle / 2 * Mathf.Deg2Rad) * YRotationNomal.x, Mathf.Sin(angle / 2 * Mathf.Deg2Rad) * YRotationNomal.y, Mathf.Sin(angle / 2 * Mathf.Deg2Rad) * YRotationNomal.z, Mathf.Cos(angle / 2 * Mathf.Deg2Rad)); q = r * objectItem.transform.rotation; director = ELift_Director.ZContrary; } else { q = objectItem.transform.rotation; } return(director); }