1
votes

I have no idea whats happening here but need a solution. Its a very simple task - I have an empty that contains an armature/body mesh that I move around using ITween, and I need the body to rotate to face the object it is moving towards.

My problem is my armature inside my container object has z axis up, and for whatever reason using any lookAt rotation for either the child armature or larger container is off by 90 degrees so the left side of object is facing the target.

enter image description here enter image description here

I've tried:

Vector3 relativePos = test.transform.position - transform.position;
        Quaternion rotation = Quaternion.LookRotation(relativePos);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 1f);

Ive been trying to use both LookAt and iTween, none work. I know that iTween is working on rotation because this makes the object rotate by 90 degrees on the z axis (I just need the object to face the target on the Vector3.up axis - where mainModel is the armature:

 iTween.RotateTo(mainModel, iTween.Hash(
              "z", this.gameObject.transform.eulerAngles.x + 90,
              "time", 1f,
              "easetype", "easeInOutSine",
              "looptype", "pingpong"
            ));

iTween either doesn't work when I replace with a LookRotation at the target, or faces to left.

How get I offset the LookRotation by 90 degrees on the up axis? What is wrong here?

1
I understand you want to make it lerp/tween but when you do transform.LookAt(test.transform.position);, the only problem is that it's looking left?Ruzihm
It turns, but 1. with lookAt its too abrupt but 2. the right side of the object faces the target, not the front @Ruzihmskyguy

1 Answers

1
votes

You can multiply the result of LookRotation with a modifying Quaternion to offset the rotation you need. And to get lerping, you can use RotateTowards to set an appropriate max rotation speed.

// Find out where we want to look    
Quaternion targetRotation = Quaternion.LookRotation(test.transform.position 
                                                    - transform.position);
targetRotation *= Quaternion.Euler(0f, 90f, 0f); // might need to be -90f


// Find out how far to rotate
float maxDegreesPerSecond = 90f; // This is your max speed for rotation in degrees/sec
float maxDegreesDelta = maxDegreesPerSecond * Time.deltaTime; 
transform.rotation = Quaternion.RotateTowards(transform.rotation,
                                                        targetRotation,
                                                        maxDegreesDelta); 

To avoid tilting up/down, you can zero out the direction before you feed it into LookRotation:

// Find out where we want to look 
Vector3 targetDirection = test.transform.position - transform.position;
targetDirection = new Vector3(targetDirection.x, 0f, targetDirection.z);

Quaternion targetRotation = Quaternion.LookRotation(targetDirection);
targetRotation *= Quaternion.Euler(0f, 90f, 0f); // might need to be -90f