Camera smooth follow X & Y

So I’m using this as a way to get my camera to follow my player;

using UnityEngine;
using System.Collections;
public class cameracontrol : MonoBehaviour
{
	public GameObject cameraTarget; // object to look at or follow
	public GameObject player; // player object for moving
	public float smoothTime = 0.1f; // time for dampen
	public bool cameraFollowX = true; // camera follows on horizontal
	public bool cameraFollowY = true; // camera follows on vertical
	public Vector2 velocity; // speed of camera movement
	private Transform thisTransform; // camera Transform
	// Use this for initialization
	void Start()
	{
		thisTransform = transform;
	}
	// Update is called once per frame
	void Update()
	{
		if (cameraFollowX)
		{
			thisTransform.position = new Vector3(Mathf.SmoothDamp(thisTransform.position.x, cameraTarget.transform.position.x, ref velocity.x, smoothTime), thisTransform.position.y, thisTransform.position.z);
		}
		if (cameraFollowY)
		{
			thisTransform.position = new Vector3(Mathf.SmoothDamp(thisTransform.position.y, cameraTarget.transform.position.y, ref velocity.x, smoothTime), thisTransform.position.x, thisTransform.position.z);
		}
	}
}

Except it’s not very smooth, it is actually shaking very much like an over done earthquake camera shake.

Also the camera does not seem to follow the player/object correctly the camera will be centered on the player then the player will start to wander over to the right or left, it’s odd.

1 Like

Hi, One way is to put Your camera object as a child Your player object. Then You will see that camera follow Your player.

But if You want implement rather something like follow with smooth delay You could script this. I do not know Mathf.SmoothDamp method, but I used Mathf.Lerp to smooth animation.

Something like this:

using UnityEngine;
using System.Collections;

public class CameraFollowObjectSmoothScript : MonoBehaviour {

    public GameObject player;
    private float helpCameraFollowFaster = 3;
   
    void FixedUpdate () {
        this.transform.position = new Vector3 (Mathf.Lerp(this.transform.position.x, player.transform.position.x, Time.deltaTime*helpCameraFollowFaster),
                                               Mathf.Lerp(this.transform.position.y, player.transform.position.y, Time.deltaTime*helpCameraFollowFaster),0);
    }
}

But in this case, player could go outside camera.

You could also using any kind of animation tween. Look at this. I wrote about this on my blog.

Placing the camera as the child does work, but because of how my character is controlled it causes some un-intentional results.

I’m not to familiar with Marthf.Lerp but I’ll see if I can adapt your script for my situation.

I was thinking that maybe I can make the player the child of the camera and setup a portion of my movement controls on the camera. I’ll have to see.

EDIT/ The later worked, thanks for your help codeedward.

Found out that if my player gets stuck on another object the camera keeps moving…

I almost had it! :stuck_out_tongue:

Vector3 expects Vector3(float x, float y, float z) or Vector3(float x, float y) in which z is defaulted to zero.
You can’t flip the order around.

In the script above in “if (cameraFollowY)” you are changing the new x position to a what you are calculating from the y, and then setting the new y position to the present target x position.

If you want to alter y in a Vector3 constructor you have to do it in the second place.
IE: Vector3( old.x, iAmDoingSomethingHere(new.y), old.z);

You would also want to throw this in LateUpdate, or leave it in Update and adjust the scripting order to make sure the camera move is always called after whatever is moving your tracked object.

I am not sure what you are doing for your movement, but there should be a way to just attach a camera if you want it to always be centered on the character(?).

Yes, making the Camera the child of my player does work…

But as for player control, the player is always facing the mouse cursor. As such for some strange reason when I make the camera a child of the player the world also rotates which is an un-wanted result.

You could block camera rotation for example. Here I found some topic about how to lock camera.

 function Update(){
     transform.localRotation.y=0; //or any initial value that you want
}