- Home /
Third Person Camera Rotation Offset Trouble
Hey guys;
I've a very basic TPS camera (its a free-floating camera that tracks the character's position and is controlled by mouse movement, standard TPS camera for games like MGSV), have integrated a lot of stuff into it, one part that keeps eluding me is how to make the character stay in the left side of the center of the camera at all times. The offset value that i input works great until I turn the camera 180 degrees, then the character is placed on the right side of the camera. I've read on some forums here that this is because of localRotation, but just couldn't wrap my head around how I'd make it work... Here's the code that does the rotation;
function LateUpdate () {
if (Time.timeScale == 1){
if (Input.GetButtonDown ("ChangeCam")){
if (camMode == 'close'){camFar();
}else{
camClose();
}}
var desiredAngle = target.transform.eulerAngles.y;
x += Input.GetAxis("Mouse X") * xSpeed * 0.02 ;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
targetOffset = Vector3(1,1.2,0);
var rotation = Quaternion.Euler(y, x, 0);
var targetPos = target.position + targetOffset;
var direction = rotation * -Vector3.forward;
var targetDistance = AdjustLineOfSight(targetPos, direction);
currentDistance = Mathf.SmoothDamp(currentDistance, targetDistance, distanceVelocity, closerSnapLag * .3);
transform.rotation = rotation ;
transform.localPosition = targetPos + direction * currentDistance;
}}
This part of the camera code is based on the platformer demo that came with Unity 2.x installations. I realize that this question was asked quite a few times, but none of the solutions worked for me. Can you please advise on what to change or how to proceed?
Thanks in advance;
Jon