Forums › 💬 NodeCanvas › 🗨️ General Discussion › 2D support › Reply To: 2D support
Hello,
Indeed, I’ve missed the Check Line of Sight 2D version. Here is the task 🙂
CheckLOS2D.cs
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
using UnityEngine; using NodeCanvas.Variables; using System.Linq; namespace NodeCanvas.Conditions{ [Name("Check Line Of Sight 2D")] [Category("GameObject")] [AgentType(typeof(Transform))] [Description("Check of agent is in line of sight with target by doing a linecast and optionaly save the distance")] public class CheckLOS2D : ConditionTask{ [RequiredField] public BBGameObject LosTarget; [BlackboardOnly] public BBFloat saveDistanceAs; private RaycastHit2D[] hits; [GetFromAgent] private Collider2D agentCollider; protected override string info{ get {return "LOS with " + LosTarget.ToString();} } protected override bool OnCheck(){ hits = Physics2D.LinecastAll(agent.transform.position, LosTarget.value.transform.position); foreach (Collider2D collider in hits.Select(h => h.collider)){ if (collider != agentCollider && collider != LosTarget.value.GetComponent<Collider2D>()) return false; } return true; } protected override void OnGizmosSelected(){ if (agent && LosTarget.value) Gizmos.DrawLine(agent.transform.position, LosTarget.value.transform.position); } } } |
I will also double check the 2D versions and make sure to have them there. Let me know if you have anything particular in mind and I will add it.
Cheers!
