### Unity 屏幕外死亡的敵人的分數顯示在屏幕內

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

public class Test01 : MonoBehaviour
{
Transform cam;//相機物體
Vector3 pos;
// Start is called before the first frame update
void Start()
{
pos = transform.position;
cam = GameObject.FindWithTag("MainCamera").transform;
}

// Update is called once per frame
void Update()
{
Vector3 offset = pos - cam.position;//相對相機的偏移位置
Vector3 camSize = new Vector3(1.51f*(1920f/1080f)-0.2f, 1.51f-0.2f,0);//相機的寬度和高度的一半尺寸，這里假設相機的高度一半為1.51
camSize = camSize - new Vector3(0.2f, 0.2f, 0);//在坐標在屏幕外時，讓自己在屏幕內離邊緣一段距離0.2
//判斷是在屏幕內還是屏幕外
bool isInCam = (Mathf.Abs(offset.x) < camSize.x) && (Mathf.Abs(offset.y) < camSize.y);
//如果是在屏幕內，那么坐標位置等於自己的坐標位置
//如果是在屏幕外，那么坐標位置在屏幕邊緣
if (isInCam) {
transform.position = pos;
}
else {
float x=pos.x;
if (Mathf.Abs(offset.x) > camSize.x) x = Mathf.Sign(offset.x) * camSize.x + cam.position.x;
//x = x + -Mathf.Sign(offset.x) * 0.2f;

float y = pos.y;
if (Mathf.Abs(offset.y) > camSize.y) y = Mathf.Sign(offset.y) * camSize.y + cam.position.y;
//y = y + -Mathf.Sign(offset.y) * 0.2f;

transform.position =new Vector3(x, y, 0);
}
}

//判斷是在屏幕內還是屏幕外
//如果是在屏幕內，那么坐標位置等於自己的坐標位置
//如果是在屏幕外，那么坐標位置在屏幕邊緣

private void OnDrawGizmos()
{
//Gizmos.color = Color.red;
//float x = 1920f / 1080f;
//Gizmos.DrawCube(transform.position, new Vector3(1.51f * (1920f / 1080f), 1.51f, 0) * 2);
}
}```