Hello guys ![]()
i want to create a 3d line (roughly → see the picture what my object really looks like) between two positions (start and end).
So i thought i could position the game object like this:
- Place the starting point of the line (left point in the picture) at the starting position
- Get the current direction of my line (vector between the start and end point of the line) and the one between the start and end point i want to connect with the line (calculation of the corresponding vector between this start and end position)
- Use Quaternion.FromToRotation(currentDirectionOfLine, directionBetweenPositions) to rotate the end point of the line to the end position i want to reach
As you can see all is good as long as the end point i want to reach is right from the starting point (altough i didn’t found out yet how to display the UI text properly in 3D ^^). But as soon as the end point lies left of the start point my line bustles between several wrong positions and never stands still.
I heard of the problem with quaternions and rotations over more than 180 degrees but isn’t Quaternion.FromToRotation supposed to avoid this?
I also tried several other solutions like calculating the rotation angles to reach the end point myself. they were correctly calculated and worked fine if the end point is right of the starting point, too. But had the same problem if the end point is left of the starting point altough the line didn’t bustle but was rotated almost right except for some angle around the z-axis. I tried to fix this with re-adjustments after the first rotation which yields the slightly wrong rotation but this also lead to a wrong rotation.
So i am really desperate how i can solve this? My current code is this (sorry for the long instructions somehow distorting the display of the code):
//creates the visualization instance and set the image data of the corresponding image view as the parent
m_measurementVisualization = (GameObject)GameObject.Instantiate(MeasurementManager.Instance.m_distanceMeasurementPrefab3D, new Vector3(0, 0, 0), Quaternion.identity)).GetComponent<DistanceMeasurementVisualization3D>();
m_measurementVisualization.transform.SetParent(m_correspondingImageView.m_imageData.transform);
m_measurementVisualization.transform.localPosition = start.LocalPosition;
(m_measurementVisualization as DistanceMeasurementVisualization3D).scaleToLength((float)m_measurementValue);
m_measurementVisualization.transform.localScale = HelperFunctions.calcLocalFromGlobalScale(m_measurementVisualization.gameObject, new Vector3(0, MeasurementManager.Instance.adaptLineThickness3DToZoom(), MeasurementManager.Instance.adaptLineThickness3DToZoom())) + new Vector3(m_measurementVisualization.transform.localScale.x, 0, 0);
m_measurementVisualization.transform.rotation *= Quaternion.FromToRotation((m_measurementVisualization as DistanceMeasurementVisualization3D).getCurrentDirection(), globalPositionEnd - start.GlobalPosition);
