67 lines
1.8 KiB
C#
67 lines
1.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
public class ShowPlayer : MonoBehaviour
|
|
{
|
|
public Transform targetPos;
|
|
public float distance = 5f;
|
|
[Header("远离目标区域事件")]
|
|
public UnityEvent onEnterFarRange; // 进入远距离事件
|
|
[Header("进入目标区域事件")]
|
|
public UnityEvent onEnterCloseRange; // 进入近距离事件
|
|
|
|
private bool isInCloseRange = true;
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
private void Update()
|
|
{
|
|
if (targetPos != null)
|
|
{
|
|
// 计算物体与目标点的距离
|
|
float dis = Vector3.Distance(targetPos.position, transform.position);
|
|
|
|
if (dis > distance && isInCloseRange)
|
|
{
|
|
onEnterFarRange.Invoke();
|
|
isInCloseRange = false;
|
|
}
|
|
else if (dis <= distance && !isInCloseRange)
|
|
{
|
|
onEnterCloseRange.Invoke();
|
|
isInCloseRange = true;
|
|
}
|
|
}
|
|
}
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
Player player = other.transform.GetComponent<Player>();
|
|
if (player != null)
|
|
{
|
|
player.user.gameObject.SetActive(true);
|
|
foreach (var item in player.meshRendererObj)
|
|
{
|
|
item.SetActive(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
{
|
|
Player player = other.transform.GetComponent<Player>();
|
|
if (player != null)
|
|
{
|
|
if (player.group != Config.Group)
|
|
{
|
|
player.user.gameObject.SetActive(false);
|
|
foreach (var item in player.meshRendererObj)
|
|
{
|
|
item.SetActive(false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |