- Home /
Im soo close. How can i tweak my camera's rotation?
var target:Transform;
var distance:float = 10.0;
var height:float = 5.0;
var smoothTime = 2.5;
function Update ()
{
if(!target)
return;
currentRot = Quaternion(transform.rotation.x, transform.rotation.y, transform.rotation.z, transform.rotation.w);
targetRot = target.rotation;
newRot = Quaternion.Lerp(currentRot, targetRot, smoothTime * Time.deltaTime);
transform.rotation = newRot;
transform.position = target.position - (transform.forward*distance)+(target.up*height);
}
This is the current script i am using. My player rotates in every axis and this script allows the camera to follow behind the character no matter what rotation. When i attached it to my camera it follows my character perfectly. However, the camera is not angled to face the character directly. All i need to do is somehow tweak the camera's local x rotation so its looking down a tad but i dont know how to go about that with this script. If anyone could help that would be great Thanks c:
I am just so lost when it comes to these:
transform.rotation
transform.eulerAngles
transform.localEulerAngles
Quaternion.Euler
Quaternion.EulerAngles
Quaternion's stupid w value
WooHoo i forgot i could combine rotations using '*='
$$anonymous$$y new script:
function Update () { if(!target) return;
currentRot = transform.rotation;
targetRot = target.rotation;
targetRot *= Quaternion.Euler(offset, 0, 0);
newRot = Quaternion.Lerp(currentRot, targetRot, smoothTime * Time.deltaTime);
transform.rotation = newRot;
transform.position = target.position - (transform.forward*distance)+(target.up*height);
}
Answer by robertbu · Aug 12, 2013 at 05:01 PM
One easy way to do this is to make create an empty game object, position the empty game object at the same position as the camera, and then make the camera a child of the empty game object by dragging and dropping on the empty game object in the inspector. The script above goes on the empty game object. You can then rotate your camera a bit on the 'x' axis to create a relative rotation wrt the parent object.
It is unlikely you should ever have to deal with the 'w' in a Quaternion. Line 11 could better be written as:
currentRot = transform.Rotation;
Depending on where you want the camera to look, and another way to achieve your goal her would be to use Quaternion.LookRotation(). The code for lines 11 and 12 would look like:
CurrentRot = transform.rotation;
targetRot = Quaternion.LookRotation(target.position - transform.position, target.up);