private void RecordProperties() { _startPosition = transform.position; _rigidbody.isKinematic = true; var attachedColliders = new List <Collider2D>(); _rigidbody.GetAttachedColliders(attachedColliders); Bounds?b = null; foreach (var collider in attachedColliders) { if (!b.HasValue) { b = collider.bounds; } else { var bounds = collider.bounds; bounds.Encapsulate(b.Value); b = bounds; } } if (b.HasValue) { var bounds = b.Value; bounds.center = transform.InverseTransformPoint(bounds.center); colliderLocalBounds = bounds; } else { colliderLocalBounds = new Bounds(); } }
private float4 m_playableAreaAabb; // World space axis-aligned bounding box of what the camera can see private void Start() { m_rigidbody = GetComponent <Rigidbody2D>(); m_gameManager = GameManager.gameManager; m_gameManager.health = m_maxHealth; // Calculate the world-space AABB of the sub from its colliders List <Collider2D> colliders = new List <Collider2D>(); m_rigidbody.GetAttachedColliders(colliders); m_aabb = new float4(float.MaxValue, float.MaxValue, float.MinValue, float.MinValue); foreach (var collider in colliders) { var bounds = collider.bounds; float3 min = bounds.min; float3 max = bounds.max; m_aabb.xy = math.min(m_aabb.xy, min.xy); m_aabb.zw = math.max(m_aabb.zw, max.xy); } // Set the origin of the AABB to the position of the sub. float3 pos = transform.position; m_aabb.xyzw -= pos.xyxy; // Calculate the playable area AABB var cam = Camera.main; float3 campos = cam.transform.position; float2 camExtents; camExtents.y = cam.orthographicSize; camExtents.x = camExtents.y * cam.aspect; m_playableAreaAabb = new float4(-camExtents, camExtents) + campos.xyxy; }
private float CalculateWidth() { float leftmostX = transform.position.x + 100f; float rightmostX = transform.position.x - 100f; List <Collider2D> allColliders = new List <Collider2D>(); bool activeState = gameObject.activeSelf; if (!activeState) { gameObject.SetActive(true); } rBody.GetAttachedColliders(allColliders); foreach (Collider2D collider in allColliders) { if (collider.bounds.min.x < leftmostX) { leftmostX = collider.bounds.min.x; } if (collider.bounds.max.x > rightmostX) { rightmostX = collider.bounds.max.x; } } gameObject.SetActive(activeState); return(rightmostX - leftmostX); }
protected virtual void Start() { contactFilter.useTriggers = false; contactFilter.SetLayerMask(Physics2D.GetLayerCollisionMask(gameObject.layer)); contactFilter.useLayerMask = true; List <Collider2D> collidersTmp = new List <Collider2D>(); var colliderCount = body.GetAttachedColliders(collidersTmp); colliders = collidersTmp.Take(colliderCount).Where(_ => _.isTrigger == false).ToArray(); }
private void Awake() { rBody = GetComponent <Rigidbody2D>(); contactFilter.useTriggers = false; contactFilter.SetLayerMask(Physics2D.GetLayerCollisionMask(gameObject.layer)); contactFilter.useLayerMask = true; colliders = new List <Collider2D>(16); overlappedColliders = new List <Collider2D>(16); rBody.GetAttachedColliders(colliders); }
public static Bounds[] GetAllBounds(this Rigidbody2D body) { int count = body.GetAttachedColliders(tempColliders); Bounds[] bounds = new Bounds[count]; for (int i = 0; i < count; i++) { bounds[i] = tempColliders[i].bounds; } return(bounds); }
private static void HandleSpawnSnowball(SpawnSnowball spawn) { if (spawn == null) { throw new WrongPacketTypeException(); } Player player = World.Instance.GetPlayer(spawn.UserID); if (player.Carrying) { return; } GameObject snowball = GameObject.Instantiate(Prefabs.Instance.SnowballPrefab); snowball.transform.position = new Vector2(spawn.PosX, spawn.PosY); Vector2 angle = Quaternion.AngleAxis(spawn.Direction, Vector3.forward) * Vector3.down; Rigidbody2D rigidbody = snowball.GetComponent <Rigidbody2D>(); // Ignore collisions with player who threw it Rigidbody2D playerRigidbody = player.GetComponent <Rigidbody2D>(); // TODO: Make this a helper function in utilities Collider2D[] playerCols = new Collider2D[playerRigidbody.attachedColliderCount]; Collider2D[] snowCols = new Collider2D[rigidbody.attachedColliderCount]; rigidbody.GetAttachedColliders(snowCols); foreach (Collider2D snowCol in snowCols) { foreach (Collider2D playerCol in playerCols) { Physics2D.IgnoreCollision(snowCol, playerCol); } } rigidbody.velocity = angle * Snowball.Speed; int id = World.Instance.AddObject(snowball); snowball.GetComponent <Snowball>().Initialize(id, spawn.UserID); SnowballSync sync = new SnowballSync { ObjectID = id, PosX = spawn.PosX, PosY = spawn.PosY, Direction = spawn.Direction, Velocity = Snowball.Speed, }; Socket.Instance.SendPacket(sync, Packets.SnowballSync); }
/// <summary> /// Shorthand for setting Physics2D.IgnoreCollision for every combination of the Rigidbodies' Colliders. /// </summary> public static void IgnoreCollisions(Rigidbody2D a, Rigidbody2D b, bool ignore) { int aColliderCount = a.attachedColliderCount; int bColliderCount = b.attachedColliderCount; Collider2D[] aColliders = new Collider2D[aColliderCount]; Collider2D[] bColliders = new Collider2D[bColliderCount]; a.GetAttachedColliders(aColliders); b.GetAttachedColliders(bColliders); for (int i = 0; i < aColliderCount; i++) { for (int j = 0; j < bColliderCount; j++) { Physics2D.IgnoreCollision(aColliders[i], bColliders[j], ignore); } } }
void FixedUpdate() { if (!alive && rb2dEnemy) { //Resize the enemy and make it fall to the destroyer Vector3 size = rb2dEnemy.transform.localScale; size.y = .2f; rb2dEnemy.transform.localScale = size; Collider2D[] collidersInObject = new Collider2D[rb2dEnemy.attachedColliderCount]; rb2dEnemy.GetAttachedColliders(collidersInObject); foreach (Collider2D collider in collidersInObject) { collider.enabled = false; } rb2dEnemy.isKinematic = false; } }
public void Move(Vector2 direction) { float distance = direction.magnitude; for (int col = 0; col < rb2d.GetAttachedColliders(attachedColliders); col++) { if (!attachedColliders[col].isTrigger) { // we cast each non-trigger collider in the direction of movement and handle collisions for each one int count = attachedColliders[col].Cast(direction, contactFilter, hitArray, distance + skinWidth); for (int i = 0; i < count; i++) { float realDistance = hitArray[i].distance - skinWidth; distance = (realDistance < distance) ? realDistance : distance; } } } rb2d.position = rb2d.position + direction * distance; }
protected void Movement(Vector2 move, bool yMovement) { float distance = move.magnitude; if (distance > minMoveDistance) { int count = rb2d.Cast(move, contactFilter, hitBuffer, distance + shellRadius); Collider2D[] rbC = new Collider2D[1]; rb2d.GetAttachedColliders(rbC); Collider2D rbCol = rbC[0]; Vector2 pos = new Vector2(rbCol.bounds.center.x, rbCol.bounds.center.y - rbCol.bounds.extents.y); Debug.DrawRay(pos, move * 100, Color.blue); hitBufferList.Clear(); for (int i = 0; i < count; i++) { PlatformEffector2D platform = hitBuffer[i].collider.GetComponent <PlatformEffector2D>(); //PlatformAddition platformAddition = hitBuffer[i].collider.GetComponent<PlatformAddition>(); if (!platform) { hitBufferList.Add(hitBuffer[i]); // get the colliding objects } else if ((velocity.y < 0 && yMovement)) { //hitBuffer[i].normal == Vector2.up && if (hitBuffer[i].normal.y > 0) { //Debug.Log(hitBuffer[i].normal); if (rb2d.IsTouching(hitBuffer[i].collider)) { hitBufferList.Add(hitBuffer[i]); } } } //if (platformAddition) //{ // if (platformAddition.MoveWithPlatform) // { // //Debug.Log("Platform Move: " + platformAddition.rb2d.velocity); // rb2d.velocity += hitBuffer[i].collider.GetComponent<PlatformAddition>().rb2d.velocity; // } //} } for (int i = 0; i < hitBufferList.Count; i++) { Vector2 currentNormal = hitBufferList[i].normal; if (currentNormal.y > minGroundNormalY) { grounded = true; if (yMovement) { groundNormal = currentNormal; currentNormal.x = 0; } } float projection = Vector2.Dot(velocity, currentNormal); if (projection < 0) { velocity = velocity - projection * currentNormal; } float modifiedDistance = hitBufferList[i].distance - shellRadius; distance = modifiedDistance < distance ? modifiedDistance : distance; } } rb2d.position = rb2d.position + move.normalized * distance; }
public static Collider2D[] GetAllColliders(this Rigidbody2D self) { Collider2D[] colliders = new Collider2D[self.attachedColliderCount]; self.GetAttachedColliders(colliders); return(colliders); }