Camera always behind player
I'm struggling with this for quit some time now. I have GameObject, being a sphere, which is my player on a 3d Terrain. I have a Camera which is always on a fixed distance from the player, follows it where it goes with below script:
public GameObject player;
private Vector3 offset;
// Use this for initialization
void Start () {
offset = transform.position - player.transform.position;
}
void LateUpdate () {
transform.position = player.transform.position + offset;
}
So far so good. However what I actually want is that the camera rotates with the player, so it always looks into the direction where the sphere is moving, but always stays behind the player at the same fixed distance, so that the player is always visible in the camera view.
There are a lot of scripts available, but the problem with the onces I've seen so far is that the camera indeed rotate with the player, but because the player actually is a rolling sphere the camera view is rolling and turning as well.
The best script I found so far is below, but this one has the same problem as the other onces, the camera rolls with the player.
public Transform target;
public float distance = 3.0f;
public float height = 3.0f;
public float damping = 5.0f;
public bool smoothRotation = true;
public bool followBehind = true;
public float rotationDamping = 10.0f;
void Update () {
Vector3 wantedPosition;
if(followBehind)
wantedPosition = target.TransformPoint(0, height, -distance);
else
wantedPosition = target.TransformPoint(0, height, distance);
transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * damping);
if (smoothRotation) {
Quaternion wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
//Quaternion ownRotation = Quaternion.RotateTowards;
transform.rotation = Quaternion.Slerp (transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
}
else transform.LookAt (target, target.up);
}
Can anyone help me with this please?
Did you figure this one out? I'm struggling with the same problem..
Hi, I didn't found a workable answer on the Unity forum, so I posted it on Stackoverflow ("https://stackoverflow.com/questions/43709392/camera-always-behind-player-in-unity3d"). Please have a look at this post, as it helped me in the end.
Hope this does solve you issue as it did $$anonymous$$e
Look its easy. You leave the camera alone as a separate GameObject. No parenting. A script on your camera will handle this. Have a field for a Target Object. Your camera simply moves towards the object if it is over a certain distance then stops when at the threshold. Figure out via transform which direction the player is facing and add a slight rotation to the camera. I imagine what you are looking for is very similar to what's in Roll-A-Ball tutorial.
Your answer
Follow this Question
Related Questions
Recognize when ever camera looks up and turns back down 0 Answers
why the camera rotate in the z axis 0 Answers
Rotating Camera on click and drag 0 Answers
Third person controller camera 1 Answer