- Home /
This post has been wikified, any user with enough reputation can edit it.
Need help on the 3ds max style camera control
Hello unity folks, i'v been using 3ds max style camera contol from unify community (http://wiki.unity3d.com/index.php?title=MouseOrbitZoom) with a little modifications, this code controls the camera movement with respect to target object the left click and drag for rotation around target, middle button click and drag for panning around target and right click and drag for zooming around target. the zoom control have smoothing effect using lerp (after the right click is released it eases slowly and stops) but the other controls panning and rotating not smooth(stop their motion right after the click is released). Can you help me add easing effect for panning and rotating? Here is the partial code
if (Input.GetMouseButton(1))
{
desiredDistance -= Input.GetAxis("Mouse Y") * Time.deltaTime * zoomRate*0.125f * Mathf.Abs(desiredDistance);
}
// If middle mouse and left alt are selected? ORBIT
else if (Input.GetMouseButton(0))
{
xDeg += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
yDeg -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
////////OrbitAngle
//Clamp the vertical axis for the orbit
yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
// set camera rotation
desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
currentRotation = transform.rotation;
rotation = Quaternion.Lerp(currentRotation, desiredRotation, Time.deltaTime * zoomDampening);
transform.rotation = rotation;
}
// otherwise if middle mouse is selected, we pan by way of transforming the target in screenspace
else if (Input.GetMouseButton(2))
{
//grab the rotation of the camera so we can move in a psuedo local XY space
target.rotation = transform.rotation;
target.Translate(Vector3.right * -Input.GetAxis("Mouse X") * panSpeed);
target.Translate(transform.up * -Input.GetAxis("Mouse Y") * panSpeed, Space.World);
}
////////Orbit Position
// affect the desired Zoom distance if we roll the scrollwheel
desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
//clamp the zoom min/max
desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
// For smoothing of the zoom, lerp distance
currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDampening);
// calculate position based on the new currentDistance
position = target.position - (rotation * Vector3.forward * currentDistance + targetOffset);
transform.position = position;
Thankyou.
Comment