How do I make my camera follow the player without rotating in 2D?

Here’s what I got so far:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour {

	// Use this for initialization
	void Start () {

		// camera = GetComponent<Transform> ();
		player = GameObject.Find ("Player");

	}

	// Update is called once per frame
	void Update () {
		
		GetComponent<Transform>.postion.x = player.postion.x;
		GetComponent<Transform>.postion.y = player.postion.y;
		GetComponent<Transform>.postion.z = player.postion.z - 10;

	}
}

so, you want your camera to follow behind your character, but without turning when you turn? Like a 3rd person adventure game view (where the camera is always facing the same direction, no matter how the player moves around)

Instead of a script to move your camera, just put your camera as a child object of the player object. It will follow at the distance you set it to in the editor, and stay there. You can keep the camera always looking at the player object with a script if you need to, but if I recall correctly, you won’t need that.

You shouldn’t use GetComponent in a method which runs every frame. It is slow and can hit performance when called every frame. You were correct to get a reference to the camera transform in the Start method so I am not sure why you commented that out.

I also personally do not like string lookups such as GameObject.Find(“Player”); The issue you can have is they will never give you any compiler errors and much be 100% correct in case and spelling. A prefered method to find the player would be using FindObjectOfType().

That being said, here is a simple camera follow script I made some time ago which may be of help to you;

using UnityEngine;

public class SimpleCameraFollow : MonoBehaviour
{
    public enum FollowType
    {
        Rigid,
        Lerp,
        Slerp,
    }

    //Populate in Inspector with you players transform
    public Transform    TargetToFollow;

    //The distance the camera will be from the player
    public Vector3      FollowOffset        = new Vector3(0, 0, -10);

    //How quickly the camera moves
    public float        FollowSpeed         = 5f;

    //How the camera will follow
    public FollowType   FollowMethod        = FollowType.Lerp;

    private Transform   _cameraTransform;
    private Vector3     _targetPos;

    private void Start()
    {
        if(TargetToFollow == null)
            Debug.LogError($"{nameof(TargetToFollow)} is null", this);

        _cameraTransform = transform;
    }

    private void LateUpdate()
    {
        switch (FollowMethod)
        {
            case FollowType.Rigid:
                _targetPos = TargetToFollow.position + FollowOffset;
                break;
            case FollowType.Lerp:
                _targetPos = Vector3.Lerp(_cameraTransform.position , TargetToFollow.position + FollowOffset, Time.deltaTime * FollowSpeed);
                break;
            case FollowType.Slerp:
                _targetPos = Vector3.Slerp(_cameraTransform.position, TargetToFollow.position + FollowOffset, Time.deltaTime * FollowSpeed);
                break;
        }

        _cameraTransform.position = _targetPos;
    }
}