摄像机窗格(伪Cinemachine)

本文介绍了一个Unity脚本,用于实现相机跟随玩家角色,并在超出预设视野范围时进行边界检查,确保游戏画面始终覆盖玩家位置。脚本通过计算与玩家的距离,决定是否启动相机跟随,同时使用屏幕坐标系绘制边界线。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayCamera : MonoBehaviour {

    public Transform player;   //玩家
    Camera playerCamera;        //主相机
    Vector2 boxSize;            //视野范围
    public bool cameraMove;
    void Start() {
        boxSize = new Vector2(2, 2);
        playerCamera = GetComponent<Camera>();
        cameraMove = false;
    }

    // Update is called once per frame
    void Update() {

        if (cameraMove)
        {
            FollowPlayer();
        }
        else
        {
            CheckBoundary();
        }

    }

    public void FollowPlayer()
    {
        Vector3 targetPos = new Vector3(player.position.x, player.position.y, transform.position.z);
        transform.position = Vector3.Lerp(transform.position, targetPos, 0.08f);
        float distance = Vector3.Distance(targetPos,transform.position);
        if (distance<0.5f)
        {
            cameraMove = false;
        }
    }

    public void CheckBoundary()
    {
        float leftDistance = 0; //左右距离
        if (player.position.x < transform.position.x) //在左边
        {
            leftDistance = transform.position.x - player.position.x;

        }
        else
        {
            leftDistance = player.position.x - transform.position.x;
        }
        if (leftDistance > boxSize.x * 0.5f)
        {
            Debug.Log("超出视野范围X轴");
            cameraMove = true;
        }

        float uDDistance = 0;   //上下距离

        if (player.position.y < transform.position.y)
        {
            uDDistance = transform.position.y - player.position.y;
        }
        else
        {
            uDDistance = player.position.y - transform.position.y;
        }

        if (uDDistance > boxSize.y * 0.5f)
        {
            Debug.Log("超出视野范围Y轴");
            cameraMove = true;
        }
    }
    private void Awake()
    {
        if (!lineMaterial)
        {
            lineMaterial = new Material(Shader.Find("Particles/Alpha Blended"));
            lineMaterial.hideFlags = HideFlags.HideAndDontSave;
            lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
        }
    }
    private Vector3 pos(int i, int j)
    {
        Vector2 p = Vector2.zero;
        p = new Vector2((this.transform.position.x + i * boxSize.x / 2), (this.transform.position.y + j * boxSize.y / 2));
        p = Camera.main.WorldToScreenPoint(p);
        p.x = p.x / Screen.width;
        p.y = p.y / Screen.height;
        return p;
    }
    Material lineMaterial;
    public bool isUseWindow = true;
    void OnPostRender()
    {
        if (isUseWindow)
        {
            GL.PushMatrix();
            lineMaterial.SetPass(0);
            GL.LoadOrtho();
            GL.Begin(GL.LINES);
            GL.Color(Color.green);

            GL.Vertex3(pos(-1, 1).x, pos(-1, 1).y, 0);
            GL.Vertex3(pos(-1, -1).x, pos(-1, -1).y, 0);

            GL.Vertex3(pos(-1, -1).x, pos(-1, -1).y, 0);
            GL.Vertex3(pos(1, -1).x, pos(1, -1).y, 0);

            GL.Vertex3(pos(1, -1).x, pos(1, -1).y, 0);
            GL.Vertex3(pos(1, 1).x, pos(1, 1).y, 0);

            GL.Vertex3(pos(-1, 1).x, pos(-1, 1).y, 0);
            GL.Vertex3(pos(1, 1).x, pos(1, 1).y, 0);

            GL.End();
            GL.PopMatrix();
        }

    }
}

 

「LLM那些事」系列第 4 篇《上下文窗口的边界》,文章连接:https://blog.csdn.net/houwenjin/article/details/163999753。 演示什么:在「预测」Sheet 的黄色格子里输入一句话(默认「来泡一杯」),四个「模型」——分别只统计最后 1 / 2 / 3 / 4 个字的 n-gram 查表——同时预测下一个字。同一个输入,看的上下文越长,候选越少、预测越确定: ┌────────────────┬──────────┬───────────────┬──────┐ │ 只看最后几个字 │ 用的前缀 │ 候选下一字数 │ 预测 │ ├────────────────┼──────────┼───────────────┼──────┤ │ 1 个 │ 杯 │ 3(茶/子/水) │ 模糊 │ ├────────────────┼──────────┼───────────────┼──────┤ │ 2 个 │ 一杯 │ 2(茶/水) │ 收窄 │ ├────────────────┼──────────┼───────────────┼──────┤ │ 3 个 │ 泡一杯 │ 1(茶) │ 确定 │ ├────────────────┼──────────┼───────────────┼──────┤ │ 4 个 │ 来泡一杯 │ 1(茶) │ 确定 │ └────────────────┴──────────┴───────────────┴──────┘
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值