MouseOrbitImproved resets a variable can't figure out why

I’m using the c# MouseOrbitImproved script. I’ve actually got this to work by just commenting out the raycaster bit.

void LateUpdate () {
    if (target) {
        x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
        y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
 
        y = ClampAngle(y, yMinLimit, yMaxLimit);
 		Debug.Log (y);

        Quaternion rotation = Quaternion.Euler(y, x, 0);
 		distance -= Input.GetAxis("Mouse ScrollWheel")*1.5f;
		Debug.Log ("distance = " + distance);
		distance = Mathf.Clamp(distance, distanceMin, distanceMax);
//        distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*1.5f, distanceMin, distanceMax);
			

        RaycastHit hit;
        if (Physics.Linecast (target.position, transform.position, out hit)) {
                distance -=  hit.distance;
        }
        Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
        Vector3 position = rotation * negDistance + target.position;
 
        transform.rotation = rotation;
        transform.position = position;
 

RaycastHit hit;
        if (Physics.Linecast (target.position, transform.position, out hit)) {
                distance -=  hit.distance;
        }
        Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
        Vector3 position = rotation * negDistance + target.position;
 
        transform.rotation = rotation;
        transform.position = position;
    }

1298935--60195--$distance.jpg

I feel like I walked in half way though a conversation, remember to explain what your issue is when you post.

However, you’ve a lot of redefined variables in that script, that shouldn’t even compile and let you run the game so the runtime console output confuses me even more.

I know, sorry about that, i wanted to delete the post, but since it’s a thread i couldn’t do it. I completely missed the raycaster thing, and while i don’t quite figure out how it works yet, i managed to use it to do exactly what i wanted. It’s not a game yet, i’m still a complete beginner :frowning:

Here’s what i made.

Changing this

 distance -=  hit.distance;

to this

distance = hit.distance;

Did it for my little scene.