public void setTarget( Vector2 at ) { Target = null; var n = NavMsh.findNode(at, TargetNode); if(n == null) return; TargetNode = n; TargetP = at; }
protected void Start() { // Target = FindObjectOfType<PlayerController>(); NavMsh = FindObjectOfType <NavMesh>(); TargetNode = CurNode = NavMsh.findNode(Trnsfrm.position, CurNode); TargetP = ValidPos = Trnsfrm.position; invincibleTimer = invincibleTimerInit; }
public void setTarget(Vector2 at) { Target = null; var n = NavMsh.findNode(at, TargetNode); if (n == null) { return; } TargetNode = n; TargetP = at; }
public void Start() { var sys = Sys.get(); State_Dat = sys.SiteDat[InitOptionsI]; var nm = FindObjectOfType<NavMesh>(); Dp_Node = nm.findNode(DropPoint.position); Index = sys.Site.IndexOf(this); if(Dp_Node == null || Index == -1 ) { Debug.Log("err "+name +" "+DropPoint.position ); // Destroy(gameObject); } }
protected void Update() { if( NavMsh == null ) return; CurNode = NavMsh.findNode(Trnsfrm.position, CurNode); if(Target != null) { TargetP = Target.Trnsfrm.position; TargetNode = Target.CurNode; } Vector2 tPos = TargetP, cPos = Trnsfrm.position; //Debug.DrawLine(tPos, cPos); // if target moved much - need to recalculate path if(Path != null && LastPath -Time.frameCount < -10 ) { // Debug.Log(" LastPath -Time.frameCount " + (LastPath - Time.frameCount) ); if((Path.Smooth[0].P - tPos).sqrMagnitude > 0.25f || LastPath - Time.frameCount < -180 ) Path = null; } if(Path == null) { Path = NavMsh.getPath(cPos, tPos, TargetNode); if(Path != null) { LastPath = Time.frameCount; CurNodeI = Path.Smooth.Count - 2; ValidPos = Trnsfrm.position; // Debug.Log("pc " + Path.Smooth.Count + " cn " + CurNode); } else { CurNodeI = -1; // Debug.Log("path fail"); } } ///funnel! // basicly recursivlely look at edge we must travers to get to next node until we find a corner in our way - then move thataway // no corner in the way means just go towards target Vector2 vec, cnrA, cnrB; if(Path != null) { for(; CurNodeI > 0; CurNodeI--) { //path is backwards - because .... reasons // tPos = Path.Smooth[CurNode].P; /// if(Util.sign(Path.Smooth[CurNodeI].E2, cPos, Path.Smooth[CurNodeI].E1) < 0) { // if so then we passed through an edge - advance our place along the path // -done awkard way because fo how things were refactored - not neatening because it won't be more efficent - and might somehow implode //CurNode = Path.Smooth[CurNodeI].;///todo continue; } // the arc defined between cnrA < cPos > cnrB is the range of current valid directions cnrA = smoothHelper(Path.Smooth[CurNodeI].E1, cPos, true); cnrB = smoothHelper(Path.Smooth[CurNodeI].E2, cPos, false); float sgn = -1; // int msi = MaxSmoothIter; for(int ci = CurNodeI - 1; ; ci--) { if(ci <= 0) break; //reached end of path // only one of these will be different from current corners -- this would be an obvious place to optimise (todo) Vector2 nCnrA = smoothHelper(Path.Smooth[ci].E1, cPos, true); Vector2 nCnrB = smoothHelper(Path.Smooth[ci].E2, cPos, false); //new corner may refine our current valid arc // it may refine it so far that the angle of the arc becomes 0 - ie a direction - cnrA . cnrB would be exactly the same direction - if so then we are done here if((nCnrA - cnrA).sqrMagnitude > (nCnrB - cnrB).sqrMagnitude) { //Debug.DrawLine(cPos, fnlA2, Color.black); sgn = Util.sign(nCnrA, cPos, cnrA); if(Util.sign(nCnrA, cPos, cnrA) > 0) { if(Util.sign(cnrB, cPos, nCnrA) < 0) { tPos = cnrB; // Debug.Log("breakb"); break; } cnrA = nCnrA; } } else { //Debug.DrawLine(cPos, fnlB2, Color.black); sgn = Util.sign(cnrB, cPos, nCnrB); if(Util.sign(cnrB, cPos, nCnrB) > 0) { if(Util.sign(cnrB, cPos, nCnrA) < 0) { tPos = cnrA; // Debug.Log("breaka"); break; } cnrB = nCnrB; } } // if( msi-- == 0 ) break; } if(Util.sign(cnrB, cPos, tPos) < 0) tPos = cnrB; if(Util.sign(tPos, cPos, cnrA) < 0) tPos = cnrA; /*// Debug.Log("sgn " + sgn); Debug.DrawLine(cPos, fnlA, Color.green); Debug.DrawLine(cPos, fnlB, Color.red); Debug.DrawLine(cPos, tPos, Color.white); */ break; } } else { if(NavMsh.findNode(cPos) != TargetNode) //fallen off map somehow ... used to happen when colliders dind match map and also current node wasn't clamped -- try and move back to last valid position tPos = ValidPos; } vec = tPos - cPos; DesVec = vec; if (isInvincible) { if ((invincibleTimer -= Time.deltaTime) <= 0) { isInvincible = false; invincibleTimer = invincibleTimerInit; } } }
protected void Start() { // Target = FindObjectOfType<PlayerController>(); NavMsh = FindObjectOfType<NavMesh>(); TargetNode = CurNode = NavMsh.findNode(Trnsfrm.position, CurNode); TargetP = ValidPos = Trnsfrm.position; invincibleTimer = invincibleTimerInit; }
protected void Update() { if (NavMsh == null) { return; } CurNode = NavMsh.findNode(Trnsfrm.position, CurNode); if (Target != null) { TargetP = Target.Trnsfrm.position; TargetNode = Target.CurNode; } Vector2 tPos = TargetP, cPos = Trnsfrm.position; //Debug.DrawLine(tPos, cPos); // if target moved much - need to recalculate path if (Path != null && LastPath - Time.frameCount < -10) { // Debug.Log(" LastPath -Time.frameCount " + (LastPath - Time.frameCount) ); if ((Path.Smooth[0].P - tPos).sqrMagnitude > 0.25f || LastPath - Time.frameCount < -180) { Path = null; } } if (Path == null) { Path = NavMsh.getPath(cPos, tPos, TargetNode); if (Path != null) { LastPath = Time.frameCount; CurNodeI = Path.Smooth.Count - 2; ValidPos = Trnsfrm.position; // Debug.Log("pc " + Path.Smooth.Count + " cn " + CurNode); } else { CurNodeI = -1; // Debug.Log("path fail"); } } ///funnel! // basicly recursivlely look at edge we must travers to get to next node until we find a corner in our way - then move thataway // no corner in the way means just go towards target Vector2 vec, cnrA, cnrB; if (Path != null) { for (; CurNodeI > 0; CurNodeI--) //path is backwards - because .... reasons // tPos = Path.Smooth[CurNode].P; /// { if (Util.sign(Path.Smooth[CurNodeI].E2, cPos, Path.Smooth[CurNodeI].E1) < 0) { // if so then we passed through an edge - advance our place along the path // -done awkard way because fo how things were refactored - not neatening because it won't be more efficent - and might somehow implode //CurNode = Path.Smooth[CurNodeI].;///todo continue; } // the arc defined between cnrA < cPos > cnrB is the range of current valid directions cnrA = smoothHelper(Path.Smooth[CurNodeI].E1, cPos, true); cnrB = smoothHelper(Path.Smooth[CurNodeI].E2, cPos, false); float sgn = -1; // int msi = MaxSmoothIter; for (int ci = CurNodeI - 1; ; ci--) { if (ci <= 0) { break; //reached end of path } // only one of these will be different from current corners -- this would be an obvious place to optimise (todo) Vector2 nCnrA = smoothHelper(Path.Smooth[ci].E1, cPos, true); Vector2 nCnrB = smoothHelper(Path.Smooth[ci].E2, cPos, false); //new corner may refine our current valid arc // it may refine it so far that the angle of the arc becomes 0 - ie a direction - cnrA . cnrB would be exactly the same direction - if so then we are done here if ((nCnrA - cnrA).sqrMagnitude > (nCnrB - cnrB).sqrMagnitude) { //Debug.DrawLine(cPos, fnlA2, Color.black); sgn = Util.sign(nCnrA, cPos, cnrA); if (Util.sign(nCnrA, cPos, cnrA) > 0) { if (Util.sign(cnrB, cPos, nCnrA) < 0) { tPos = cnrB; // Debug.Log("breakb"); break; } cnrA = nCnrA; } } else { //Debug.DrawLine(cPos, fnlB2, Color.black); sgn = Util.sign(cnrB, cPos, nCnrB); if (Util.sign(cnrB, cPos, nCnrB) > 0) { if (Util.sign(cnrB, cPos, nCnrA) < 0) { tPos = cnrA; // Debug.Log("breaka"); break; } cnrB = nCnrB; } } // if( msi-- == 0 ) break; } if (Util.sign(cnrB, cPos, tPos) < 0) { tPos = cnrB; } if (Util.sign(tPos, cPos, cnrA) < 0) { tPos = cnrA; } /*// Debug.Log("sgn " + sgn); * Debug.DrawLine(cPos, fnlA, Color.green); * Debug.DrawLine(cPos, fnlB, Color.red); * Debug.DrawLine(cPos, tPos, Color.white); */ break; } } else { if (NavMsh.findNode(cPos) != TargetNode) //fallen off map somehow ... used to happen when colliders dind match map and also current node wasn't clamped -- try and move back to last valid position { tPos = ValidPos; } } vec = tPos - cPos; DesVec = vec; if (isInvincible) { if ((invincibleTimer -= Time.deltaTime) <= 0) { isInvincible = false; invincibleTimer = invincibleTimerInit; } } }
protected void updatePath() { if(NavMsh == null) return; var lCn = CurNode; CurNode = NavMsh.findNode(SyncO.Body. position, CurNode); if(CurNode == null) { Debug.Log("no node.."); Debug.DrawLine(Trnsfrm.position, Vector3.zero); } checkTarget(); Vector2 biasOff = (Vector2)Trnsfrm.up * RoughRadius*2 + Body.velocity / Acceleration; //cPos = SyncO.Body.position Debug.DrawLine(SyncO.Body.position + biasOff, SyncO.Body.position, Color.black); if(!PathActive) return; Vector2 tPos = TargetP, cPos = SyncO.Body.position; Vector2 tPos2 = tPos; if(CurNode != TargetNode) { if(Path != null && LPathTime - Time.time < -0.2f) { if(LPathTime - Time.time < -2.0f) Path = null; //recalc every so often else { var sqDiff = (LTPos - tPos).sqrMagnitude; // if target moved much - need to recalculate path if(sqDiff > 4.0f || (LTNode != TargetNode && sqDiff > 0.5f)) Path = null; } } if(Path != null && lCn != CurNode) { if(!fixNodeI(ref CurNodeI, CurNode)) { ///not resolved!! -- we got shunted off to side most likely.. --- or possibly rewound but too far - (likely cos we just repathed) // Debug.Log("SHUNTED!!"); Path = null; //todo - we may be able to quick fix some of thse cases } } if(Path == null) { Path = NavMsh.getPath2(cPos, cPos+ biasOff, tPos, TargetNode); if(Path != null) { LTPos = tPos; LTNode = TargetNode; LPathTime = Time.time; LSmoothTime = -1.0f; CurNodeI = Path.Smooth.Count - 1; if(CurNode != Path.Smooth[CurNodeI].N) Debug.LogError("err"); // ValidPos = Trnsfrm.position; // Debug.Log("pc " + Path.Smooth.Count + " cn " + CurNode); } else { CurNodeI = -1; // Debug.Log("path fail"); } } ///funnel! // basicly recursivlely look at edge we must travers to get to next node until we find a corner in our way - then move thataway // no corner in the way means just go towards target if(Time.time - LSmoothTime > 0.5f) { //todo - can check if we have diverged from path to affect resmooth var smthP = WorkingSmoothPath; smthP.Clear(); if(Path != null) { #if DRAW_NAV_LINES Vector2 lastPos = cPos; for(int i = CurNodeI; i-- > 0; ) { Debug.DrawLine(lastPos, Path.Smooth[i].P, Color.white); Debug.DrawLine(lastPos, Path.Smooth[i].E1, Color.grey); Debug.DrawLine(lastPos, Path.Smooth[i].E2, Color.grey); lastPos = Path.Smooth[i].P; } Debug.DrawLine(lastPos, tPos, Color.white); #endif // for(; CurNodeI >= 0; CurNodeI--) { //path is backwards - because .... reasons funnel(cPos, ref tPos, CurNodeI); var lp = cPos; var cp = tPos; var cni = CurNodeI; var n = CurNode; float dis = (lp - cp).magnitude; float lDis = 0; float maxDis = 25; for(int maxIter = 10; maxIter-- > 0; ) { // Debug.Log("iter " + maxIter); smthP.Add(cp); var tp = TargetP; if(dis > maxDis) { // Debug.Log("PASS dis " + maxIter); var v = (cp - lp); #if DRAW_NAV_LINES Debug.DrawLine(lp, lp + v * (maxDis - lDis) / v.magnitude, Color.black); #endif break; } if((tp - cp).sqrMagnitude < 0.5f) { // Debug.Log("PASS " + maxIter); #if DRAW_NAV_LINES Debug.DrawLine(lp, cp, Color.black); #endif break; } #if DRAW_NAV_LINES Debug.DrawLine(lp, cp, Color.black); #endif var ln = n; n = NavMsh.findNode(cp, n); if(ln != n) { if(n == TargetNode) { // Debug.Log("PASS " + maxIter); #if DRAW_NAV_LINES Debug.DrawLine(cp, tp, Color.black); #endif smthP.Add(tp); break; } if(!fixNodeI(ref cni, n)) { // Debug.Log("FAIL --- you shall not pass"); smthP.Add(tp); //hope for the best break; } } funnel(cp, ref tp, cni); float d = (cp - tp).magnitude; // Debug.Log("dis " + d); float minStep = 0.75f; if(d < minStep) { tp = cp + (tp - cp) * minStep / d; d = minStep; } lDis = dis; dis += d; // break; lp = cp; cp = tp; } } else { smthP.Add(TargetP); if(CurNode != TargetNode) //fallen off map somehow ... used to happen when colliders dind match map and also current node wasn't clamped -- try and move back to last valid position // tPos = ValidPos; Debug.Log("Awk noes we appear to have fallen off the map"); } if( SmoothPath.Count > 0 ) { if(Vector2.Dot((Body.position - smthP[0]).normalized, (Body.position - SmoothPath[SyncO.SPi]).normalized) < 0.8f) steerUpdate(); } else steerUpdate(); SmoothPath = new List<Vector2>(smthP); //todo SPi = SyncO.SPi = 0; } } else { Path = null; //bool dirty = false; if(SmoothPath.Count < 1) { // dirty = true; SmoothPath.Add(TargetP); steerUpdate(); } else if( (SmoothPath[0]- TargetP).sqrMagnitude > 0.5f ) { // dirty = true; if( Vector2.Dot( (Body.position - TargetP).normalized, (Body.position - SmoothPath[0]).normalized ) < 0.8f ) steerUpdate(); SmoothPath.Clear(); SmoothPath.Add(TargetP); } SPi = SyncO.SPi = 0; } // Debug.DrawLine( Body.position, tPos, Color.white); // Debug.DrawLine( tPos2, tPos, Color.white); //DesPos = tPos; //vec = tPos - cPos; // DesVec = vec; }
/* public bool RefreshLA = true; void localAvoidance() { LocalAvoidance_FB.reset(); LocalAvoidance_FB.line( Trnsfrm.position, TargetP, Color.red ); Vector2 cp = Trnsfrm.position; var vec = TargetP - cp; LocalAvoidance_FB.sphere(cp + vec.normalized * 4, 1, Color.red); } GizmoFeedBack LocalAvoidance_FB = new GizmoFeedBack(); */ protected void checkTarget() { if(Target != null) { Vector2 tp = Target.Trnsfrm.position; var vec = tp - SyncO.Body.position; var sm = vec.sqrMagnitude; if(sm < Util.pow2( EngageRange * 0.9f )) { SyncO.PathActive = PathActive = false; } else if(sm > EngageRange * EngageRange ) SyncO.PathActive = PathActive = true; if(PathActive) { var tk = Target as Unit_Kinematic; if(tk != null) { TargetP = tp; TargetNode = tk.CurNode; } else { var tb = Target as Unit_Structure; TargetP = tb.Site.DropPoint.position; TargetNode = tb.Site.Dp_Node; } } else { TargetNode = CurNode; TargetP = SyncO.Body.position; } } }
new protected void Start() { base.Start(); // DesPos = transform.position; NavMsh = FindObjectOfType<NavMesh>(); TargetNode = CurNode = NavMsh.findNode(Trnsfrm.position); // Rpc_DesPos(Body.position); //WRONG!! }
public void Rpc_attackUnit(GameObject trgt) { if(trgt == null) return; var tu = trgt.GetComponent<Unit>(); if(tu == null) return; var tuk = tu as Unit_Kinematic; if(tuk == null) { var tb = tu as Unit_Structure; TargetP = tb.Site.DropPoint.position; TargetNode = tb.Site.Dp_Node; } else { TargetNode = tuk.CurNode; TargetP = tuk.Body.position; } Target = tu; EngageRange = calcEngageRange(Target); EngageRange += tu.RoughRadius * 0.75f; foreach(var t in Trgtn) t.Timer = 0; PathActive = true; SyncO.PathActive = true; }
public Unit_SyncHelper SyncO; //todo client only virtual protected bool desPos(Vector2 dp) { var n = NavMsh.findNode(dp, TargetNode); //err here if(n == null) return false; TargetNode = n; TargetP = dp; Target = null; PathActive = true; SyncO.PathActive = true; return true; }