making a camera stay at the right distance (c#)

I have this script that lets me rotate a camera around a target using the mouse, but when the target moves (this is for a third person controller/camera) the minimum distance between the character and the camera get longer.

How do i stop the camera from going further away from the character? see the screenshots below for an example and the code.

the distance to begin with:

and after moving for a while:

the script:

using UnityEngine;
using System.Collections;

public class PlayerCameraScript : MonoBehaviour 
{
	public GameObject target;
	private float speedMod = 10.0f;
	private Vector3 point;
	
	void Start () 
	{

	}
	
	void Update () 
	{
		point = target.transform.position;

		float mouseV = Input.GetAxis ("Mouse X");
		float mouseH = Input.GetAxis ("Mouse Y");

		transform.RotateAround (point,new Vector3(mouseH,mouseV,0.0f),20 * Time.deltaTime * speedMod);
		transform.LookAt(point);
	}
}

to recap I want the camera to stay at the same distance from the character all the time and not zoom out like in the second screenshot.

the_megajoey

Just make the camera a child of a child of you player model:

Player → Somechild–> Camera

If you rotate Somechild the camera will rotate around your gameobject.

So Somechild would have the coords 0,0,0 relative to your player.
The Camera would have some coords depending on what distance you want.

Good luck

I guess this is something that you need.

http://wiki.unity3d.com/index.php?title=MouseOrbitImproved.

Here is a clean solution for camera follow. Add a camera as a child of the player located to have the exact view you want. Disable the camera component on this object. Add another camera to your scene and add this script. Drag the other camera to it’s transform property in the inspector. Hit play and watch the magic!

Use this code:

using UnityEngine;
using System.Collections;

public class SmoothCamFollow : MonoBehaviour 
{
	public Transform target;
	private new Transform camera;

	public float posSpeed = 1.0F;
	public float rotSpeed = 1.0F;

	// Use this for initialization
	void Start () 
	{
		camera = GetComponent<Transform> ();
	}

	// Update is called once per frame
	void Update () 
	{
		// position movement
		camera.position = Vector3.Lerp (camera.position, target.position, (posSpeed * Time.deltaTime));

		// rotation movement
		camera.rotation = Quaternion.Lerp (camera.rotation, target.rotation, (rotSpeed * Time.deltaTime));
	}
}