# Smooth move + orbit camera around target

Hey guys, got a tough one here.
I have 2 versions of my camera code…

One version creates a smooth camera movement with different dampening for different directions (this version doesn’t turn or orbit, it just follows the target sideways).

The other version is basically the orbit camera code from the standard assets with minor mods.

What i’d like to do is take my custom smooth camera code and make it orbit around the target. Here’s my custom smooth camera code:

``````public void SmoothCamFollow ( Transform target, Transform camTransform, bool getRelativePos ) {

if( getRelativePos )
{
relativeBikePos = Mathf.Abs( Mathf.Clamp( target.InverseTransformPoint( camTransform.position ).y, camMov.maxCamPosZ, 0f ) );
}

// Get current velocity of the target.
bikeVelocity = target.rigidbody.velocity;

// we define some temporary variables for raycasting...
// Starting point of the ray.
rayStart = target.transform.position + new Vector3( 0, 2, 0 );

RaycastHit hit;

// Cast a ray forward to check if terrain is going uphill (used for both cameras)...
if( Physics.Raycast( rayStart, rayDirRight, out hit, camMov.rayLenghtRight, layerMask ) )
{
goingUpHill = true;
}
else
{
goingUpHill = false;
}

// We now calculate the offset of the camera by adding camOffset to the target's position.
camPosX = target.position.x + camMov.camOffsetX;
camPosY = target.position.y + camMov.camOffsetY;
camPosZ = target.position.z + camMov.camOffsetZ;

/// <summary>
/// Create the "smooth" effect when the camera follows the bike.
/// </summary>
camRight	= Mathf.Lerp( camTransform.position.x, camPosX, Time.deltaTime * camMov.rightDamping );
camLeft		= Mathf.Lerp( camTransform.position.x, camPosX, Time.deltaTime * camMov.leftDamping );
camUp		= Mathf.Lerp( camTransform.position.y, camPosY, Time.deltaTime * camMov.upDamping );
camDown		= Mathf.Lerp( camTransform.position.y, camPosY, Time.deltaTime * camMov.downDamping );
camGround	= Mathf.Lerp( camTransform.position.y, camPosY, Time.deltaTime * camMov.groundDamping );

/// <summary>
/// Now let's zoom in and out
/// </summary>
maxCamZoomZ		= Mathf.Lerp( camTransform.position.z, target.position.z + camMov.maxCamPosZ,
Time.deltaTime * ( camMov.zoomDampingUp * ( relativeBikePos * 0.02f ) ) );

minCamZoomZ		= Mathf.Lerp( camTransform.position.z, camPosZ,
Time.deltaTime * camMov.zoomDampingDown );

speedMaxZoom	= Mathf.Lerp( camTransform.position.z, target.position.z + camMov.maxCamPosZ,
Time.deltaTime * ( camMov.zoomDampingGround * 0.02f ) );

camZoomZ		= Mathf.Lerp( camTransform.position.z, camPosZ,
Time.deltaTime * camMov.zoomDampingGround );

// Check if a crash was detected, and don't continue camera movements if it was
if( GameStates.crashing )
{
return;
}
else
{
// We now create a new Vector3 for the camera using the variables above...
if( CheckWheelCollision.hasFrontWC || CheckWheelCollision.hasRearWC )
{
if( bikeVelocity.x > orthoCam.velocityTresholdX )
camZoom = speedMaxZoom;
else
camZoom = camZoomZ;
}
else if( !CheckWheelCollision.hasFrontWC && !CheckWheelCollision.hasRearWC )
{
if( Mathf.Sign( bikeVelocity.y ) > 0 )
camZoom = maxCamZoomZ;
else
camZoom = minCamZoomZ;
}

// We now create a new Vector3 position for the 3D cam...
camPosXYZ.z = camZoom;

// Now let's define which dampening settings to use depending on the bike's movement
if( target.position.x >= camRight - camMov.camOffsetX )
{
camPosXYZ.x = camRight;
}
else
{
camPosXYZ.x = camLeft;
}

// Which cam settings to use for vertical movements
camPosXYZ.y = camGround;
if( !goingUpHill || !Managers.PlayerCollision.cameraLockArea )
{
if( !CheckWheelCollision.hasFrontWC && !CheckWheelCollision.hasRearWC )
{
if( target.position.y > camUp - camMov.camOffsetY )
{
camPosXYZ.y = camUp;
}
else
{
camPosXYZ.y = camDown;
}
}
}
if( goingUpHill || Managers.PlayerCollision.cameraLockArea )
{
camPosXYZ.y = camGround;
}
}

camTransform.position = camPosXYZ;
camTransform.LookAt( new Vector3( camPosX, camPosY + 2, target.position.z));

// ***************** THIS SECTION IS FOR ORBITING THE CAMERA ***************** //

// Calculate the current rotation angles
float wantedRotationAngle = target.eulerAngles.y;
float currentRotationAngle = camTransform.eulerAngles.y;

// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle( currentRotationAngle, wantedRotationAngle, 2 * Time.deltaTime );

// Convert the angle into a rotation
Quaternion currentRotation = Quaternion.Euler( 0, currentRotationAngle, 0 );

// Set the position of the camera on the x-z plane to distance meters behind the target
/// That's the part I can't hook into my final smooth camera code, since I can only make it work when using 'target.position',
/// but I really need to use the smooth camera position 'camPosXYZ' instead...
Vector3 camPos = target.position - ( currentRotation * Vector3.forward * 21 );

// ***************** END OF SECTION FOR ORBITING THE CAMERA ***************** //
}
``````

If you guys know another way to make my custom smooth camera movements orbit around a moving target, please do share hahaha!

Thanks for your time!
Stephane

Here you go:

Smooth Move / RotateAround