Camera to follow player?

Hello i’m trying to make camera that follows player from behind cuz now is following but rotation is random can anyone help me with this?

using UnityEngine;
using System.Collections;

public class camLook : MonoBehaviour {

	public Transform target;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	  Vector3 pos = target.position;
	  transform.LookAt(target);
	  transform.position = new Vector3(pos.x +10.0f,pos.y+10.0f,pos.z +10.0f);
	}
}

all u need to do is child the camera to the player and position the camera behind the player.
hope this helps

Gamer’sHelmet is correct this will do as he says but I think the issue with your code is that you set the rotation before you set the position.

You’re basically aligning the camera rotation and then shifting the object’s position which means it’s not pointing at it’s target anymore.

Move the object… then set the rotation.

Hope this helps.

ill try ur way.
@BPPHarv can u tell me how to set the rotation of camera?

Try reversing the order of your code.

This:

transform.position = new Vector3(pos.x +10.0f,pos.y+10.0f,pos.z +10.0f);
transform.LookAt(target);

Instead of

transform.LookAt(target);
transform.position = new Vector3(pos.x +10.0f,pos.y+10.0f,pos.z +10.0f);

And how to LookAt from behind? any idea?

Add this

public Vector3 PositionOffset;

Change:

transform.position = new Vector3(pos.x +10.0f,pos.y+10.0f,pos.z +10.0f);

for:

transform.position = new Vector3(pos.x +PositionOffset.x,pos.y+PositionOffset.y,pos.z +PositionOffset.x);

Now in the component you will have a new option called Position Offset, you change the camera to whatever position you need with that