- Home /
Add rotation to LookAt (CarSmoothFollow) ?
Hi,
I would like to add a rotation to the camera, which already uses a LookAt method. When I add this code to a file :
void Update () {
float newZ = Input.GetAxis("Horizontal") * -1 * 25;
Vector3 rotEnd = new Vector3( 0, 0, newZ );
transform.eulerAngles += rotEnd;
}
the camera does not rotate with the horizontal axis because of the lookAt method in the CarSmoothFollow script. I am trying to add it directly in the lookAt method, but it does not work correctly, it kind of repeats the rotation 100 times per second. Would you know how to do this? Here is the code :
void LateUpdate () {
wantedHeight = target.position.y + height;
currentHeight = transform.position.y;
//HORIZONTAL
float newZ = Input.GetAxis("Horizontal") * -1 * 25;
Vector3 rotEnd = new Vector3( 0, 0, newZ );
//transform.eulerAngles += rotEnd;
wantedRotationAngle = target.eulerAngles.y;
currentRotationAngle = transform.eulerAngles.y;
currentRotationAngle = Mathf.SmoothDampAngle(currentRotationAngle, wantedRotationAngle, ref yVelocity, rotationSnapTime);
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
wantedPosition = target.position;
wantedPosition.y = currentHeight;
usedDistance = Mathf.SmoothDampAngle(usedDistance, distance + (parentRigidbody.velocity.magnitude * distanceMultiplier), ref zVelocity, distanceSnapTime);
wantedPosition += Quaternion.Euler(0, currentRotationAngle, 0) * new Vector3(0, 0, -usedDistance);
transform.position = wantedPosition;
transform.LookAt(target.position + lookAtVector + rotEnd);//wrong
}
Thanks
If you rotate it in the Update() method, it will keep rotating every frame... Why don't you explain what you're trying to achieve exactly and we'll try to help.
@tbkn thanks for the comment, robertbu gave the right solution, the goal was to lean the camera to one side or the other, when the object turns to one side. It works fine now, thanks anyway!
Answer by robertbu · Oct 11, 2013 at 06:50 PM
It is hard to make a best fix without knowing the geometry of your situation. Let me answer the specific question you ask. I think you want:
transform.LookAt(target.position + lookAtVector);
transform.eulerAngles += rotEnd;
Note that given you have a look at vector (instead of a position), you can do this:
transform.roation = Quaternion.LookRotation(lookAtVector);
transform.eulerAngles += rotEnd;
And you should be able to combine the two concepts like this (untested):
transform.rotation = Quaternion.Euler(rotEnd) * Quaternion.LookAt(lookAtVector);
Thank you! So the first one works fine, I don't know why it does not work when using another file, with the Update method (while CarSmoothFollow uses LateUpdate) and located above in the inspector (don't know if the order changes anything). About the second and third solutions, the camera is trembling but the effect still works, and there is a note in the console about lookRotation with "zero". The goal was to lean the camera to one side or the other, when the object turns to one side. Anyway, thank you very much, it works fine now!
LookAt() handles a Vector3.zero situation. For the other two, you would need to place them inside an if() if you code is generating Vector3.zero for lookAtVector:
if (lookAtVector != Vector3.zero) {
Your answer
Follow this Question
Related Questions
smooth transform look at with space self rotation 0 Answers
smooth look at with offset 0 Answers
Making a camera that follows a rigidbodied sphere. 2 Answers
rotate gameobject 1 Answer
Camera following/looking at aircraft 1 Answer