一、镜头跟随

镜头跟随可以使玩家有着良好的反馈,所以我们来实现一下镜头跟随(虽然直接把镜头放在玩家对象上面也可以实现~)

首先我们先创建一个GameObject装MainCamera

unity2d怎么给背景 unity加背景_游戏引擎

并创建脚本CameraFollow挂载到该对象

我们在LateUpdate生命周期中实现功能

由于LateUpdate在Update后面执行,同时,Player的移动是在Update中执行的,所以在LateUpdate中可以较好的处理相机跟随Player

public Transform player; // 相机绑定的对象
    void LateUpdate()
    {
    // =相机跟随玩家
    transform.position = new Vector3(player.position.x, player.position.y, player.position.z);
    }

二、背景滑动

背景的滑动也是可以给玩家好的视觉反馈的~所以我们来实现一下

背景可以分为近中远景~

我们这样子创建(我这里是只分了远景和中景~)

unity2d怎么给背景 unity加背景_图层_02

同时修改他们的显示图层,展示在摄像机中~

unity2d怎么给背景 unity加背景_unity2d怎么给背景_03

然后呢我们实现一下代码~

创建代表背景的变量,同时创建一个Vector2变量,用于记录每一帧相机的位置

public Transform farBack, midBack; // 远景,近景
    public Vector2 lastCamere; // 最后相机位置

接着,我们创建一个记录较之前相机移动的偏移变量lastFrame

拿到偏移量之后呢,我们添加到背景的位移中实现滑动,不同的背景,我们可以设置位移的速度

// 记录相机与上一帧移动的距离
        Vector2 lastFrame = new Vector2(transform.position.x - lastCamere.x, transform.position.y - lastCamere.y);
        // 远景移动 = 初始位置加上偏移位置
        farBack.position += new Vector3(lastFrame.x, lastFrame.y, 0);
        // 中景如上
        midBack.position += new Vector3(lastFrame.x * 0.5f,lastFrame.y * 0.5f, 0);

        // 重新获取位置
        lastCamere = transform.position;

完整代码

public Transform player; // 相机绑定的对象
    public Transform farBack, midBack; // 远景,近景
    public Vector2 lastCamere; // 最后相机位置
     Start is called before the first frame update
    void Start()
    {
        lastCamere = transform.position; // 先设置相机初始位置
    }

    // Update is called once per frame
    void Update()
    {
        // =相机跟随玩家
        transform.position = new Vector3(player.position.x, player.position.y, player.position.z);

        // 记录相机与上一帧移动的距离
        Vector2 lastFrame= new Vector2(transform.position.x - lastCamere.x, transform.position.y - lastCamere.y);
        // 远景移动 = 初始位置加上偏移位置
        farBack.position += new Vector3(lastFrame.x, lastFrame.y, 0);
        // 中景如上
        midBack.position += new Vector3(lastFrame.x * 0.5f,lastFrame.y * 0.5f, 0);

        // 重新获取位置
        lastCamere = transform.position;
    }

三、无限背景

如果背景不是跟摄像机同位移滑动的话,就会出现背景跟不上摄像机出现断层的情况,所以我们需要判断当前摄像机中心点相对于背景的中心点在左边还是右边,从而实现背景的平移

给需要平移的背景添加一个脚本

我这里是给sea添加

unity2d怎么给背景 unity加背景_unity2d怎么给背景_04

脚本代码

private GameObject mianCamera; // 获取相机
 private float mathWidth; // 地图宽度
 private SpriteRenderer spriteRenderer;
 // Start is called before the first frame update
 void Start()
 {
     mianCamera = GameObject.FindWithTag("MainCamera");
     spriteRenderer = GetComponent<SpriteRenderer>();
     mathWidth = spriteRenderer.sprite.bounds.size.x; // 获取地图宽度 
 }

 // Update is called once per frame
 void Update()
 {
     Vector3 tempTran = transform.position; // 获取当前位置
     // 如果 当前摄像机位置(中心点)大于背景中心点的话,就平移(代表来到了右边)
     if(mianCamera.transform.position.x >= transform.position.x + (mathWidth / 2))
     {
         // 添加偏移
         tempTran.x += mathWidth;
         // 开始偏移
         transform.position = tempTran;
     }
     // 表示来到了左边,开始偏移
     else if(mianCamera.transform.position.x < transform.position.x - (mathWidth / 2))
     {
         tempTran.x -= mathWidth;
         transform.position = tempTran;
     }
 }

四、效果展示

unity2d怎么给背景 unity加背景_unity2d怎么给背景_05