1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
// Pour la composante Y du point de l'écran, je soustrais la hauteur de l'écran à la position du point IR car les coordonnées écran se font en partan du bas-gauche alors que les coordonnées IR se font en partant du haut-gauche.
ray = Camera.main.ScreenPointToRay(new Vector3(controllerData.virtualX, Screen.height - controllerData.virtualY));
Vector3 vecUpDown = new Vector3(0, ray.direction.y, ray.direction.z);
Vector3 vecLeftRight = new Vector3(ray.direction.x, 0, ray.direction.z);
vecUpDown.Normalize();
vecLeftRight.Normalize();
float dotUpDown = Vector3.Dot(vecUpDown, Vector3.forward);
float dotLeftRight = Vector3.Dot(vecLeftRight, Vector3.forward);
Debug.Log(vecUpDown);
float angleUpDown = Mathf.Acos(dotUpDown) * Mathf.Rad2Deg;
float angleLeftRight = Mathf.Acos(dotLeftRight) * Mathf.Rad2Deg;
Vector3 crossUpDown = Vector3.Cross(vecUpDown, Vector3.right);
Vector3 crossLeftRight = Vector3.Cross(vecLeftRight, Vector3.up);
crossUpDown.Normalize();
crossLeftRight.Normalize();
float dotSignUpDown = Vector3.Dot(Vector3.forward, crossUpDown);
float dotSignLeftRight = Vector3.Dot(Vector3.forward, crossLeftRight);
if (ValueBetween(-1.0F, 0.0F, dotSignUpDown))
angleUpDown = -angleUpDown;
if (ValueBetween(-1.0F, 0.0F, dotSignLeftRight))
angleLeftRight = -angleLeftRight;
Quaternion rotationLeftRight = Quaternion.Euler(0, angleLeftRight, 0);
Quaternion rotationUpDown = Quaternion.Euler(angleUpDown, 0, 0);
Vector3 position = ray.direction * sphericalRay + ray.origin;
objectSelected.localRotation = rotationLeftRight * rotationUpDown;
objectSelected.position = position; |
Partager