// Update is called once per frame void Update() { #if MOVINGSHAHID #else Transform myref = LeaderVehicle.transform; Vector2 Hummer2 = new Vector2(myref.position.x, myref.position.z); Vector2 Shahid2 = new Vector2(ShahidPosition.x, ShahidPosition.y); float targetVehicleDist = Vector2.Distance(Shahid2, Hummer2); Debug.Log("Update: Shahid distance:" + targetVehicleDist.ToString() + "Shahid: (" + Shahid2.x.ToString() + ", " + Shahid2.y.ToString() + ")" + "Hammer: (" + Hummer2.x.ToString() + ", " + Hummer2.y.ToString() + ")"); if ((targetVehicleDist > 10f) && isHummerAfter) { Shahid.SetActive(true); Debug.Log("Hummer is after"); } else if (targetVehicleDist <= 10f) { isHummerAfter = true; Debug.Log("Hummer is less then 10"); } #endif }
void ShahidSetup() { float AlongPath = 0, PerpePath = 0; //Note that if we have 3 obstacles in the XML file, then only the last coordinates are relevant!!! foreach (var shahidXML in file.Descendants("obstacles_on_path").Descendants("obstacle_on_path")) { AlongPath = float.Parse(shahidXML.Element("obstacle_on_path_i_location_along_the_path").Value); //Percentage of the entire path length PerpePath = float.Parse(shahidXML.Element("obstacle_on_path_i_location_perpendicular_to_the_path").Value); //absolutely in meters } Debug.Log("ShahidSetup:AlongPath:" + AlongPath.ToString() + ",PerpePath" + PerpePath.ToString()); float distFromStart = 0; Vector2 WPcurrent = LeaderPose; Vector2 WPnext = LeaderPose; float WPdist = 1, WPang = 0; int WPid = 0; foreach (var wpXML in file.Descendants("Path").Descendants("WayPoint")) { WPdist = float.Parse(wpXML.Element("wp_i_relative_distance").Value); WPang = float.Parse(wpXML.Element("wp_i_relative_angle").Value); WPid = int.Parse(wpXML.Attribute("ID").Value); Vector2 vecToNextWP = new Vector2(Mathf.Sin(WPang), Mathf.Cos(WPang)) * WPdist; WPnext = WPcurrent + vecToNextWP; distFromStart += WPdist; if (distFromStart > pathLength * AlongPath) { Debug.Log("ShahidSetup: WP ID=" + WPid.ToString() + " distFromStart=" + distFromStart.ToString() + " pathLength=" + pathLength.ToString() + " AlongPath=" + AlongPath.ToString()); //The shahid should meet the oshkosh before this WP break; } else { Debug.Log("ShahidSetup: ELSE WP ID=" + WPid.ToString() + " distFromStart=" + distFromStart.ToString() + " pathLength=" + pathLength.ToString() + " AlongPath=" + AlongPath.ToString()); } WPcurrent = WPnext; } #if MOVINGSHAHID Debug.Log("ShahidSetup: WPcurrent=" + WPcurrent.ToString() + " WPnext=" + WPnext.ToString()); float rem_alo = ((AlongPath - distFromStart / pathLength) * pathLength) / WPdist; Debug.Log("ShahidSetup: rem_alo=" + rem_alo.ToString()); float shahid_x = WPcurrent.x + rem_alo * (WPnext.x - WPcurrent.x) - PerpePath * (WPnext.y - WPcurrent.y) / WPdist; float shahid_y = WPcurrent.y + rem_alo * (WPnext.y - WPcurrent.y) + PerpePath * (WPnext.x - WPcurrent.x) / WPdist; Vector2 ShahidPose = new Vector2(shahid_x, shahid_y); - //Vector2 shahidPose = Vector2.Lerp(WPcurrent,WPnext,rem_alo); #endif terrainAttachment shahidPoseOnterrain = Shahid.GetComponent <terrainAttachment>(); ShahidWPController shahidWpController = Shahid.GetComponent <ShahidWPController>(); #if MOVINGSHAHID shahidPoseOnterrain.moveTo(new Vector3(ShahidPose.x, ShahidPose.y, 0.0f)); -shahidWpController.shahidTargetPoseAndVel = new Vector3(ShahidPose.x, ShahidPose.y, 0.0f); -Debug.Log("ShahidSetup: Shahid position:" + ShahidPose.x.ToString() + "," + ShahidPose.y.ToString()); #else ShahidPosition = new Vector3(WPnext.x, WPnext.y, 0.0f); shahidPoseOnterrain.moveTo(new Vector3(WPnext.x, WPnext.y, 0.0f)); shahidWpController.shahidTargetPoseAndVel = new Vector3(WPnext.x, WPnext.y, 0.0f); Debug.Log("ShahidSetup: Shahid position:" + WPnext.x.ToString() + "," + WPnext.y.ToString()); Shahid.SetActive(false); #endif }