【Unity2D】更好的相机跟随目标实现

实现效果:

1、相机跟随目标角色。

2、目标角色周围存在一个目标区域,角色在目标区域中移动相机不移动,角色离开目标区域边界时带动目标区域和相机一起移动。

3、相机跟随时平滑过渡。

将以下代码复制并挂载到相机上即可使用:

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

/// <summary>
/// 相机跟随
/// </summary>
public class CameraFollow : MonoBehaviour
{
    public void Start()
    {
        _focusArea = new FocusArea(_target.bounds, _focusSize);
        _targetPos = transform.position;
    }

    public void Update()
    {
        if (_target == null) return;
        _focusArea.Update(_target.bounds);
        if(_focusArea.velocity != Vector2.zero)
        {
            _targetPos += (Vector3)_focusArea.velocity;
        }
        transform.position = Vector3.Lerp(transform.position, _targetPos, _smoothSpeed * Time.deltaTime);
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = new Color(0.5f, 1, 0.5f, 0.5f);
        Gizmos.DrawCube(_focusArea.center, _focusSize);
    }

    public Collider2D _target;
    public Vector2 _focusSize;
    public float _smoothSpeed = 20;
    
    FocusArea _focusArea;
    Vector3 _targetPos;

}

public class FocusArea
{
    public FocusArea(Bounds focusBounds, Vector2 size)
    {
        left = focusBounds.center.x - size.x / 2;
        right = focusBounds.center.x + size.x / 2;
        top = focusBounds.min.y + size.y;
        bottom = focusBounds.min.y;

        center = new Vector2((left + right) / 2, (top + bottom) / 2);
        velocity = Vector2.zero;
    }

    public void Update(Bounds targetBounds)
    {
        float shiftX = 0;
        if (targetBounds.min.x < left)
        {
            shiftX = targetBounds.min.x - left;
        }
        else if (targetBounds.max.x > right)
        {
            shiftX = targetBounds.max.x - right;
        }
        left += shiftX;
        right += shiftX;

        float shiftY = 0;

        if (targetBounds.max.y > top)
        {
            shiftY = targetBounds.max.y - top;
        }
        else if (targetBounds.min.y < bottom)
        {
            shiftY = targetBounds.min.y - bottom;
        }
        top += shiftY;
        bottom += shiftY;
        center = new Vector2((left + right) / 2, (top + bottom) / 2);
        velocity = new Vector2(shiftX, shiftY);

    }

    public float left;
    public float right;
    public float top;
    public float bottom;
    public Vector2 center;
    public Vector2 velocity;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity实现鱼的集群跟随,你可以按照以下步骤编写代码: 1. 创建一个鱼的预制体(Prefab)包含图像和脚本组件。 2. 创建一个空的游戏对象,作为鱼群的父对象。 3. 在鱼的脚本中,添加以下代码: ```csharp using UnityEngine; public class Fish : MonoBehaviour { public Transform target; // 目标点 public float speed = 2f; // 移动速度 private void Update() { if (target != null) { Vector3 dir = target.position - transform.position; transform.Translate(dir.normalized * speed * Time.deltaTime); } } } ``` 4. 在场景中实例化一定数量的鱼预制体,并将它们添加到鱼群的父对象下。 5. 在鱼群的父对象上创建一个空的游戏对象,并将它命名为"Target",作为鱼群的中心点。 6. 在鱼群的父对象上创建一个脚本,用于控制目标点的移动。 ```csharp using UnityEngine; public class FishTarget : MonoBehaviour { public float moveSpeed = 1f; // 目标点移动速度 private void Update() { float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); Vector3 moveDirection = new Vector3(horizontal, vertical, 0f); transform.Translate(moveDirection * moveSpeed * Time.deltaTime); } } ``` 7. 将鱼群的父对象(包含鱼预制体和目标点)拖拽到场景中的Fish脚本中的target变量中。 8. 运行游戏,观察鱼的集群跟随效果。 这段代码实现了鱼根据目标点的位置进行跟随移动。你可以根据自己的需求调整速度、控制方式等。希望对你有所帮助!如果有更多问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值