private void Update() { if (_grabHingeJoint2D != null) { _grabHingeJoint2D.autoConfigureConnectedAnchor = false; } if (Activated == false || GrabbedObject != null) { _grabFocus = null; return; } BehaviorManager.JumpBehavior.Activated = true; List <Collider2D> overlappingColliders = new List <Collider2D>(); if (Physics2D.GetContacts(GrabHitbox, overlappingColliders) > 0) { for (int i = 0; i < overlappingColliders.Count; i++) { if (overlappingColliders[i].gameObject.tag == "Grabbable") { //Debug.Log("Ready to Grab"); _grabFocus = overlappingColliders[i].gameObject; BehaviorManager.JumpBehavior.Activated = false; return; } } } _grabFocus = null; }
private void _CheckCollisions() { Physics2D.GetContacts(boxCollider, contactFilter2D, colliders); if (colliders.Count > 0 && colliders[0] != null) { // Debug.Log("Hit: " + colliders[0].gameObject.name); BulletManager.Instance().ReturnBullet(gameObject); } }
private void _checkCollision() { Physics2D.GetContacts(boxCollider, contactFilter, colliders); if (colliders.Count > 0) { if (colliders[0] != null) { BulletManager.Instance().ReturnBullet(gameObject); } } }
/// <summary> /// Обработчик кадра. Обрабатываем ввод пользователя, если нужно - включаем интерфейс магазина. /// </summary> void Update() { GameObject player = GameObject.FindGameObjectWithTag("Player"); int x = Physics2D.GetContacts( gameObject.GetComponent <Collider2D>(), new Collider2D[] { player.GetComponent <Collider2D>() }); if (Input.GetKeyUp(KeyCode.E) && x > 0) { SwitchVisible(); } }
/// <summary> /// Обработчик кадра. Проверяем ввод пользователя, если нужно, запускаем процедуру подбора предмета. /// </summary> private void Update() { GameObject player = GameObject.FindGameObjectWithTag("Player"); int x = Physics2D.GetContacts( gameObject.GetComponent <Collider2D>(), new Collider2D[] { player.GetComponent <Collider2D>() }); if ((Input.GetKeyDown(KeyCode.E)) && x > 0 && pickable) { PickUp(player.GetComponent <Inventory>()); } }
private void _CheckCollision() { Physics2D.GetContacts(circleCollider, contactFilter, colliders); if (colliders.Count > 0) { if (colliders[0] != null) { BulletManager.Instance().ReturnBullet(PoolType.PLAYER, gameObject); colliders.Clear(); } } }
protected override bool IsOnGround() { var count = Physics2D.GetContacts(collider, contactPoints); for (int i = 0; i < count; i++) { if (Vector2.Angle(contactPoints[i].normal, Vector2.up) < 30) { return(true); } } return(false); }
public void OnTriggerStay2D(Collider2D _other) { detectedEntities = new Collider2D[5]; Physics2D.GetContacts(this.collider, detectedEntities); PlayerManager _detectedPlayer = _other.GetComponentInParent <PlayerManager>(); if (_detectedPlayer != null) { if (_detectedPlayer.team == targetTeam) { float _distance = (this.transform.position - _detectedPlayer.transform.position).magnitude; WarnPlayer(_distance); } } }
protected override bool IsOnGround() { var count = Physics2D.GetContacts(collider, contactPoints); for (int i = 0; i < count; i++) { Debug.DrawRay(contactPoints[i].point, contactPoints[i].normal * 10); if (Vector2.Dot(contactPoints[i].normal, Vector2.up) > 0.4f) { return(true); } } return(false); }
private void CalculateNextDir(Collision2D coll) { if (coll.collider.CompareTag("Wall") || coll.collider.CompareTag("Bullet")) { ContactPoint2D[] contacts = new ContactPoint2D[1]; int contactCount = Physics2D.GetContacts(coll.collider, contacts); //Debug.Log("contactCount : " + contactCount); if (contactCount > 0) { Vector2 _normalVector = contacts[0].normal; //Debug.Log("Normal : " + _normalVector); float _angle = Mathf.Asin(Vector2.Dot(moveDir, _normalVector)) * Mathf.Rad2Deg; //Debug.Log(_angle); nextMoveDir = moveDir - 2 * Vector2.Dot(moveDir, _normalVector) * _normalVector; moveDir = nextMoveDir; } } }
void Update() { ContactPoint2D[] cp = new ContactPoint2D[11]; Collider2D[] colliders = new Collider2D[11]; Physics2D.GetContacts(collider: kolliderKtoryDotyka, contacts: cp); kolliderKtoryDotyka.GetContacts(contacts: cp); kolliderKtoryDotyka.GetContacts(contactFilter: cf, contacts: cp); // // // rig.GetContacts (colliders: colliders); // rig.GetContacts (contactFilter: cf, colliders: colliders); // // rig.GetContacts (contacts: cp); // rig.GetContacts (contactFilter: cf, contacts: cp); cps = new ContactPoint2DSerialized(cp [0]); }
/// генерация золота из трупа (в будущем ещё и лута) private void LootGeneration() { GameObject player = GameObject.FindGameObjectsWithTag("Character")[0]; int x = Physics2D.GetContacts(gameObject.GetComponent <Collider2D>(), new Collider2D[] { player.GetComponent <Collider2D>() }); if (Input.GetKeyDown(KeyCode.E) && x > 0) { isGenerated = true; var value = Random.Range(10, 100); var inventory = FindObjectOfType <Inventory>(); inventory.money += value; FindObjectOfType <QuestNoticeManager>().ShowNotice( new QuestNotice("Нашёл деньги", "+ " + value + " монет")); Debug.Log(inventory.money); } }
private void Update() { List <Collider2D> overlappingColliders = new List <Collider2D>(); if (Physics2D.GetContacts(GroundedCollider2D, overlappingColliders) > 0) { for (int i = 0; i < overlappingColliders.Count; i++) { // if (overlappingColliders[i].gameObject.tag == "Ground") // { Grounded = true; // } } } else { Grounded = false; } }
private void UpdateSlopeAngle(Collider2D pColliderTerrain) { ContactPoint2D[] arrContactPoint = new ContactPoint2D[10]; int iContactCount = Physics2D.GetContacts(pColliderTerrain, _pCollider_Leg, new ContactFilter2D(), arrContactPoint); _fSlopeAngle = 0f; _fSlopeAngle_Signed = 0f; _vecCurrentSlopeNormal = Vector3.zero; for (int i = 0; i < iContactCount; i++) { float fSlopeAngle = Vector2.Angle(arrContactPoint[i].normal, Vector2.up); if (_fSlopeAngle == 0f || _fSlopeAngle > fSlopeAngle) { _fSlopeAngle = fSlopeAngle; _fSlopeAngle_Signed = Vector2.SignedAngle(arrContactPoint[i].normal, Vector2.up); _vecCurrentSlopeNormal = arrContactPoint[i].normal; } } }
void FixedUpdate() { character.Move(moveVector * Time.deltaTime); if (alive && Physics2D.IsTouching(dropCollider, contactFilter)) { Collider2D[] targets = new Collider2D[1]; Physics2D.GetContacts(dropCollider, contactFilter, targets); if (targets.Length > 0) { Collider2D coll = targets[0]; switch (coll.tag) { case "Player": Player.Players p = coll.gameObject.GetComponent <Player>().playerChoice; GM.ScorePoints(p, 15); break; default: break; } } alive = false; Rigidbody2D rb = GetComponent <Rigidbody2D>(); rb.simulated = false; dropCollider.enabled = false; spriteRenderer.enabled = false; if (pickupParticles != null) { Vector3 spawn = transform.position; spawn.z = -1f; GameObject b = GameObject.Instantiate(pickupParticles, spawn, Quaternion.identity); } DieForReal(); } }
private void Update() { bool isGrounded = Physics2D.IsTouchingLayers(col2d, groundLayer); if (isGrounded) { ContactFilter2D filter = new ContactFilter2D(); filter.layerMask = groundLayer; ContactPoint2D[] contacts = new ContactPoint2D[10]; int nb = Physics2D.GetContacts(col2d, filter, contacts); if (nb == 1) { targetAngle = GetLandingAngle(contacts[0].normal); } } else { PlotTrajectory(transform.position, rb.velocity, 0.1f, 0.5f, ref targetAngle); } currentAngle = Mathf.Lerp(currentAngle, targetAngle, Time.deltaTime / timeToRotate); SetSpriteAngle(currentAngle); }
// Update is called once per frame void FixedUpdate() { transform.position = entity.position; transform.localScale = entity.localScale; Physics2D.GetContacts(LOSCollider, contactFilter, colliders); }
private void OnDrawGizmos() { if (_pRigidbody == null || _pCollider_Leg == null || (p_bUseCrouching && _pTransform_CeilingCheck == null)) { EventOnAwake_Force(); } if (p_eDebugFilter.ContainEnumFlag(EDebugFilter.Debug_Level_1)) { bool bHitGround = false; Collider2D pColliderTerrain = null; Collider2D[] arrColliders = Physics2D.OverlapCircleAll((Vector2)transform.position + _pCollider_Leg.offset, _pCollider_Leg.radius, p_sWhatIsGround); for (int i = 0; i < arrColliders.Length; i++) { if (arrColliders[i].gameObject != gameObject) { pColliderTerrain = arrColliders[i]; bHitGround = true; break; } } if (bHitGround) { Gizmos.color = Color.red; } else { Gizmos.color = Color.green; } Gizmos.DrawWireSphere((Vector2)transform.position + _pCollider_Leg.offset, _pCollider_Leg.radius); if (bHitGround && p_bUseSlopeSliding) { Gizmos.color = Color.green; ContactPoint2D[] arrContactPoint = new ContactPoint2D[10]; int iContactCount = Physics2D.GetContacts(pColliderTerrain, _pCollider_Leg, new ContactFilter2D(), arrContactPoint); for (int i = 0; i < iContactCount; i++) { float fSlopeAngle = Vector2.Angle(arrContactPoint[i].normal, Vector2.up); Gizmos.DrawWireSphere(arrContactPoint[i].point, 1f); UnityEditor.Handles.Label(arrContactPoint[i].point, "fSlopeAngle : " + fSlopeAngle); Gizmos.DrawRay(arrContactPoint[i].point, arrContactPoint[i].normal * 1f); } } if (p_bUseCrouching) { if (Physics2D.OverlapCircle(_pTransform_CeilingCheck.position, p_fCeilingRadius, p_sWhatIsGround)) { Gizmos.color = Color.red; } else { Gizmos.color = Color.green; } Gizmos.DrawWireSphere(_pTransform_CeilingCheck.position, p_fCeilingRadius); } if (p_eWallSlidingDetect == EWallSlidingDetect.Collider || p_eWallSlidingDetect == EWallSlidingDetect.RayCasting) { Color pColorLight_Red = new Color(0.7f, 0f, 0f, 0.5f); Color pColorLight_Green = new Color(0f, 0.7f, 0f, 0.5f); if (_pBoxCollider_LeftCheck == null || _pBoxCollider_RightCheck == null) { EventOnAwake_Force(); } if (Physics2D.OverlapBox((Vector2)_pBoxCollider_LeftCheck.transform.position + _pBoxCollider_LeftCheck.offset, _pBoxCollider_LeftCheck.size, 0f, p_sWhatIsGround)) { Gizmos.color = pColorLight_Red; } else { Gizmos.color = pColorLight_Green; } Gizmos.DrawWireCube((Vector2)_pBoxCollider_LeftCheck.transform.position + _pBoxCollider_LeftCheck.offset, _pBoxCollider_LeftCheck.size); if (Physics2D.OverlapBox((Vector2)_pBoxCollider_RightCheck.transform.position + _pBoxCollider_RightCheck.offset, _pBoxCollider_RightCheck.size, 0f, p_sWhatIsGround)) { Gizmos.color = pColorLight_Red; } else { Gizmos.color = pColorLight_Green; } Gizmos.DrawWireCube((Vector2)_pBoxCollider_RightCheck.transform.position + _pBoxCollider_RightCheck.offset, _pBoxCollider_RightCheck.size); } if (p_eWallSlidingDetect == EWallSlidingDetect.RayCasting) { // 왠지 모르게 Bounds가 잘 구해지지 않아서 직접 구현 Vector2 vecRayPos = Vector2.zero; vecRayPos.x = _pBoxCollider_LeftCheck.transform.position.x + _pBoxCollider_LeftCheck.offset.x + (_pBoxCollider_LeftCheck.size.x / 2f); vecRayPos.y = _pBoxCollider_LeftCheck.transform.position.y + _pBoxCollider_LeftCheck.offset.y - (_pBoxCollider_LeftCheck.size.y / 2f); if (Physics2D.Raycast(vecRayPos, Vector2.left, _pBoxCollider_LeftCheck.size.x, p_sWhatIsGround)) { Gizmos.color = Color.red; } else { Gizmos.color = Color.green; } Gizmos.DrawRay(vecRayPos, Vector2.left * _pBoxCollider_LeftCheck.size.x); vecRayPos.y = _pBoxCollider_LeftCheck.transform.position.y + _pBoxCollider_LeftCheck.offset.y + (_pBoxCollider_LeftCheck.size.y / 2f); if (Physics2D.Raycast(vecRayPos, Vector2.left, _pBoxCollider_LeftCheck.size.x, p_sWhatIsGround)) { Gizmos.color = Color.red; } else { Gizmos.color = Color.green; } Gizmos.DrawRay(vecRayPos, Vector2.left * _pBoxCollider_LeftCheck.size.x); vecRayPos.x = _pBoxCollider_RightCheck.transform.position.x + _pBoxCollider_RightCheck.offset.x - (_pBoxCollider_RightCheck.size.x / 2f); vecRayPos.y = _pBoxCollider_RightCheck.transform.position.y + _pBoxCollider_RightCheck.offset.y - (_pBoxCollider_RightCheck.size.y / 2f); if (Physics2D.Raycast(vecRayPos, Vector2.right, _pBoxCollider_RightCheck.size.x, p_sWhatIsGround)) { Gizmos.color = Color.red; } else { Gizmos.color = Color.green; } Gizmos.DrawRay(vecRayPos, Vector2.right * _pBoxCollider_RightCheck.size.x); vecRayPos.y = _pBoxCollider_RightCheck.transform.position.y + _pBoxCollider_RightCheck.offset.y + (_pBoxCollider_RightCheck.size.y / 2f); if (Physics2D.Raycast(vecRayPos, Vector2.right, _pBoxCollider_RightCheck.size.x, p_sWhatIsGround)) { Gizmos.color = Color.red; } else { Gizmos.color = Color.green; } Gizmos.DrawRay(vecRayPos, Vector2.right * _pBoxCollider_RightCheck.size.x); } float fPosYOffset = 1f; Vector3 vecPos = transform.position + new Vector3(1f, 1f); UnityEditor.Handles.Label(vecPos, "Movement State ---------------------------------------------"); vecPos.y -= fPosYOffset; UnityEditor.Handles.Label(vecPos, " _vecAddForce_Custom : " + _vecAddForce_Custom); vecPos.y -= fPosYOffset; UnityEditor.Handles.Label(vecPos, "_fMoveDelta_0_1 : " + _fMoveDelta_0_1 + " p_fMoveVelocity : " + p_fMoveVelocity); vecPos.y -= fPosYOffset; UnityEditor.Handles.Label(vecPos, "_pRigidbody.velocityPrev : " + _queueVelocityPrev.ToList().ToStringList()); vecPos.y -= fPosYOffset; UnityEditor.Handles.Label(vecPos, "_pRigidbody.velocity : " + _pRigidbody.velocity); vecPos.y -= fPosYOffset; UnityEditor.Handles.Label(vecPos, "_bInputAddForce_Custom : " + _bInputAddForce_Custom + " _vecAddForce_Custom : " + _vecAddForce_Custom); vecPos.y -= fPosYOffset * 2f; UnityEditor.Handles.Label(vecPos, "Platformer State ---------------------------------------------"); vecPos.y -= fPosYOffset; UnityEditor.Handles.Label(vecPos, "_ePlatformerState_Flags_Prev : " + p_ePlatformerState_Prev); vecPos.y -= fPosYOffset; UnityEditor.Handles.Label(vecPos, "_ePlatformerState_Flags_Current : " + p_ePlatformerState_Current); vecPos.y -= fPosYOffset * 2f; UnityEditor.Handles.Label(vecPos, "Platform Check State -----------------------------------------"); vecPos.y -= fPosYOffset; UnityEditor.Handles.Label(vecPos, "_bIsGround : " + p_bIsGround + " _bIsCrouch : " + p_bIsCrouch + " _bIsMoving : " + p_bIsMoving + " _bIsJumping : " + p_bIsJumping); vecPos.y -= fPosYOffset; UnityEditor.Handles.Label(vecPos, "_bCheckIsFalling : " + _bCheckIsFalling + " _bIsFalling : " + _bIsFalling); vecPos.y -= fPosYOffset; UnityEditor.Handles.Label(vecPos, "_bIsWallSliding : " + _bIsWallSliding + " _bLeftDirection_IsBlocked : " + p_bLeftDirection_IsBlocked + " _bRightDirection_IsBlocked : " + p_bRightDirection_IsBlocked); vecPos.y -= fPosYOffset * 2f; if (p_bUseSlopeSliding) { UnityEditor.Handles.Label(vecPos, "Slope Check State -----------------------------------------"); vecPos.y -= fPosYOffset; UnityEditor.Handles.Label(vecPos, "p_bIsSlopeSliding : " + p_bIsSlopeSliding + " _fSlopeAngle : " + _fSlopeAngle + " _fSlopeAngle_Signed : " + _fSlopeAngle_Signed + " _vecCurrentSlopeNormal : " + _vecCurrentSlopeNormal); vecPos.y -= fPosYOffset; } } }
void FixedUpdate() { if (destroyWhenOutOfView) { Vector3 screenPoint = Camera.main.WorldToViewportPoint(transform.position); bool onScreen = screenPoint.z > 0 && screenPoint.x > -0.01f && screenPoint.x <1 + 0.01f && screenPoint.y> -0.01f && screenPoint.y < 1 + 0.01f; if (!onScreen) { Destroy(this.gameObject); } } if (timeBeforeAutodestruct > 0f) { timer += Time.deltaTime; if (timer > timeBeforeAutodestruct) { Destroy(this.gameObject); } } if (alive && Physics2D.IsTouching(bulletCollider, contactFilter)) { Collider2D[] targets = new Collider2D[1]; Physics2D.GetContacts(bulletCollider, contactFilter, targets); if (targets.Length > 0) { Collider2D coll = targets[0]; switch (coll.tag) { case "Player": //Debug.Log("hit player"); break; case "Enemy": //Debug.Log("hit enemy"); HealthLife hl = coll.gameObject.GetComponent <HealthLife>(); if (hl != null) { hl.TakeHit(damage); } break; default: break; } } alive = false; bulletCollider.enabled = false; spriteRenderer.enabled = false; if (particles != null) { particles.Play(); } Invoke("DieForReal", 2f); } }
// Update is called once per frame void FixedUpdate() { Physics2D.GetContacts(LOSCollider, contactFilter, colliders); }
static public IEnumerable <ContactPoint2D> GetContacts(Collider2D collider1, Collider2D collider2) { return(CONTACT_POINT_POOL.UseEnumerateExpand(delegate(ContactPoint2D[] contacts) { return Physics2D.GetContacts(collider1, collider2, ContactFilter2DExtensions.NONE, contacts); })); }
void Update() { if (curEnergy <= 0f || energyTransferScale == 0f || !mTriggerBoxColl || (block && block.state == (int)EntityState.Dead)) { return; } float curTime = Time.time; if (curTime - mLastUpdateTime >= energyTransferDelay) { mLastUpdateTime = curTime; //setup energy to transfer bool isDeath = explodeOnOverCapacity && isCapacityReached; float energyTransfer = curEnergy * energyTransferScale; curEnergy -= energyTransfer; mReceivers.Clear(); int contactCount = Physics2D.GetContacts(mTriggerBoxColl, mCollContacts); if (contactCount > 0) { M8.EntityBase killEnt = null; for (int i = 0; i < contactCount; i++) { var coll = mCollContacts[i]; //kill? if (!string.IsNullOrEmpty(killTag) && !killEnt && coll.gameObject.CompareTag(killTag)) { killEnt = coll.GetComponent <M8.EntityBase>(); continue; } var conductive = coll.GetComponent <ConductiveController>(); //check criterias if (coll == mTriggerBoxColl) { continue; } if (conductive.energyReceiveScale == 0f) //cannot receive energy? { continue; } if (conductive.isCapacityReached) //capacity already reached { continue; } if (conductive.IsReceiver(this)) //are we already a receiver from this conductor? { continue; } mReceivers.Add(new Contact(coll, conductive)); } //distribute energy transfer if (mReceivers.Count > 0) { float energyPerReceiver = energyTransfer / mReceivers.Count; for (int i = 0; i < mReceivers.Count; i++) { var ctrl = mReceivers[i].ctrl; ctrl.curEnergy += energyPerReceiver; if (ctrl.receivedCallback != null) { ctrl.receivedCallback(this, energyPerReceiver); } } } //kill entity? if (killEnt) { //check if it is within kill bounds if (colliderBody) { Vector2 pos = transform.worldToLocalMatrix.MultiplyPoint3x4(killEnt.transform.position); Vector2 boxPos = mTriggerBoxColl.offset; Vector2 min = boxPos - mKillExt, max = boxPos + mKillExt; if (pos.x >= min.x && pos.x <= max.x && pos.y >= min.y && pos.y <= max.y) { killEnt.state = (int)EntityState.Dead; } } else { killEnt.state = (int)EntityState.Dead; } } } //die from capacity reached? if (isDeath && block && !block.isReleased) { block.state = (int)EntityState.Dead; } } }
protected override bool IsDetectedGround() { var count = Physics2D.GetContacts(collider, contactPoints); return(count > 0); }
// Update is called once per frame void FixedUpdate() { Physics2D.GetContacts(LosColliderp, Contactfilterp, collidep); }