MouseOrbit Return behind the character

Hey guys,
I’m trying to make a MouseOrbit, but I get hard time with something.
When I press on the left mouse button, I can rotate the camera and when I release it, the camera come back smoothly behind the character. However, if I click a second time on the mouse left button, the camera move toward to the last camera position at the time that I released the button. Basically, the camera doesn’t stay behind the character when I press on the mouse.
Honestly, I’m working hard to find where’s the issue, I tried some things, but I can figure out how to fix it.

void Update () {

        OriginRot = Target.rotation;
        OriginRot *= Quaternion.Euler (25f, 0f, 0f);

        Origin = Target.TransformPoint (new Vector3 (0f, 8f, -15f));


       
        if (Input.GetMouseButton (0)) {
           

            x += Input.GetAxis("Mouse X") * 20;
            y += Input.GetAxis("Mouse Y") * 20;

            rotation = Quaternion.Euler(y , x , 0);

            negDistance = new Vector3(0f, 0f, -15f);
            position = rotation * negDistance + Target.position;
           
            transform.rotation = rotation;
            transform.position = position;


        }
        else
        {
            transform.position =  Vector3.Lerp (transform.position, Origin, Time.deltaTime * 5);
           
            transform.rotation = Quaternion.Slerp (transform.rotation, OriginRot, Time.deltaTime * 5);


        }
       
    }
}

Basically, I think I need to always keep the camera position in a variable, but I don’t know which variable I need to give this position.

Any clue ?

Up

Up

Comment out the ‘else’ section of the script and see what happens.

Basically, the “else” section is to smoothly rotate the camera to the origin position and it works, but when I press on the mouse the camera move toward to the last position and doesn’t stay to the origin.

After few hours/days, I think the issue is the x,y save the last position, but I don’t know how to fix it.

The main issue here is to rotate the camera behind the character (the origin position) after rotated the camera around this character.
I’m trying to find a way to do that with this code. Basically the “else” is only the half of the solution that I found

if (Input.GetMouseButton (0)) {

            x += Input.GetAxis("Mouse X") * 20;
            y += Input.GetAxis("Mouse Y") * 20;

            rotation = Quaternion.Euler(y , x , 0);

            negDistance = new Vector3(0f, 0f, -15f);
            position = rotation * negDistance + Target.position;
         
            transform.rotation = rotation;
            transform.position = position;


        }

I’ve worked with MouseOrbit. I came across issues when Saving/Loading from XML, I would set the position and rotation of the camera but it would still be incorrect. I realized it was the x and y variables from that script. Simply set the x and y variables to be the desired position. (The x and y are what determines the rotation when the script is behaving normally) So if you’ll need to set x and y at the end of your Lerp. (This may prove complicated if you want it to be dynamic–you will have to calculate x and y based off of the position of camera versus the origin. Reverse engineering is required)

Hi, I tried that, but whatever the value I set for x and y, the camera still going on the left.

X and Y should be behind the character ( Target.transformpoint), but I don’t know how to do that.

I just tried something else, but I really can’t fix it.

Basically, I want X and Y have the same value that transform.position before pressing button.

Can I use Target.transformpoint like Target.transform.position.x, can I give the x position based on the character position.

Up