// TP us to some position public bool TP_ToPos(Vector3 InPoint) { // first of all we spawn particle if (TP_Particle != null) { GameObject MyPar = Instantiate(TP_Particle, transform.position, transform.rotation); // For particle attract to our finale position ParticleAttractor MyAttractor = MyPar.GetComponent <ParticleAttractor>(); if (MyAttractor != null) { MyAttractor.PointToAttract = transform; } } // Make us invisible GetComponent <SpriteRenderer>().enabled = false; // recharge timer IncideTPTimer = 0.5f; // Make us invincible for bullets gameObject.layer = 15; // if distance allows us to tp in exact point we will return true bool ToReturn = false; Vector2 InPosLocal = InPoint; // if distance more then tp distance we we will tp only on distance of tp if ((InPoint - transform.position).magnitude > TPDistance) { InPosLocal = transform.position + (InPoint - transform.position).normalized * TPDistance; } else { ToReturn = true; } // In this section we check if we colliding with something.. RaycastHit2D[] NewRaycastHit; int Num = SphereHit(out NewRaycastHit, InPoint, 2); // and if yes we adjust location (correcting InPosLocal) if (Num > 0) { float Coeffition; Vector2 MyVector; // we are checking for every hit for (int i = 0; i < Num; ++i) { MyVector = InPosLocal - NewRaycastHit[i].point; // find on which distance from the wall we need to adjust our location Coeffition = Mathf.Abs(MyVector.x * NewRaycastHit[i].normal.x + MyVector.y * NewRaycastHit[i].normal.y); // and adjust it InPosLocal += NewRaycastHit[i].normal * (2 - Coeffition); } } // After all tp us to that position transform.position = InPosLocal; return(ToReturn); }
// so I have a lot of explain here. private bool GoToPosition(Vector2 InPosition) { Vector2 MyOrigin = transform.position; Vector2 InPosLocal; // this thing we need becouse we would adjust location on tp // here we just spawn particle if (MyParticle != null) { GameObject MyPar = Instantiate(MyParticle, transform.position, transform.rotation); // because of specific particle, we need set attractor ParticleAttractor MyAttractor = MyPar.GetComponent <ParticleAttractor>(); if (MyAttractor != null) { MyAttractor.PointToAttract = transform; } } // this how these variable need to act when we in tp GetComponent <SpriteRenderer>().enabled = false; gameObject.layer = 15; // and reset tp timer VisibleTimer = TPTime; TPPenaltyTimer = TPPenalty; // first of all we need to check is our possible finale location in something bool CollRes = IsValidPoint(InPosition); // after that we need to check soft ray (if it will gave false we will tp into strong wall) List <RaycastHit2D> NewList; // if coll res is true (not pointing in anything) and raycast gave false (so we didn't cross any strong wall or somethiing) we just tp in this position if (!SoftRayCast2D(MyOrigin, InPosition, out NewList) && CollRes) { InPosLocal = InPosition; } else { InPosLocal = NewList[NewList.Count - 1].point + NewList[NewList.Count - 1].normal * 0.2f; // but if we was pointing into something, or we cross some strong wall, we mast take last hit from raycast //InPosLocal = InPosLocal + ((Vector2)transform.position - InPosLocal).normalized * 0.2f; } // after we tp we want to adjust our location if possible RaycastHit2D[] NewRaycastHit = new RaycastHit2D[8]; int Num = SphereHit(ref NewRaycastHit, InPosLocal, 2); // if Num == 0, it's meen we didn't hit anything and we did not need to adjust collision if (Num > 0) { float Coeffition; Vector2 MyVector; // from every normal of each hit we have, we adjusting location on distance we need to not colliding with this wall (or some thing) // P.S. all this system of adjusting very simplified and works only with circles for (int i = 0; i < Num; ++i) { MyVector = InPosLocal - NewRaycastHit[i].point; Coeffition = Mathf.Abs(MyVector.x * NewRaycastHit[i].normal.x + MyVector.y * NewRaycastHit[i].normal.y); InPosLocal += NewRaycastHit[i].normal * (2 - Coeffition); } } // after all we just set our position transform.position = InPosLocal; return(true); }