- Home /
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.
After the transform.LookAt line, insert this line:
transform.position = transform.position - target.transform.position * 10f;
Are you sure that line will work as is? Unless I'm missing something, you would have to add parentheses around the subtraction or else the order of operations will cause the multiplication to be evaluated first. Also, without normalizing the vector subtraction, it doesn't look like it will maintain a constant distance from the target.
Correct me if I'm wrong, but I believe the following changes are necessary:
transform.position = (transform.position - target.transform.position).normalized * 10f;
Yes, you are right. I forgot about the order.
Answer by Graphics_Dev · Mar 16, 2016 at 12:18 PM
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));
}
}
What do you mean by drag the camera to its transform property? I tried dragging it into the transform component in the inspector but none of the fields would accept it.
When you put the script on the non-parented camera, there will be a transform slot on the script ;)
Answer by ComradeVanti · Mar 16, 2016 at 10:12 AM
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
Your answer
Follow this Question
Related Questions
Multiple clip exceptions for a camera. 1 Answer
Rotating Camera around player object 0 Answers
How to pan camera based on mouse position 0 Answers
Multiple Cars not working 1 Answer
3rd Person Camera & Controller C# 1 Answer