Smooth Transition 3rd person cam rotating around playe

Im currently having trouble in getting 3rd Person camera to work the way I want it to.

The problems which Im facing:

  1. Smooth transition of camera moving from point A to point B upon user interaction with the mouse click

  2. Camera rotating around the player. In other words, the pivot point is based on the player, and not the camera’s pivot. Actually, what I wanted to do is just an Over-The-Shoulder camera effect (when the player hold onto the right-click mouse button, it will switch from the default ThirdPersonCam to Over-The-Shoulder cam)

Before going further, I would like to highlight that:

  1. For the smooth transition, it seems that the function for moving the camera smoothly with the Lerp seems to be ran. However, that Lerping has been overwrite by the LateUpdate function in the ThirdPersonView’s script(Which I’ll be showing in a moment).

  2. Camera actually does rotates around the player. However, this can only work when the camera is DIRECTLY behind the player. It does not rotate around the player if the camera has been altered to move a certain distance to the right of the position.x. After trying with the changing of the position.x value, the whole camera just does not rotate around the player.

Now, there are two scripts in which the first was provided by the kind Unity’s community and which has been modified by me. The other script was created for the switching of camera.

ThirdPersonCam script located in a gameobject called “ThirdPersonCam”:

// the target for the camera to orbit around
var target : Transform;

// initial distance between the camera and the target
var distance = 5.0;
// min and max distance for zooming
var minDistance = 3.0;
var maxDistance = 10.0;

// an offset to stop the camera clipping walls
var offsetFromWall : float = 0.28;

// the speed at which the scrollwheel zooms in/out
var zoomRate = 20;

// some internal variables to calculate distances
private var currentDistance : float;
private var desiredDistance : float;
private var correctedDistance : float;

// speed of camera rotation around the object in both axes and smoothing for rotation
var xSpeed : float = 200.0;
var ySpeed : float = 200.0;
var rotationDampening : float = 3.0;

// the min and max rotation limits of the camera in the y-axis (up and over the object) and smoothing for zooming
var yMinLimit : int = 0;
var yMaxLimit : int = 80;
var zoomDampening : float = 5.0;

// variables to store the rotation of the camera
private var x  : float = 0.0;
private var y : float = 0.0;

// variables for zooming axis limit
public var yMinLimitZoomed: float = 0.00;
public var yMaxLimitZoomed: float = 0.00;

// adds a menu option to attach this script via the Component Menu
@script AddComponentMenu("Camera-Control/Mouse Orbit")


function Start () {
   // store the initial rotation of the camera on the x and y axes
    var angles = transform.eulerAngles;
    x = angles.y;
    y = angles.x;

   // set zoom distances to the value of the initial distance
   currentDistance = distance; //Original distance uses the variable called "distance"
   desiredDistance = distance;
   correctedDistance = distance;
   
   // Make the rigid body (if there is one) not change rotation
      if (rigidbody) {
      rigidbody.freezeRotation = true;
   }
}

// LateUpdate is called every frame after all other updates have happened
//(preferable for camera follows since all objects should have already moved)
function LateUpdate () {
   
   // if a target has been assigned (drag and drop one in the editor)
   if (target) {
      // if there is input on the vertical or horizontal axes (default is arrow keys and wasd)
      if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal")) {
         // store the current y-axis rotation of the target and the camera
         var targetRotationAngle = target.eulerAngles.y;
         var currentRotationAngle = transform.eulerAngles.y;
         // interpolate between the current y-axis rotation and the target y-axis rotation over time
         // setting the changing value to the x-axis rotation so the camera rotates around behind the PC
         x = Mathf.LerpAngle(currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);
         
		 ////////////////////////
		 //EDITTED VERSION//
		 ////////////////////////
		 x += Input.GetAxis("Mouse X") * xSpeed * 0.001;//EDITED VERSION,. DO REMOVE IF NOT WORKING
         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.01;//EDITED VERSION,. DO REMOVE IF NOT WORKING
		 
      // if there is no input from the vertical/horizontal axes (we are not moving)
      } else {
         // allow the player to rotate the camera using the mouse by updating the camera's
         // x/y rotation values based on the mouse position
         x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
      }
      
         
      // set the y-axis rotation value based on the result of the ClampAngle function
      y = ClampAngle(y, yMinLimit, yMaxLimit);
      
      // store the camera's rotation
       var rotation = Quaternion.Euler(y, x, 0);
      
      
      // convert input from the scrollwheel into zooming between the min/max distance ranges
      //desiredDistance -= Input.GetAxis ("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs (desiredDistance); //Disabled zooming feature, so that the mouse scroll is used for striking matches
	  desiredDistance = Mathf.Clamp (desiredDistance, minDistance, maxDistance);
      correctedDistance = desiredDistance;
      
      // store the camer's position
      var position = target.position - (rotation * Vector3.forward * desiredDistance);
      
      // a variable to hold a reference to raycast collision objects
      var hit : RaycastHit;
      // a variable to hold a flag so we know if we have already corrected the camera's position
      var isCorrected : boolean = false;
      // if a ray between the target and the desired position hits something then their must be something blocking the line of sight
        if (Physics.Linecast (target.position, position, hit)) {
            // shorten the distance between the camera and the target using the distance to the wall
            // minus an offset to prevent clipping
            correctedDistance = Vector3.Distance (target.position, hit.point) - offsetFromWall;
            // we have corrected the camera's position
            isCorrected = true;
      }

      // if we haven't corrected the camera's position or the new distance is greater than the current distance
      if(!isCorrected || correctedDistance > currentDistance){
      // interpolate the distance of the camera from the target between the current distance and the corrected distances
      // (ie. in front of the wall that is blocking our view)
            
         currentDistance = Mathf.Lerp (currentDistance, correctedDistance, Time.deltaTime * zoomDampening);
         
      }
      // set our current distance to this new distance
      currentDistance = correctedDistance;
         
      // clamp this value between the minimum and maximum distance settings
      currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
	  
	  // store the new position
	  position = target.position - (rotation * Vector3.forward * currentDistance);
	  
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//FOR UPDATING THE CAMERA MOVEMENT. THE MAIN CODING THAT AFFECTS THE LOCKING OF THE POSITION AND THE ROTATION OF THE GAMEOBJECT//
		/////////////////////////////////////////////////This is just purely for switching between views, FPS and TPV modes//////////////////////////////////////////////////
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		var locateThirdPersonCam = gameObject.Find("ThirdPersonCam");
		
		//If right click is not down,  the camera is in TPV mode
		if (!Input.GetKey(KeyCode.Mouse1))
		{			
			// set the current rotation and position of the camera to the new values to make it move
			transform.rotation = rotation;
			transform.position = position; //NOTICE, THIS IS THE CODE THAT IS OVERWRITING THE LERP ANIMATION~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		}
		
		//If right click is down, the camera is in FPS mode
		if (Input.GetKey(KeyCode.Mouse1))
		{	
			//transform.rotation = rotation;
			//transform.position = position;
			y = ClampAngle(y, yMinLimitZoomed, yMaxLimitZoomed);
			rotation = Quaternion.Euler(y, x, 0);
			transform.rotation = rotation;
		}
   }
}   

