Unity 摄像机跟随人物、人物走到地图边缘摄像机停止平移、手指长按屏幕摄像机平移

1. 摄像机跟随人物

首先,你需要一个脚本来控制摄像机跟随人物。这个脚本应该附加到你的摄像机对象上。

CameraFollow.cs

using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    public Transform target; // 指向你的人物对象
    public float smoothSpeed = 0.125f; // 摄像机平滑移动的速度
    public Vector3 offset; // 摄像机相对于人物的偏移量,比如(0, 10, -10)表示摄像机在人物的上方和后方

    void LateUpdate()
    {
        // 计算摄像机应该到达的位置
        Vector3 desiredPosition = target.position + offset;
        // 平滑地移动到那个位置
        Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
        transform.position = smoothedPosition;

        // 让摄像机始终看向人物
        transform.LookAt(target);
    }
}

在这个脚本中,target是你想要摄像机跟随的人物对象。offset是摄像机相对于人物的偏移量,你可以根据需要调整它。smoothSpeed控制摄像机移动时的平滑程度。

2. 人物走到地图边缘 摄像机停止平移

为了检测人物是否走到地图边缘,你可以在地图边缘放置一些碰撞体(如BoxCollider),并给它们一个共同的父对象,然后将这个父对象的Transform组件赋值给CameraFollow脚本中的mapBoundary变量。

CameraFollow.cs(扩展)

// ...之前的代码

public Transform mapBoundary; // 地图边界的父对象Transform

void LateUpdate()
{
    // ...之前的代码

    // 检查摄像机是否超出地图边界
    Vector3 desiredPosition = target.position + offset;
    if (desiredPosition.x < mapBoundary.position.x + mapBoundaryBoundaryOffset.x)
    {
        desiredPosition.x = mapBoundary.position.x + mapBoundaryBoundaryOffset.x;
        offset.x = desiredPosition.x - target.position.x;
    }
    else if (desiredPosition.x > mapBoundary.position.x + mapBoundary.localScale.x - mapBoundaryBoundaryOffset.x)
    {
        desiredPosition.x = mapBoundary.position.x + mapBoundary.localScale.x - mapBoundaryBoundaryOffset.x;
        offset.x = desiredPosition.x - target.position.x;
    }

    // 对y和z轴进行相同的处理
    // ...

    // 平滑地移动到那个位置(这部分代码与之前相同)
    Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
    transform.position = smoothedPosition;

    // 让摄像机始终看向人物(这部分代码与之前相同)
    transform.LookAt(target);
}

public Vector3 mapBoundaryBoundaryOffset = new Vector3(1, 1, 1); // 用于调整边界检测的灵敏度

在这个扩展中,我添加了mapBoundary变量来存储地图边界的父对象Transform。我还添加了mapBoundaryBoundaryOffset变量来允许你调整边界检测的灵敏度。现在,当人物走到地图边缘时,摄像机将停止平移。

3. 手指长按屏幕 摄像机平移

为了通过触摸控制摄像机平移,你需要另一个脚本。

CameraTouchControl.cs

using UnityEngine;

public class CameraTouchControl : MonoBehaviour
{
    private Vector3 touchOrigin = -Vector3.one; // 用于存储触摸开始的位置
    public float panSpeed = 0.5f; // 摄像机平移的速度

    void Update()
    {
        // 检查是否有触摸输入
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);

            // 当触摸开始时,记录触摸位置
            if (touch.phase == TouchPhase.Began)
            {
                touchOrigin = touch.position;
                return;
            }

            // 当触摸移动时,移动摄像机
            if (touch.phase == TouchPhase.Moved)
            {
                Vector3 touchEnd = touch.position;
                float x = touchEnd.x - touchOrigin.x;
                float y = touchEnd.y - touchOrigin.y;

                touchOrigin = touchEnd;

                // 根据触摸移动的方向和距离移动摄像机
                Vector3 move = new Vector3(x, 0, y) * panSpeed * Time.deltaTime;
                transform.Translate(-move, Space.World);
            }
        }
    }
}

在这个脚本中,panSpeed控制摄像机平移的速度。当用户在屏幕上触摸并移动时,摄像机将根据触摸移动的方向和距离进行平移。

现在,你应该有了实现所需功能的完整代码。将这些脚本附加到相应的对象上,并调整变量以适应你的游戏。

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Unity中实现摄像机跟随人物,可以使用以下两种方式: 1. 通过脚本控制摄像机位置和旋转 可以在摄像机上添加一个脚本,通过脚本代码实现摄像机跟随人物的效果。具体实现步骤如下: 1)获取人物的Transform组件,即人物的位置和旋转信息。 2)将摄像机的位置和旋转信息设置为人物的位置和旋转信息,可以通过Vector3.Lerp()方法实现平滑跟随。 3)在Update()方法中不断更新摄像机的位置和旋转信息,从而实现摄像机跟随效果。 示例代码: ``` public class CameraFollow : MonoBehaviour { public Transform target; // 人物的Transform组件 public float smoothTime = 0.3f; // 平滑跟随时间 private Vector3 velocity = Vector3.zero; void Update() { // 计算摄像机新的位置 Vector3 targetPosition = target.TransformPoint(new Vector3(0, 5, -10)); // 平滑跟随 transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime); // 计算摄像机新的旋转角度 transform.LookAt(target); } } ``` 2. 使用Unity跟随组件 Unity提供了一个跟随组件(Follow),可以通过简单的配置实现摄像机跟随人物的效果。具体实现步骤如下: 1)在摄像机上添加一个跟随组件(Component -> New Script -> Follow)。 2)将跟随组件的Target设置为人物的Transform组件。 3)调整跟随组件的其他参数,例如跟随速度、距离等。 示例代码: ``` using UnityEngine; using UnityStandardAssets.Utility; public class CameraFollow : MonoBehaviour { public Transform target; // 人物的Transform组件 public float distance = 10f; // 摄像机人物的距离 public float height = 5f; // 摄像机人物的高度 public float damping = 1f; // 跟随速度 private FollowTarget followTarget; void Start() { followTarget = GetComponent<FollowTarget>(); followTarget.target = target; followTarget.distance = distance; followTarget.height = height; followTarget.damping = damping; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯狂跳跳虎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值