遮挡透明若没有渐变实现方法:
1、透明中物体存在list中
2、每过一段时间(可以每帧,但是流畅性会降低)摄像机发送一条射线向玩家,out hitInfo
3、list与hitInfo比对,将在list中但是没有在hitInfo中的物体转变成不透明,list.remove;将在hitInfo中但是没有在list中转变成透明,list.add
若有渐变,实现比较麻烦一点,物体有两个状态:转变成透明过程中、转变成不透明过程中,添加两个list(InTransparent,outTransparent)对应两种状态,那么维护两个list即可:
1、未遮挡->遮挡,查看outTransparent,如果有该物体,从outTransparent移入InTransparent,outTransparent remove;如果没有添加新的加入InTransparent
2、遮挡->未遮挡,查看InTransparent,如果有该物体,从InTransparent移入outTransparent,InTransparent remove;如果没有添加新的加入outTransparent
摄像机CameraFollow.cs:
1 public class CameraFollow : MonoBehaviour 2 { 3 4 public static CameraFollow instance; 5 public Transform Player; 6 7 public List<CameraMaskInfo> Mask; 8 public List<CameraMaskInfo> InTransparentList; 9 public List<CameraMaskInfo> OutTransparentList; 10 11 RaycastHit[] hitInfo; 12 13 void Awake() 14 { 15 instance = this; 16 } 17 void Start() 18 { 19 StartCoroutine(IEMask()); 20 } 21 public void ClearMask() 22 { 23 Mask.Clear(); 24 InTransparentList.Clear(); 25 OutTransparentList.Clear(); 26 } 27 28 IEnumerator IEMask() 29 { 30 while (true) 31 { 32 hitInfo = Physics.CapsuleCastAll(FollowTarget.position + Vector3.up * 0.2f, FollowTarget.position - Vector3.up * 0.1f, 0.2f, (transform.position - FollowTarget.position).normalized, 100f, layerMask); 33 bool flag; 34 for (int i = Mask.Count - 1; i >= 0; i--) 35 { 36 flag = false; 37 for (int j = 0; j < hitInfo.Length; j++) 38 { 39 if (Mask[i].GetObj() == hitInfo[j].collider.gameObject) 40 { 41 flag = true; 42 break; 43 } 44 } 45 if (!flag) 46 { 47 Mask[i].PutOutTransparent(); 48 Mask.RemoveAt(i); 49 } 50 } 51 52 for (int i = 0; i < hitInfo.Length; i++) 53 { 54 flag = false; 55 for (int j = 0; j < Mask.Count; j++) 56 { 57 if (hitInfo[i].collider.gameObject == Mask[j].GetObj()) 58 { 59 flag = true; 60 break; 61 } 62 } 63 if (!flag) 64 { 65 CameraMaskInfo maskInfo = new CameraMaskInfo(hitInfo[i].collider.gameObject); 66 maskInfo.PutInTransparent(); 67 Mask.Add(maskInfo); 68 } 69 } 70 } 71 yield return new WaitForSeconds(0.2f); 72 } 73 74 void Update() 75 { 76 for (int i = InTransparentList.Count - 1; i >= 0; i--) 77 { 78 InTransparentList[i].Sub(); 79 } 80 81 for (int i = OutTransparentList.Count - 1; i >= 0; i--) 82 { 83 OutTransparentList[i].Add(); 84 } 85 } 86 }