private void CheckIfTouchingValidGround() { colliderGround = Physics2D.OverlapCapsule(isTouchingGroundedChecker.position, capsuleSize, CapsuleDirection2D.Horizontal, 0, groundLayer); colliderWallLeft = Physics2D.OverlapCircle(isTouchingWallLeftChecker.position, checkGroundRadius, groundLayer); colliderWallRight = Physics2D.OverlapCircle(isTouchingWallRightChecker.position, checkGroundRadius, groundLayer); if (colliderGround != null) { isGrounded = true; } else { isGrounded = false; } if (colliderWallLeft != null) { isTouchingLeftWall = true; } else { isTouchingLeftWall = false; } if (colliderWallRight != null) { isTouchingRightWall = true; } else { isTouchingRightWall = false; } }
public bool IsGrounded() { float extraHeight = 0.1f; Collider2D col = Physics2D.OverlapCapsule(new Vector2(MainCollider.transform.position.x, MainCollider.transform.position.y - extraHeight), MainCollider.size, CapsuleDirection2D.Vertical, 0, PlatformLayer); return(col != null); }
// return the resulting ground gameobject private GroundedStates GroundedCheck() { // perform a capsule overlap of the same size as the player's collider, but slightly below the player Vector2 capsuleOverlapPoint = new Vector2(coll2D.bounds.center.x, coll2D.bounds.center.y - groundedCheckOffset); Collider2D result = Physics2D.OverlapCapsule(capsuleOverlapPoint, coll2D.size, coll2D.direction, 0, LayerMask.GetMask("World")); if (result != null) { if (result.gameObject.CompareTag("JumpBoost")) { return(GroundedStates.BoostGround); } else if (result.gameObject.CompareTag("NoJump")) { return(GroundedStates.None); } else { return(GroundedStates.Ground); } } else { return(GroundedStates.None); } }
private void CheckIfTouchingDeathGround() { colliderGround = Physics2D.OverlapCapsule(isTouchingGroundedChecker.position, capsuleSize, CapsuleDirection2D.Horizontal, 0, deathLayer); colliderWallLeft = Physics2D.OverlapCircle(isTouchingWallLeftChecker.position, checkGroundRadius, deathLayer); colliderWallRight = Physics2D.OverlapCircle(isTouchingWallRightChecker.position, checkGroundRadius, deathLayer); colliderCrushed = Physics2D.OverlapCircle(isCrushedChecker.position, checkGroundRadius, groundLayer); if ((colliderGround != null) || (colliderWallLeft != null) || (colliderWallRight != null) || (colliderCrushed != null)) { //Debug.Log(rigidbody2DPlayer.transform.position.ToString()); Vector2 temp = spawnPoint.transform.position; rigidbody2DPlayer.transform.position = temp; rigidbody2DPlayer.velocity = Vector2.zero; deathFlag = true; } else if (deathFlag) { deathFlag = false; deaths++; PlayerPrefs.SetInt("Deaths", PlayerPrefs.GetInt("Deaths", 0) + 1); //Debug.Log("Deaths: " + deaths.ToString()); deathsText.text = Convert.ToString(deaths, 2); audioSource.PlayOneShot(audioDeath, audioVolume * (audioVolumeGlobal / 100.0f)); //Debug.Log(rigidbody2DPlayer.position.ToString()); } }
public int AttackDir(Vector2 dir) { if (!IsCanDamage || nextDamageTimer > 0) { return(0); } nextDamageTimer = damageRate; m_DamagerTransform = (Vector2)transform.position + size / 2 * dir; angle = Vector2.SignedAngle(Vector2.right, dir); int hitCount = Physics2D.OverlapCapsule(m_DamagerTransform, size, CapsuleDirection2D.Horizontal, angle, m_AttackContactFilter, m_AttackOverlapResults); //Debug.Log("Damage : " + m_DamagerTransform + "; angle" + angle + " , hitCount : " + hitCount); for (int i = 0; i < hitCount; i++) { m_LastHit = m_AttackOverlapResults[i]; Damageable damageable = m_LastHit.GetComponentInParent <Damageable>(); if (damageable) { damageable.TakeDamage(this, ignoreInvincibility); //OnDamageableHit.Invoke(this, damageable); //if (disableDamageAfterHit) // DisableDamage(); } } //OnNonDamageableHit.Invoke(this); return(hitCount); }
private void FixedUpdate() { ForceX = Mathf.Abs(ForceX) < 4 ? 0 : Mathf.Sign(ForceX) * (Mathf.Abs(ForceX) - gravityScale * windFriction / 2); ForceY = Mathf.Abs(ForceY) < 4 ? 0 : Mathf.Sign(ForceY) * (Mathf.Abs(ForceY) - gravityScale * windFriction); grounded = Physics2D.OverlapCapsule(groundCheck.position, new Vector2(0.5f, 1.5f), CapsuleDirection2D.Vertical, 0, whatIsGround); bool IsSlope = Physics2D.OverlapCapsule(groundCheck.position, new Vector2(0.5f, 1.5f), CapsuleDirection2D.Vertical, 0, whatIsSlope); animator.SetBool("IsSlope", IsSlope); if (playerAction == PlayerAction.ClimbingUp) { rgdb.velocity = new Vector2(ClimbingVelocityX, ClimbingVelocityY); } else if (playerAction == PlayerAction.HangUp) { rgdb.velocity = new Vector2(ForceX, move.y * ropeSpeed); } else { rgdb.velocity = new Vector2(move.x * maxSpeed + ForceX, rgdb.velocity.y + ForceY); } //If it is hanged on the rope and has no energy, the player falls if (energyController.GetCurrentEnergy() <= energyController.GetEnergyRecoverySpeed()) { animator.Play("Falling"); playerAction = PlayerAction.Falling; setNeutralState(); } }
private void Update() { var pointPlayer = new Vector2(transform.position.x, transform.position.y); var sizePlayer = new Vector2(0.9f, 5.4f);//the size of the capsule collider of the player. This is not perfect. //bool isOverlappingFrontLayer = Physics2D.OverlapCapsule(playerPosition, playerHeight, CapsuleDirection2D.Vertical, playerEulerAngleZ, frontLayerMask) != null; bool isOverlappingFrontLayer = Physics2D.OverlapCapsule(pointPlayer, sizePlayer, CapsuleDirection2D.Vertical, 0, frontLayer) != null; bool isOverlappingBackLayer = Physics2D.OverlapCapsule(pointPlayer, sizePlayer, CapsuleDirection2D.Vertical, 0, backLayer) != null; //Change plane if (Input.GetButtonDown("ChangePlane")) { if (!isOverlappingFrontLayer && !isOverlappingBackLayer) { SwitchAvatar(); } //SwitchAvatar(); } //if the position change is in the switch() it does not work properly if (whichAvatarIsOn == 2) { transform.position = new Vector3(transform.position.x, transform.position.y, 1); } else { transform.position = new Vector3(transform.position.x, transform.position.y, -1); } }
// Update is called once per frame void Update() { if (poisonCheck) { --poison; if (poison <= 0) { poison = 15; --PlayerStats.Health; } } if (PlayerStats.Health <= 0) { SceneManager.LoadScene(0); } Playermove(); isGrounded = Physics2D.OverlapCapsule(groundCheck.position, new Vector2(1f, 1f), CapsuleDirection2D.Horizontal, 0, groundMask); if (SceneManager.GetActiveScene().name == "level1") { gameObject.SetActive(true); } else if (SceneManager.GetActiveScene().name == "RPGScene") { gameObject.SetActive(false); } }
private bool IsGrounded() { float extraHeight = 0.2f; Collider2D col = Physics2D.OverlapCapsule(new Vector2(MainCollider.transform.position.x, MainCollider.transform.position.y - extraHeight), new Vector2(MainCollider.size.x - 0.2f, MainCollider.size.y), CapsuleDirection2D.Vertical, 0, worldLayer); return(col != null); }
private void AttackSlash() { Collider2D hit = Physics2D.OverlapCapsule(slashPos.position, slashRange, CapsuleDirection2D.Horizontal, slashAngle, playerMask); if (hit) { object[] package = new object[1]; package[0] = DamageSlash; hit.SendMessage("TakeDamage", package); } }
private bool CheckSideCollision() { Collider2D colliderCheck = Physics2D.OverlapCapsule(coll.bounds.center, new Vector2(coll.size.x * wallJumpDetectionSizeMod, coll.size.y * 0.9f), CapsuleDirection2D.Vertical, 0f, jumpMask); if (colliderCheck) { return(true); } else { return(false); } }
public Collider2D CanDropThrough() { Collider2D colliderCheck = Physics2D.OverlapCapsule(coll.bounds.center + Vector3.down * jumpCastOffset, new Vector2(coll.size.x * 0.9f, coll.size.y * 0.9f), CapsuleDirection2D.Vertical, 0f, jumpMask); if (!falling && !frozen && colliderCheck && colliderCheck.CompareTag("DropThrough")) { if (currentState == State.Idle || currentState == State.Walk) { return(colliderCheck); } } return(null); }
bool CheckGrounded() { Collider2D colliderCheck = Physics2D.OverlapCapsule(coll.bounds.center + Vector3.down * 0.1f, new Vector2(coll.size.x * 0.9f, coll.size.y * 0.9f), CapsuleDirection2D.Vertical, 0f, groundMask); if (colliderCheck) { return(true); } else { return(false); } }
private bool CheckGrounded() { Collider2D colliderCheck = Physics2D.OverlapCapsule(coll.bounds.center + Vector3.down * jumpCastOffset, new Vector2(coll.size.x * 0.9f, coll.size.y * 0.9f), CapsuleDirection2D.Vertical, 0f, jumpMask); if (colliderCheck) { return(true); } else { return(false); } //return Physics.CheckCapsule (coll.bounds.center, new Vector3 (coll.bounds.center.x, coll.bounds.min.y, coll.bounds.center.z), coll.bounds.extents.x - 1f, jumpMask); }
/// <summary> /// Fires the laser towards the enemy and deals damage /// </summary> // TODO - Animate the laser slightly (make it pulse) protected override void Attack() { Vector3 triangleCentre = transform.position; // Get the end point of the line renderer Vector3 direction = (firePoint.up * range.GetStat()); Vector3 endPosition = (direction + triangleCentre); // Get all enemies the laser hits var results = new List <Collider2D>(); Physics2D.OverlapCapsule(direction / 2 + triangleCentre, new Vector2(range.GetStat(), lineRenderer.endWidth), CapsuleDirection2D.Vertical, transform.rotation.y, new ContactFilter2D().NoFilter(), results); List <Enemy> enemies = results.Select(result => result.transform.GetComponent <Enemy>()).ToList(); enemies.RemoveAll(x => x == null); // Deal damage to every enemy hit foreach (Enemy enemy in enemies) { enemy.TakeDamage(damage.GetStat() * Time.deltaTime, gameObject); } foreach (Module module in modules) { module.OnHit(enemies, this); } // Enable visuals if (!lineRenderer.enabled) { lineRenderer.enabled = true; //impactEffect.Play(); } // Set Laser positions Vector3 firePointPosition = transform.position; lineRenderer.SetPosition(0, firePointPosition); lineRenderer.SetPosition(1, endPosition); // Set impact effect rotation Transform impactEffectTransform = impactEffect.transform; var aimDir = (Vector3)((Vector2)firePointPosition - (Vector2)impactEffectTransform.position).normalized; impactEffectTransform.rotation = Quaternion.LookRotation(aimDir); // Set impact effect position impactEffectTransform.position = endPosition + aimDir * 0.2f; }
void Update() // Update is called once per frame { move = Input.GetAxis("Horizontal"); jump = Input.GetButtonDown("Jump"); onGround = Physics2D.OverlapCapsule((Vector2)transform.position + ground_collision, radius, groundLayer); if (onGround && jump) { rb2d.velocity = new Vector2(move * speed, jumpForce); } else { rb2d.velocity = new Vector2(move * speed, rb2d.velocity.y); } }
/// <summary> /// 计算闪烁位置 /// </summary> public float calFlashDistance() { Vector2 flashVec = RuntimeCharacter.dir82Vec(direction); //闪烁方向 Vector2 dropPos = collCenter + flashVec * flashDistance; //落点 Vector2 colliderSize = new Vector2(collider.bounds.size.x - 0.02f, collider.bounds.size.y - 0.02f); //微调碰撞盒 Collider2D collHard = Physics2D.OverlapCapsule(dropPos, colliderSize, CapsuleDirection2D.Horizontal, 0f, (1 << 11) | (1 << 4) | (1 << 15)); //落点碰撞判断 RaycastHit2D raycastHit2D = Physics2D.Raycast(pos, flashVec, flashDistance, (1 << 11) | (1 << 4) | (1 << 15)); Collider2D collSoft = Physics2D.OverlapCapsule(dropPos, colliderSize, CapsuleDirection2D.Horizontal, 0f, (1 << 8));//落点碰撞判断 float flashDistStep = 0.05f; float flashDistRes = 0.0f; if (raycastHit2D || collSoft) { //寻找最远落点 while (flashDistStep <= flashDistance) { collHard = Physics2D.OverlapCapsule(collCenter + flashVec * flashDistStep, colliderSize, CapsuleDirection2D.Horizontal, 0f, (1 << 11) | (1 << 4) | (1 << 15)); collSoft = Physics2D.OverlapCapsule(collCenter + flashVec * flashDistStep, colliderSize, CapsuleDirection2D.Horizontal, 0f, (1 << 8));//落点碰撞判断 debugLog(collHard?.name); debugLog(collSoft?.name); if (collHard) { break; } if (!collSoft || collSoft.isTrigger) { flashDistRes = flashDistStep; } flashDistStep += 0.05f; } return(flashDistRes); } //直接闪烁到最远距离 else { return(flashDistance); } }
public bool CheckPenetrate(float reduceOfs, LayerMask mask) { if (mCapsuleColl) { Transform collT = transform; Vector2 collPos = collT.position + collT.localToWorldMatrix.MultiplyPoint3x4(mCapsuleColl.offset); Vector2 collSize = mCapsuleColl.size; collSize.y -= reduceOfs; return(Physics2D.OverlapCapsule(collPos, collSize, mCapsuleColl.direction, collT.eulerAngles.z, mask) != null); } else { return(Physics2D.OverlapCircle(transform.position, mRadius - reduceOfs, mask) != null); } }
private void DropDown() { Collider2D colliderCheck = Physics2D.OverlapCapsule(coll.bounds.center + Vector3.down * jumpCastOffset, new Vector2(coll.size.x * 0.9f, coll.size.y * 0.9f), CapsuleDirection2D.Vertical, 0f, jumpMask); if (!falling && !frozen && colliderCheck && colliderCheck.CompareTag("DropThrough")) { if (currentState == State.Idle || currentState == State.Walk) { TriggerFall(); Physics2D.IgnoreCollision(coll, colliderCheck); //Debug.Log ("Collision disabled"); StartCoroutine(RestoreCollision(colliderCheck)); } } }
// được gọi bơi Attack animation private void Attack() { if (eData.CurrentHealth <= 0) { return; } Collider2D hit = Physics2D.OverlapCapsule(transform.position + AttackOffSet, AttackRange, CapsuleDirection2D.Vertical, 0, playerMask); if (hit) { object[] package = new object[1]; package[0] = AttackDamage; hit.SendMessage("TakeDamage", package); } }
public void UpdateBashCommon() { // RAMP VELOCITY TO ZERO //if (vel.sqrMagnitude > 0.5f) { // vel *= bashSpeedFalloffMult; //} else { // vel = Vector2.zero; //} // FIND TARGET var bashTar = Physics2D.OverlapCapsule(super.body.position + bashCollider.offset, bashCollider.size, bashCollider.direction, 0, ~(1 << 8) /*player*/); bashTarget = (bashTar && bashTar.tag == "Projectile") ? bashTar.transform : null; // COOLDOWN if (bashCooldown > 0) { bashCooldown -= Time.fixedDeltaTime; } if (bashTarget) { // MOVE ARROW bashArrow.position = bashTarget.position; bashArrow.localRotation = Quaternion.Euler(0, 0, bashAngle); // START BASH if (InputWrapper.GetBash() && !bashInit && bashCooldown <= 0) { bashInit = true; super.mode = MoveMode.BASH; bashTimeStarted = Time.unscaledTime; } } var isBash = super.mode == MoveMode.BASH; if (!isBash) { bashInit = false; } bashArrow.gameObject.SetActive(isBash); // SET BULLET TIME Time.timeScale = isBash ? 0.01f : 1; Time.fixedDeltaTime = Time.timeScale * super.initFixedDeltaTimescale; }
void CheckCollision() { Vector2 capPos = new Vector2(transform.position.x, transform.position.y) + coll.cap.offset; Vector2 capSize = new Vector2(1.1f, 2.1f); coll.standing = Physics2D.OverlapCapsule(capPos, coll.cap.size, coll.cap.direction, 0f, coll.groundLayer); if (!coll.standing) { coll.onWall = Physics2D.OverlapCapsule(capPos, capSize, coll.cap.direction, 0f, coll.groundLayer); } else { coll.onWall = false; } }
// Update is called once per frame void Update() { pointPlayer = new Vector2(player.transform.position.x, player.transform.position.y); sizePlayer = new Vector2(1.5f, 5.5f);//the size of the capsule collider of the player //bool isOverlappingFrontLayer = Physics2D.OverlapCapsule(playerPosition, playerHeight, CapsuleDirection2D.Vertical, playerEulerAngleZ, frontLayerMask) != null; bool isOverlappingFrontLayer = Physics2D.OverlapCapsule(pointPlayer, sizePlayer, CapsuleDirection2D.Vertical, 0, frontLayer) != null; Debug.Log(isOverlappingFrontLayer); /* if (isOverlappingFrontLayer) * { * // capsule is overlapping front layer * //Debug.Log("overlaping with frontLayer"); * }*/ }
//------------------------------------------------------------------ //------------------------------------------------------------------ void FixedUpdate() { Vector2 capPos = new Vector2(transform.position.x, transform.position.y) + cap.offset; Vector2 capSize = new Vector2(1f, 2f); standing = Physics2D.OverlapCapsule(capPos, cap.size, cap.direction, 0f, groundLayer); if (!standing) { onWall = Physics2D.OverlapCapsule(capPos, capSize, cap.direction, 0f, groundLayer); } else { onWall = false; } }
// được gọi bơi Attack animation private void Attack() { if (eData.CurrentHealth <= 0) { return; } Collider2D hit = Physics2D.OverlapCapsule(attackPos.transform.position, new Vector2(AttackRange.x * facingDirection, AttackRange.y), CapsuleDirection2D.Horizontal, 0, playerMask); if (hit) { object[] package = new object[2]; package[0] = AttackDamage; package[1] = facingDirection; hit.SendMessage("TakeDamage", package); } }
void Update() { isGrounded = Physics2D.OverlapCircle(feetPos.position, feetCheckRadius, whatIsGround); isHitting = Physics2D.OverlapCapsule(facePos.position, new Vector2(faceCheckX, faceCheckY), CapsuleDirection2D.Vertical, 0, whatIsGround); isWinning = Physics2D.OverlapCapsule(facePos.position, new Vector2(faceCheckX, faceCheckY), CapsuleDirection2D.Vertical, 0, whatIsWin); if ((Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) && isGrounded == true) { isJumping = true; jumpTimeCounter = jumpTime; rb.velocity = Vector2.up * jumpForce; } if ((Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0)) && isJumping == true) { if (jumpTimeCounter > 0) { rb.velocity = Vector2.up * jumpForce; jumpTimeCounter -= Time.deltaTime; } else { isJumping = false; } } if (Input.GetKeyUp(KeyCode.Space) || Input.GetMouseButtonUp(0)) { isJumping = false; } if (isHitting) { highScoreText.GetComponent <HighScoreCounter>().checkForHighScore(rb); FindObjectOfType <AudioManager>().Play("Death" + Random.Range(1, 5).ToString()); SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); } else if (isWinning) { winGame.SetActive(true); FindObjectOfType <AudioManager>().Play("Win2"); winPoint.SetActive(false); } }
private void FixedUpdate() { _rigidbody.velocity = new Vector2(_horizontalInput * _speed, _rigidbody.velocity.y); if (_horizontalInput < 0) { _sprite.flipX = true; } else if (_horizontalInput > 0) { _sprite.flipX = false; } _animator.SetFloat("Speed", _rigidbody.velocity.magnitude); _grounded = Physics2D.OverlapCapsule(_collider.bounds.center, _collider.size * 1.1f, direction: CapsuleDirection2D.Vertical, 0, _groundMask) != null; }
private bool GetValidSpot(out Vector2 validSpot) { Bounds wbounds = MainScript.instance.worldBounds.bounds; for (int i = 0; i < VALIDSPOT_ATTEMPTS_MAX; i++) { Vector2 spotInMap = new Vector2(Random.Range(-wbounds.extents.x, wbounds.extents.x), Random.Range(-wbounds.extents.y, wbounds.extents.y)) + (Vector2)wbounds.center; if (!Physics2D.OverlapCapsule(spotInMap, new Vector2(2, 2), CapsuleDirection2D.Vertical, 0, LayerMask.GetMask("Unit", "Ground"))) { validSpot = spotInMap; return(true); } } validSpot = new Vector2(); return(false); }
private void FixedUpdate() { _coolDownTimer -= Time.deltaTime; if (_coolDownTimer > 0) { return; } var player = Physics2D.OverlapCapsule(transform.position, new Vector2(1.5f, 11f), CapsuleDirection2D.Vertical, 0, PlayerLayer); if (player != null) { Instantiate(ShotPrefab, transform.position, Quaternion.identity).GetComponent <Rigidbody2D>(); _coolDownTimer = _shootCoolDown; } }
static bool CheckPos(Vector2 pos) { if (Physics2D.OverlapCapsule(pos, new Vector2(COLX, COLY), CapsuleDirection2D.Horizontal, 0F, mask)) { return(false); } Vector2 playerPos = GameObject.Find("Player").transform.position; if ((pos - playerPos).magnitude < 5F) { return(false); } if ((pos - playerPos).magnitude > 13F) { return(false); } return(true); }