Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class AxisAlignedPoly
- {
- public static bool ContainsPoint(Transform[] polyPoints, Transform Target)
- {
- var j = polyPoints.Length - 1;
- var inside = false;
- Vector3 p = Target.transform.position;
- for (int i = 0; i < polyPoints.Length; j = i++)
- {
- var pi = polyPoints[i].transform.position;
- var pj = polyPoints[j].transform.position;
- if (((pi.z <= p.z && p.z < pj.z) || (pj.z <= p.z && p.z < pi.z)) &&
- (p.x < (pj.x - pi.x) * (p.z - pi.z) / (pj.z - pi.z) + pi.x))
- inside = !inside;
- }
- return inside;
- }
- }
- public class FenceIn : MonoBehaviour
- {
- public Transform[] FencePoints;
- public GameObject Target;
- Material TargetMaterial;
- void Start()
- {
- TargetMaterial = Target.GetComponent<MeshRenderer>().material;
- }
- private void Update()
- {
- if (AxisAlignedPoly.ContainsPoint(FencePoints, Target.transform))
- {
- TargetMaterial.SetColor("_BaseColor", Color.green);
- }
- else
- {
- TargetMaterial.SetColor("_BaseColor", Color.red);
- }
- //DrawConnections(FencePoints);
- }
- public void DrawConnections(Transform[] polyPoints)
- {
- int index = 0;
- while (index < polyPoints.Length-1)
- {
- Vector3 PointA = polyPoints[index].position;
- PointA.y = 0;
- Vector3 PointB = polyPoints[index + 1].position;
- PointB.y = 0;
- Debug.DrawLine(PointA, PointB);
- index += 1;
- }
- if (index == polyPoints.Length-1)
- {
- Vector3 PointA = polyPoints[0].position;
- PointA.y = 0;
- Vector3 PointB = polyPoints[index].position;
- PointB.y = 0;
- Debug.DrawLine(PointA, PointB);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement