- Home /
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
Answer by Noah-1 · May 13, 2012 at 03:49 AM
Here you go:
also : http://answers.unity3d.com/questions/246709/rotate-around-the-center-of-the-world-with-mouse.html
demo (hold right mouse button and move mouse, also use scroll wheel) : www.alucardj.net16.net/unityanswers/RotateCamAround.html
=]
Hey guys thanks for the links. I already looked at both these scripts, but they're both for mouse inputs and I can't figure out how to convert them to work with the position and rotation of my target object (no mouse input at all). The position I can probably put it together myself, but the rotation and distance is harder, and I'm having a hard time.
Taking my script above, what would make work the same way?
Thanks again!
Would my script here help - if it's kind of what you need then I could talk you through it. It's got mouse input to change the angle, but it then auto-centres around the desired position and moves around to avoid obstacles obscuring the view. http://whydoidoit.com/2012/03/22/unity-terrain-avoiding-smart-follow-camera/
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Ideas for getting SmoothFollow camera to follow right behind? 0 Answers
Smoothfollow when the object changed 1 Answer
TPS Camera scipt.(C#) 0 Answers
Need help with script, one time touch to the screen = synchronize camera with face 0 Answers