protected override void ApplyMode(Mode prevMode) { switch (mode) { case Mode.Ghost: mBalloonSpriteDefaultColor = balloonSprite.color; mRopeSpriteDefaultColor = ropeSprite.color; mWidgetDefaultColor = widgetSprite.color; widgetRoot.gameObject.SetActive(true); mBody.simulated = false; mJoint.enabled = false; UpdatePositionFromEditPos(); break; case Mode.Solid: widgetRoot.gameObject.SetActive(false); balloonSprite.color = mBalloonSpriteDefaultColor; ropeSprite.color = mRopeSpriteDefaultColor; widgetSprite.color = mWidgetDefaultColor; //setup physics //setup joint var rayPt = (Vector2)transform.position - new Vector2(0f, mColl.radius + 0.001f); int numHit = Physics2D.CircleCastNonAlloc(rayPt, ropeCheckRadius, Vector2.down, mRaycastHits, ropeLength, ropeCheckLayerMask); if (numHit > 0) { var hit = mRaycastHits[numHit - 1]; var hitBody = hit.rigidbody; var hitColl = hit.collider; if (hitBody) { //check if it's an entity mEntAttach = hitBody.GetComponent <M8.EntityBase>(); if (mEntAttach) { mEntAttach.releaseCallback += OnAttachDespawn; } mJoint.connectedBody = hitBody; mJoint.connectedAnchor = hitBody.transform.worldToLocalMatrix.MultiplyPoint3x4(hit.point); } else { //just setup a fixed point mJoint.connectedBody = null; mJoint.connectedAnchor = hit.point; } mJoint.enabled = true; } else { mJoint.enabled = false; } mBody.simulated = true; mIsForceActive = false; mLastTime = Time.fixedTime; break; } }
new void UpdateHit(float dT) { pups.Clear(); hitCount = Physics2D.BoxCastNonAlloc(transform.position, box.size, 0, velocity, RaycastHits, Mathf.Max(raylength, velocity.magnitude * dT), HitLayers); for (int i = 0; i < hitCount; i++) { hit = RaycastHits[i]; if (hit.transform.IsChildOf(transform)) { continue; } ITrigger tri = hit.transform.GetComponent <ITrigger>(); if (tri != null) { tri.Trigger(transform); } IDamage dam = hit.transform.GetComponent <IDamage>(); if (dam != null) { // maybe only damage other if charging weapon or has active powerup? if (ContactDamage != null) { Damage dmg = Instantiate(ContactDamage); dmg.instigator = this; dmg.damageSource = transform; dmg.point = hit.point; dam.TakeDamage(dmg); } } } pups.Clear(); hitCount = Physics2D.CircleCastNonAlloc(transform.position, selectRange, Vector3.zero, RaycastHits, 0, Global.WorldSelectableLayers); for (int i = 0; i < hitCount; i++) { hit = RaycastHits[i]; IWorldSelectable pup = hit.transform.GetComponent <IWorldSelectable>(); if (pup != null) { pups.Add((Component)pup); /*if( !highlightedPickups.Contains( pup ) ) * { * pup.Highlight(); * highlightedPickups.Add( pup ); * }*/ } } //WorldSelectable closest = (WorldSelectable)FindClosest( transform.position, pups.ToArray() ); IWorldSelectable closest = (IWorldSelectable)Util.FindSmallestAngle(transform.position, shoot, pups.ToArray()); if (closest == null) { if (closestISelect != null) { closestISelect.Unhighlight(); closestISelect = null; } if (WorldSelection != null) { WorldSelection.Unselect(); WorldSelection = null; } } else if (closest != closestISelect) { if (closestISelect != null) { closestISelect.Unhighlight(); } closestISelect = closest; closestISelect.Highlight(); } /*highlightedPickupsRemove.Clear(); * foreach( var pup in highlightedPickups ) * if( !pups.Contains( pup ) ) * { * pup.Unhighlight(); * highlightedPickupsRemove.Add( pup ); * } * foreach( var pup in highlightedPickupsRemove ) * highlightedPickups.Remove( pup ); */ }
public override float[] Effect(Character character) { // Causing the character to be casting as to not allow any abilities // or auto attacking character.SetCasting(true); // The value for the character position Vector2 charPos = character.transform.position; var hit2D = new RaycastHit2D[50]; Physics2D.CircleCastNonAlloc(charPos, 0.5f, dir, hit2D, 10f * Time.deltaTime); hit2D = hit2D.Where(x => x).ToArray(); Debug.DrawLine(charPos, charPos + dir * (speed * Time.deltaTime)); // Checks if there is a wall in that position if (hit2D.Length > 1) { // Checks for all the colliders with a different name than the character // with the knockback ability foreach (var charHit in hit2D) { if (charHit.transform == null) { continue; } // Getting the character component of the player var hitChar = charHit.transform.GetComponent <Character>(); // Checking if (hitChar != null && hitChar != character) { // Checking, if the character is an enemy, if the other character is // an enemy if (character is Enemy && hitChar is Enemy aEnemy) { // Checks if the enemy has been knocked back already if (aEnemy.GetStatus <KnockBack>() == null) { // Finding the direction between itself and the enemy var enemyDir = charHit.point - charPos; // Adds the knockback effect to the next character aEnemy.AddStatus(new KnockBack(currentTimer, enemyDir)); } } } // If not, checks if the collider is a wall else if (charHit.transform.CompareTag("Obstacle")) { currentTimer = 0; character.AddStatus(new Root(2)); } } } if (currentTimer != 0) { // Pushes the character a certain distance backwards character.transform.position = Vector2.MoveTowards(charPos, charPos + dir, speed * Time.deltaTime); } // Calling the base effect method return(base.Effect(character)); }
int GetHit2D_CircleCollider() { return(Physics2D.CircleCastNonAlloc(p_pTransformTarget.position + (Vector3)_pCircleCollider2D_Current.offset, _pCircleCollider2D_Current.radius, Vector2.zero, _arrHitInfo2D, Mathf.Infinity, p_pLayerMask)); }
static public IEnumerable <RaycastHit2D> CircleCastAll(Vector2 position, Vector2 direction, float radius, float max_distance = float.PositiveInfinity, int layer_mask = IntBits.ALL_BITS) { return(RAYCAST_HIT_POOL.UseEnumerateExpand(delegate(RaycastHit2D[] hits) { return Physics2D.CircleCastNonAlloc(position, radius, direction, hits, max_distance, layer_mask); })); }
public void UpdatePath() { if (HasPath) { if (waypoint.Count > 0) { if (Time.time - PathEventTime > Global.instance.RepathInterval) { PathEventTime = Time.time; SetPath(DestinationPosition, OnPathEnd); } // follow path if waypoints exist Vector3 waypointFlat = waypointEnu.Current; waypointFlat.z = 0; if (Vector3.SqrMagnitude(transform.position - waypointFlat) > WaypointRadii * WaypointRadii) { MoveDirection = (Vector2)waypointEnu.Current - (Vector2)transform.position; } else if (waypointEnu.MoveNext()) { MoveDirection = (Vector2)waypointEnu.Current - (Vector2)transform.position; } else { // destination reached // clear the waypoints before calling the callback because it may set another path and you do not want them to accumulate waypoint.Clear(); #if UNITY_EDITOR debugPath.Clear(); #endif HasPath = false; DestinationPosition = transform.position; // do this to allow OnPathEnd to become null because the callback may set another path without a callback. System.Action temp = OnPathEnd; OnPathEnd = null; if (temp != null) { temp.Invoke(); } } //velocity = MoveDirection.normalized * speed; } #if UNITY_EDITOR // draw path if (debugPath.Count > 0) { Color pathColor = Color.white; if (nvp.status == NavMeshPathStatus.PathInvalid) { pathColor = Color.red; } if (nvp.status == NavMeshPathStatus.PathPartial) { pathColor = Color.gray; } foreach (var ls in debugPath) { Debug.DrawLine(ls.a, ls.b, pathColor); } } #endif } else { // no path MoveDirection = Vector2.zero; } if (Global.instance.GlobalSidestepping && SidestepAvoidance) { if (Time.time - SidestepLast > Global.instance.SidestepInterval) { Sidestep = Vector3.zero; SidestepLast = Time.time; if (MoveDirection.magnitude > 0.001f) { float distanceToWaypoint = Vector3.Distance(waypointEnu.Current, transform.position); if (distanceToWaypoint > Global.instance.SidestepIgnoreWithinDistanceToGoal) { float raycastDistance = Mathf.Min(distanceToWaypoint, Global.instance.SidestepRaycastDistance); int count = Physics2D.CircleCastNonAlloc(transform.position, 0.5f /*box.edgeRadius*/, MoveDirection.normalized, RaycastHits, raycastDistance, Global.CharacterSidestepLayers); for (int i = 0; i < count; i++) { Entity other = RaycastHits[i].transform.root.GetComponent <Entity>(); if (other != null && other != Client) { Vector3 delta = other.transform.position - transform.position; Sidestep = ((transform.position + Vector3.Project(delta, MoveDirection.normalized)) - other.transform.position).normalized * Global.instance.SidestepDistance; break; } } } } } MoveDirection += Sidestep; } #if UNITY_EDITOR Debug.DrawLine(transform.position, (Vector2)transform.position + MoveDirection.normalized * 0.5f, Color.magenta); //Debug.DrawLine( transform.position, transform.position + FaceDirection.normalized, Color.red ); #endif }
void testRayMulti() { LayerMask combinedLayers = DetectsOnLayers | ObstructedByLayers; RaycastHit2D[] hits; int numberOfHits; if (Radius > 0f) { prepareHitsBuffer(); hits = hitsBuffer; numberOfHits = Physics2D.CircleCastNonAlloc(transform.position, Radius, direction, hits, Length, combinedLayers); if (numberOfHits == CurrentBufferSize) { if (DynamicallyIncreaseBufferSize) { CurrentBufferSize *= 2; testRayMulti(); return; } else { logInsufficientBufferSize(); } } } else { prepareHitsBuffer(); hits = hitsBuffer; numberOfHits = Physics2D.RaycastNonAlloc(transform.position, direction, hits, Length, combinedLayers); if (numberOfHits == CurrentBufferSize) { if (DynamicallyIncreaseBufferSize) { CurrentBufferSize *= 2; testRayMulti(); return; } else { logInsufficientBufferSize(); } } } System.Array.Sort(hits, 0, numberOfHits, distanceComparer); for (int i = 0; i < numberOfHits; i++) { var hit = hits[i]; if ((1 << hit.collider.gameObject.layer & DetectsOnLayers) != 0) { addRayHit(hit); } if ((1 << hit.collider.gameObject.layer & ObstructedByLayers) != 0) { // Potentially blocks the ray, just make sure it isn't in the ignore list if (shouldIgnore(hit.collider.gameObject) || hit.rigidbody != null && shouldIgnore(hit.rigidbody.gameObject)) { // Obstructing collider or its rigid body is in the ignore list continue; } else { obstructionRayHit = hit; break; } } } }