unity检测范围内敌人_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);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值