system
January 10, 2012, 5:19pm
1
transform.forword is (0, 0, 1) object is,
transform.LookAt( Camera.main.transform.position - transform.position );
this code has been processed, object facing the camera.
so, Hierarchical data model,
child is affected matrix of the parent. and not facing (0, 0, 1).
how can the child object facing to camera?
I want to smooth rotation if possible. so,
Quternion.Slerp( transform.rotation,
Camera.main.transform.position - transform.position,
Time.deltaTime * speed );`
I want to include this code.
but I can not well…
You’re making some mistakes here: transform.LookAt may receive a Transform or a position as argument, not a direction vector, thus you should use transform.LookAt(Camera.main.transform) or transform.LookAt(Camera.main.transform.position).
Your Slerp is also wrong, because you should pass a Quaternion as the target rotation, not a vector: use the vector to find the rotation with Quaternion.LookRotation , then pass this rotation as the 2nd argument, like this:
var targetRot = Quaternion.LookRotation(Camera.main.transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, Time.deltaTime * speed );`
This code must be part of Update, FixedUpdate or a coroutine to work fine.
system
January 12, 2012, 11:34am
3
Somehow I solve this prebrem.
Save the direction to child object facing camera.
defaultDirToCam = (Camera.main.transform.position - parent.position).normalized;
defaultDirToCam.y = 0.0f;
defaultEuler = transform.eulerAngles;
then, on update method,
// object direction to camera
Vector3 parentDirToCam = (Camera.main.transform.position - parent.position).normalized;
// relative to camera position
Vector3 relativePosition
= parent.InverseTransformPoint(Camera.main.transform.position);
// differential y-axis
float yDiffDeg
= Vector3.Angle( defaultDirToCam, new Vector3(parentDirToCam.x, 0.0f, parentDirToCam.z) );
// differential x-axis
float xDiffDeg
= Vector3.Angle( -Vector3.forward, new Vector3(0.0f, parentDirToCam.y, defaultDirToCam.z) );
// direction check
if ( relativePosition.x < 0.0f ) {
yDiffDeg = -yDiffDeg;
}
if ( relativePosition.y < 0.0f ) {
xDiffDeg = -xDiffDeg;
}
// Relative to default rotation
transform.rotation = Quaternion.Slerp(
transform.rotation,
Quaternion.Euler( defaultEuler.x, defaultEuler.y + yDiffDeg, defaultEuler.z + xDiffDeg ),
Time.deltaTime * chaseCameraSpeed);
I was able to solve it by this above code.
thanks.