- Home /
Edit SmoothFollow.js to change height and distance vars smoothly on some triggers
SmoothFollow.js is attached to main camera and it looks at player. I have couple of places in the game, that need other height and distance variable values, like I usually have distance 3.0 and height 2.0, than when I enter trigger I want to change camera position smoothly to distance 5.0 and height 0.5. And on trigger exit I want to smoothly change them back.
I've killed couple of hours on this, the values change, but not smoothing at all (except height damping smoothes movement a bit, but it still looks jaggy). Triggering is okay and script called CollisionItem manages it fine, please help me with smoothing part.
// Standart SmoothFollow.js var target : Transform; var distance = 3.0; var height = 2.0; var heightDamping = 2.0; var rotationDamping = 3.0;
//||||||||| My addition to the script. Start |||||||||\\ var normalDistance = 0.0; var normalHeight = 0.0; var spiralDistance = 5.0; var spiralHeight = 0.3; private var mysteriousVelocity = 0.0; var smoothTime = 1.0;
function Start () { normalDistance = distance; normalHeight = height; }
function LateUpdate () { if (CollisionItem.spiralInside) { distance = Mathf.SmoothDamp(normalDistance, spiralDistance, mysteriousVelocity, smoothTime); height = Mathf.SmoothDamp(normalHeight, spiralHeight, mysteriousVelocity, smoothTime); } else if (CollisionItem.spiralExited) { distance = Mathf.SmoothDamp(spiralDistance, normalDistance, mysteriousVelocity, smoothTime); height = Mathf.SmoothDamp(spiralHeight, normalHeight, mysteriousVelocity, smoothTime); } //||||||||| My addition to the script. End |||||||||\\
// Early out if we don't have a target
if (!target)
return;
// Calculate the current rotation angles
wantedRotationAngle = target.eulerAngles.y;
wantedHeight = target.position.y + height;
currentRotationAngle = transform.eulerAngles.y;
currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
transform.position.y = currentHeight;
// Always look at the target
transform.LookAt (target);
}
i also need to know this. i think it is not possible in unity
Answer by DewDrop · Oct 26, 2012 at 09:41 AM
i think you can use global variables, as "var static height" and "var static distance". so you can alter the height and distance from other scripts.
i hope it will help you.