// keeps the value of the y-axis rotation within 360 degrees (it would cause an error otherwise)
// and within the minimum and maximum limits previously defined
static function ClampAngle (angle : float, min : float, max : float) {
   if (angle < -360)
      angle += 360;
   if (angle > 360)
      angle -= 360;
   return Mathf.Clamp (angle, min, max);
}

switchCamView script located in a gameobject called “ThirdPersonCam”:

public var transSpeed: int = 0;
public var switchShootView = false;
public var targetFPSDestination: Transform;
public var targetTPVDestination: Transform;
public var speedAdjuster: int = 0;

private var retrievePos = false;

//////////////////////
///Position Updater///
/////////////////////
private var oldPosX: float = 0.00;
private var oldPosY: float = 0.00;
private var oldPosZ: float = 0.00;

private var newPosX: float = 0.00;
private var newPosY: float = 0.00;
private var newPosZ: float = 0.00;

private var currentPosX: float = 0.00;
private var currentPosY: float = 0.00;
private var currentPosZ: float = 0.00;

function Awake()
{
	positionOld = transform.position;
}

function Update()
{
	switchCamera();
	
	if (Input.GetKey(KeyCode.Mouse1))
	{
		switchShootView = true;
	}
	
	else if (!Input.GetKey(KeyCode.Mouse1))
	{
		switchShootView = false;
	}
}

function switchCamera()
{			
	var locateThirdPersonCam = GameObject.Find("ThirdPersonCam");
	
	//goes into FPS mode
	if(switchShootView)
	{
		retrievePos = true;
		
		//Retrieve position
		if(retrievePos)
		{
			oldPosX = transform.position.x;
			oldPosY = transform.position.y;
			oldPosZ = transform.position.z;
			
			newPosX = targetFPSDestination.position.x;
			newPosY = targetFPSDestination.position.y;
			newPosZ = targetFPSDestination.position.z;
			
			retrievePos = false;
		}
		
		transform.position.x = Mathf.Lerp(oldPosX, newPosX, Time.deltaTime * speedAdjuster); 
		transform.position.y = Mathf.Lerp(oldPosY, newPosY, Time.deltaTime * speedAdjuster); 
		transform.position.z = Mathf.Lerp(oldPosZ, newPosZ, Time.deltaTime * speedAdjuster); 		
	}
	
	//goes into normal TPV mode
	if (!switchShootView)
	{
		hasClicked = false;
		retrievePos = true;
		
		//Retrieve position
		if(retrievePos)
		{
			oldPosX = transform.position.x;
			oldPosY = transform.position.y;
			oldPosZ = transform.position.z;
			
			newPosX = targetTPVDestination.position.x;
			newPosY = targetTPVDestination.position.y;
			newPosZ = targetTPVDestination.position.z;
			
			retrievePos = false;
		}
		
		transform.position.x = Mathf.Lerp(oldPosX, newPosX, Time.deltaTime * speedAdjuster); 
		transform.position.y = Mathf.Lerp(oldPosY, newPosY, Time.deltaTime * speedAdjuster); 
		transform.position.z = Mathf.Lerp(oldPosZ, newPosZ, Time.deltaTime * speedAdjuster); 
	}
}

Thanks in advance guys!

Look is this help you:
http://forum.unity3d.com/viewtopic.php?p=312195#312195

@sandolkakos: That’s just the script for making a third person camera.

As Ive stated in my previous post, I have a third person camera script that is working (was provided by the Unity forum too). Ive tweaked it to work in the way I want them too. However, the result is not quite good.

In any case, thanks for trying to help sandolkakos.

Once again, just to highlight the problems again, I’ll quote a few lines from my previous post.

Sorry for the long codes posted. Ive been trying to figure out the camera scripts for almost a week now, but to no avail…

Thanks in advance Guys!!

Bumped…sorry for bumping but…Im really getting more and more desparate…Im going insane soon…its really hard to solve :frowning:

Before rotating, update the camera position to the player position, rotate it, then just back it up along that same angle the same distance it was before. Then render. I’m more of a C# guy and not a JS, but if you need an example I can try.