- Home /
Problems with the orientation of an gameObject
Good evening everyone, I have a problem, and it is the one shown in the gif https://drive.google.com/file/d/1zbqrpi5uPyEKfNduKvPjHEyKZnZzvz0L/view?usp=sharing , the selected blue object is right(derecha), and the green is left(izquierda) , what the cube does is point to the direction generated by the vector between right and left (as shown in the code). Now the blue line would be the forward or where you should point the box to maintain the orientation. This problem occurs when I put right or left over the other, or in other words, when the product point of the vector generated between right and left and Vector3.up, is close to 1, so it is almost parallel to Vector3.up. What I want to do is that the blue vector maintains its orientation. Beforehand thank you very much.
private void Update()
{
Vector3 posRight = right.position;
Vector3 posLeft = left.position;
Vector3 vectorX;
if (posRight.y > posLeft.y)
vectorX = (posRight - posLeft).normalized;
else
vectorX = (posLeft - posRight).normalized;
float dot = Vector3.Dot(vectorX, Vector3.up);
print(dot);
vectorForward = (posRight - posLeft).normalized;
Vector3 halfPos = Vector3.Lerp(right.position, left.position, 0.5f);
transform.position = halfPos;
Quaternion rotLook = Quaternion.LookRotation(vectorForward);
Debug.DrawLine(posRight, posLeft, Color.red);
posRight.y = posLeft.y = transform.position.y;
Vector3 directionForward = (posLeft - posRight).normalized;
directionForward = Quaternion.Euler(0, -90, 0) * directionForward;
Debug.DrawRay(halfPos, directionForward, Color.blue);
transform.rotation = rotLook;
}
Your answer
Follow this Question
Related Questions
Offset raycast by specific amount 1 Answer
Angle between two vectors rotated by quaternion? 0 Answers
Changing the forward rotation for LookAt 2 Answers
Detecting when objects are facing each other 4 Answers
What could be the reason 1 Answer