[Unity] Realize camera angle follow

Realize that the camera always follows the player in the U3D project, and the player model is kept in the middle of the lens

private Transform player;
//玩家和相机的差
private Vector3 offset; 
//相机移动速度
private float speed = 3;

// Start is called before the first frame update
void Start() {
    
    
    player = GameObject.FindGameObjectWithTag("Player").transform;
    offset = transform.position - player.position;
}

// 更新相机位置
void LateUpdate() {
    
    
    //世界坐标转化为局部坐标
    Vector3 targetPosition = player.position + player.TransformDirection(offset);
    //移动相机
    transform.position = Vector3.Lerp(transform.position, targetPosition, speed * Time.deltaTime);
    //相机看向玩家的方向
    transform.LookAt(player.position);
}

Guess you like

Origin blog.csdn.net/ainklg/article/details/129797679