Strange problem with LookRotation & Slerp

Hi,

I’m trying to rotate an empty game object with its children in a way that the selected child is in front to the camera:

I use this code for the task:

if(Input.GetKeyDown(KeyCode.A))
{
    int x = Random.Range(0, direction.Length);
    start = true;
    startRotation = transform.rotation;
    progress = speed;
    targetRotation = Quaternion.LookRotation(direction[x].localPosition);
    Debug.Log(direction[x].name + " " +  targetRotation.ToString());
}

if (direction != null && start)
{
/*    Debug.Log(progress);
    Debug.Log(startRotation);
    Debug.Log(targetRotation);*/
    float str = Mathf.Min(0.5f * Time.deltaTime, 1);
    transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, progress);
    if(progress >= 1f)
    {
        transform.rotation = targetRotation;
        start = false;
    }
    progress += speed;
}

It works perfect with the sphere, capsule and the “diamond” (upper cube), but the cylinder and the (normal) cube switch places when I get to them. The script chooses the cube but the cylinder comes to the front and visa verse.

The cube has the position (0, 0, 1) and the cylinder (0, 0, -1). I found out that this happens if the z value is different then 0. What can I do, that this code snip works as I want to :slight_smile:

Thanks in advance

Alex

Hi again,

I found a workaround for my problem. I inverted the z value in line 7 of my code. I did this with this new line:

targetRotation = Quaternion.LookRotation(new Vector3(direction[x].localPosition.x, direction[x].localPosition.y , -direction[x].localPosition.z));

But I am still interested why this happens, did I do somthing wrong or is this “problem” something that has a reason…

Thanks again

Alex