bool isGrounded() { groundOverlap[0] = new Vector2(transform.position.x - 0.1f, transform.position.y - 0.15f); groundOverlap[1] = new Vector2(transform.position.x + 0.1f, transform.position.y - 0.2f); Instantiate(marker, new Vector3(groundOverlap[0].x, groundOverlap[0].y, 0), Quaternion.identity); Instantiate(marker, new Vector3(groundOverlap[1].x, groundOverlap[1].y, 0), Quaternion.identity); return(Physics2D.OverlapArea(groundOverlap[0], groundOverlap[1], LayerMask.NameToLayer("Player"))); }
public byte DetectCollisions(Vector2 position) { float _charSize = characterSize * 0.5f; Vector2 bottomLeftCorner = -Vector2.right * _charSize - Vector2.up * _charSize; //might need to be changed ************** Vector2 topRightCorner = Vector2.right * _charSize + Vector2.up * _charSize; //might need to be changed ************** return(Physics2D.OverlapArea(position + bottomLeftCorner, position + topRightCorner, wall) ? (byte)0 : (byte)1); //if overlap .. blocked == 0 //might need to be changed ************** }
private void isPlayerGrounded() { onGround = (Physics2D.OverlapArea(new Vector2(transform.position.x - collider.bounds.size.x / 2, transform.position.y - collider.bounds.size.y / 2), new Vector2(transform.position.x + collider.bounds.size.x / 2, transform.position.y - collider.bounds.size.y / 2), groundLayers) || Physics2D.OverlapArea(new Vector2(transform.position.x - collider.bounds.size.x / 2, transform.position.y - collider.bounds.size.y / 2), new Vector2(transform.position.x + collider.bounds.size.x / 2, transform.position.y - collider.bounds.size.y / 2), platformLayers)); }
private bool ObstructionAbove(float amountAbove) { Bounds bounds = boxCollider2d.bounds; Vector2 topLeft = new Vector2(bounds.center.x - bounds.extents.x, bounds.center.y + bounds.extents.y + amountAbove); Vector2 bottomRight = new Vector2(bounds.center.x + bounds.extents.x, bounds.center.y + bounds.extents.y); return(Physics2D.OverlapArea(topLeft, bottomRight, platformsLayerMask)); }
// -------------------------------------------------------------------- // GET COLLIDER2D // RETURN COLLIDER ON THE SAME LAYER // -------------------------------------------------------------------- public static Collider2D getCollider2DFromOverlapArea(Collider2D pCollider2D, LayerMask layerToDetect) { Vector2 pointA = new Vector2(pCollider2D.transform.position.x - pCollider2D.bounds.extents.x, pCollider2D.transform.position.y - pCollider2D.bounds.extents.y); Vector2 pointB = new Vector2(pCollider2D.transform.position.x + pCollider2D.bounds.extents.x, pCollider2D.transform.position.y + pCollider2D.bounds.extents.y); return(Physics2D.OverlapArea(pointA, pointB, layerToDetect)); }
private static GameObject GetPiece(float x, float y, BustBoard board) { (x, y) = RoundPosition(new Vector3(x, y)); var pointA = board.kludHolder.transform.TransformPoint(new Vector2(x + .25f, y - .25f)); var pointB = board.kludHolder.transform.TransformPoint(new Vector2(x + .75f, y - .75f)); return(Physics2D.OverlapArea(pointA, pointB, board.kludLayer)?.gameObject); }
bool IsFrontMovableObject() { mtopLeftPoint = new Vector2(transform.position.x + 0.2f, transform.position.y + 0.6f); mBottomRightPoint = new Vector2(transform.position.x + 0.9f, transform.position.y - 0.6f); var hit = Physics2D.OverlapArea(mtopLeftPoint, mBottomRightPoint, 1 << 10); return(hit != null); }
bool isTouchingCoin() { return(Physics2D.OverlapArea( new Vector2(transform.position.x - 0.5f, transform.position.y - 0.5f), new Vector2(transform.position.x + 0.5f, transform.position.y - 0.5f), coinLayer )); }
bool IsGrounded() { gtopLeftPoint = new Vector2(tr.position.x - 0.3f, tr.position.y - 0.6f); gBottomRightPoint = new Vector2(tr.position.x + 0.2f, tr.position.y - 0.8f); var hit = Physics2D.OverlapArea(gtopLeftPoint, gBottomRightPoint, 1 << 9); return(hit != null); }
private bool CanSlideLeft(float slideColliderHeight) { Bounds bounds = boxCollider2d.bounds; Vector2 topLeft = new Vector2(bounds.center.x - bounds.extents.x - 0.01f, bounds.center.y - bounds.extents.y + slideColliderHeight); Vector2 bottomRight = new Vector2(bounds.center.x - bounds.extents.x, bounds.center.y - bounds.extents.y + 0.01f); return(!Physics2D.OverlapArea(topLeft, bottomRight, platformsLayerMask)); }
bool isGrounded() { return(Physics2D.OverlapArea( new Vector2(transform.position.x - 0.5f, transform.position.y - 0.5f), new Vector2(transform.position.x + 0.5f, transform.position.y - 0.5f), groundLayer )); }
void FixedUpdate() { horizontalMovement = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime; isGrounded = Physics2D.OverlapArea(groundCheckLeft.position, groundCheckRight.position); MovePlayer(horizontalMovement); }
private bool IsOnButton() { return(Physics2D.OverlapArea(new Vector2(transform.position.x - collider.bounds.size.x / 2, transform.position.y - collider.bounds.size.y / 2), new Vector2(transform.position.x + collider.bounds.size.x / 2, transform.position.y - collider.bounds.size.y / 2), PlayerLayers) || Physics2D.OverlapArea(new Vector2(transform.position.x - collider.bounds.size.x / 2, transform.position.y - collider.bounds.size.y / 2), new Vector2(transform.position.x + collider.bounds.size.x / 2, transform.position.y - collider.bounds.size.y / 2), PlatformLayers)); }
void CheckForGround(Vector2 moveAmount) { Vector2 topLeftPoint = new Vector2((rc1.raycastOrigins.bottomLeft.x + rc1.skinWidth * 2) + moveAmount.x, rc1.raycastOrigins.bottomLeft.y - rc1.skinWidth + moveAmount.y); Vector2 bottomRightPoint = new Vector2((rc1.raycastOrigins.bottomRight.x - rc1.skinWidth * 2) + moveAmount.x, rc1.raycastOrigins.bottomRight.y - (rc1.skinWidth * 2) + moveAmount.y); genericController.collisions.grounded = Physics2D.OverlapArea(topLeftPoint, bottomRightPoint, collisionMask); Debug.DrawLine(topLeftPoint, bottomRightPoint, Color.green); }
private bool CheckIsUnderCeiling() { var colliderMax = new Vector2(coll.bounds.max.x - 1f, coll.bounds.max.y + 0.5f + (coll as BoxCollider2D).edgeRadius); var colliderMin = new Vector2(coll.bounds.min.x + 1f, coll.bounds.max.y - 0.5f + (coll as BoxCollider2D).edgeRadius); var coll2d = Physics2D.OverlapArea(colliderMin, colliderMax, LayerMask.GetMask("Ground")); return(coll2d != null); }
private void FixedUpdate() { isGrounded = Physics2D.OverlapArea(topLeft.position, bottomRight.position, whatIsGround); moveInput = Input.GetAxis("Horizontal"); body.velocity = new Vector2(moveInput * speed, body.velocity.y); FlipHandler(); }
// Update is called once per frame void FixedUpdate() { Vector2 groundCheck = new Vector2(transform.position.x, transform.position.y - (1.5f * transform.lossyScale.y)); Vector2 groundArea = new Vector2(_col2D.size.x / 2.2f, 0.05f); isGround = Physics2D.OverlapArea(groundCheck + groundArea, groundCheck - groundArea, 1 << LayerMask.NameToLayer("Stage")); Move(Input.GetAxis("Horizontal"), Input.GetButtonDown("Jump")); }
public void Move(Vector2 worldMovement) { Rect newRect = new Rect(collider2D.bounds.min.x + groundCheckMargin, renderer.bounds.min.y, collider2D.bounds.size.x - (groundCheckMargin * 2), -groundCheckHeight); isGrounded = Physics2D.OverlapArea(newRect.min, newRect.max, groundLayer); rigidbody2D.MovePosition((Vector2)transform.position + worldMovement); }
public bool isAuSol() { return(auSol = Physics2D.OverlapArea( new Vector2(transform.position.x - this.distCalc, transform.position.y - this.distCalc), new Vector2(transform.position.x, transform.position.y), terrain )); }
private void GroundCheck() { collCenter = new Vector2(transform.position.x + coll.offset.x, transform.position.y + coll.offset.y); isGrounded = Physics2D.OverlapArea(new Vector2(collCenter.x - (xBound - groundedSideShell), collCenter.y - yBound), new Vector2(collCenter.x + (xBound - groundedSideShell), collCenter.y - (yBound + groundedCheckDepth)), ground); }
protected Collider2D ifCollision(LayerMask mask) { BoxCollider2D col = gameObject.GetComponent <BoxCollider2D> (); return(Physics2D.OverlapArea( new Vector2(position.x - col.size.x / 2f, position.y + col.size.y / 2f + col.offset.y), new Vector2(position.x + col.size.x / 2f, position.y - col.size.y / 2f + col.offset.y), mask)); }
// Update is called once per frame void Update() { // Grounded Test Collider2D c = Physics2D.OverlapArea(Utils.vec3To2(groundDetector1.position), Utils.vec3To2(groundDetector2.position)); isGrounded = c != null && c.tag != "Player"; animator.SetBool("grounded", isGrounded); }
//checks for a player in line with box public bool inLine(int x, int y) { if (Physics2D.OverlapArea(transform.position, transform.position - new Vector3(x * 20, y * 20, 0), player)) { return(true); } return(false); }
// Update is called once per frame void Update() { grounded = Physics2D.OverlapArea(groundCheckTop_Left.position, groundCheckBottomRight.position, groundLayers); if (GetKeyPress(KeyCode.Escape)) { Quit(); } if (noUpdate) { return; } mana -= manaDecay * Time.deltaTime / (disabledKeys.Count + 1); if (mana <= 0 || transform.position.y < -1.0f) { GameOver(); manaBar.transform.localScale = new Vector3(0.0f, 1.0f, 1.0f); } else { if (GetKeyPress(KeyCode.Space)) { Jump(); } if (GetKeyPress(KeyCode.E)) { ToggleMenu(); } if (GetKeyDown(KeyCode.W)) { Float(); } else { floating.Stop(); } if (GetKeyPress(KeyCode.S)) { Slam(); } if (GetKeyDown(KeyCode.D)) { MoveForward(); } if (GetKeyDown(KeyCode.A)) { MoveBackward(); } manaBar.transform.localScale = new Vector3(mana / maxMana, 1.0f, 1.0f); } }
void Update() { if (isDead == true) { if (Input.GetButtonDown("Restart")) { SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); } return; } if (!grounded && Input.GetAxisRaw("Horizontal") != previousAxispos) { BaseSpeed = 0; outsideForce = false; } previousAxispos = Input.GetAxisRaw("Horizontal"); //MOVING CODE anim.SetFloat("velocityY", rb.velocity.y); if (!outsideForce && hanging == false) { if (Input.GetAxisRaw("Horizontal") != 0) { anim.SetBool("Moving", true); rb.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * speed + BaseSpeed, rb.velocity.y); transform.localScale = new Vector3(Input.GetAxisRaw("Horizontal") * 1, 1, 1); } else { anim.SetBool("Moving", false); rb.velocity = new Vector2(BaseSpeed, rb.velocity.y); } } //JUMPCODE grounded = Physics2D.OverlapArea(point1.position, point2.position, onlyGroundMask); anim.SetBool("Grounded", grounded); if ((grounded || hanging) && Input.GetButtonDown("Jump")) { hanging = false; GetComponent <Rigidbody2D> ().isKinematic = false; timer = 0; canJump = true; rb.AddForce(new Vector2(0, jumpForce * 3)); } }
// Update is called once per frame void Update() { grounded = Physics2D.OverlapArea(new Vector2(transform.position.x - 0.5f, transform.position.y - 1.7f), new Vector2(transform.position.x - 0.5f, transform.position.y - 0.6f), groundLayer); anim.SetBool("onGround", grounded); float move = Input.GetAxis("Horizontal"); pBody.velocity = new Vector2(move * moveSpeed, pBody.velocity.y); float tempY = pBody.velocity.y; if (pBody.velocity.magnitude > maxSpeed) { pBody.velocity = pBody.velocity.normalized * maxSpeed; } pBody.velocity = new Vector2(pBody.velocity.x, tempY); anim.SetFloat("Speed", Mathf.Abs(pBody.velocity.x)); var localVelocity = transform.InverseTransformDirection(pBody.velocity); if (localVelocity.x < 0) { facingRight = false; pSprite.flipX = true; } else if (localVelocity.x > 0) { facingRight = true; pSprite.flipX = false; } if (Input.GetButtonDown("Jump") && grounded) { pBody.velocity = new Vector2(pBody.velocity.x, jumpPower); } if (Input.GetButtonDown("Fire2") && !isRolling) { isRolling = true; anim.SetBool("isRolling", true); DodgeRoll(); } if (isRolling) { if (facingRight) { pBody.velocity = new Vector2(rollSpeed, -rollSpeed / 2); } else { pBody.velocity = new Vector2(-rollSpeed, -rollSpeed / 2); } } }
// Update is called once per frame void Update() { Vector3 max = _box.bounds.max; Vector3 min = _box.bounds.min; Vector2 corner1 = new Vector2(max.x, min.y - 0.1f); Vector2 corner2 = new Vector2(min.x, min.y - 0.2f); Collider2D hit = Physics2D.OverlapArea(corner1, corner2); Vector3 bombStartPos = new Vector3(Random.Range(50.0f, 80.0f), Random.Range(-1.5f, 2.5f), 0); //_anim.SetFloat("speed", 0); bool grounded = false; if (hit != null) { grounded = true; } if (Input.GetKeyDown(KeyCode.Space)) { if (grounded) { _body.AddForce(Vector2.up * 350); } } if (timer <= 2.1) { timer += Time.deltaTime; } if (Input.GetKeyDown("z") && bombCollected == true) { timer = 0; Debug.Log("Z pressed"); bombCollected = false; spawnObstacles = false; bomb.SetActive(true); } if (timer >= 2) { spawnObstacles = true; } if (bombCollected == false) { UI.SetActive(false); zButton.SetActive(false); } else { UI.SetActive(true); zButton.SetActive(true); bomb.SetActive(false); bomb.transform.position = bombStartPos; } }
void Update() { float deltaX = Input.GetAxis("Horizontal") * speed * Time.deltaTime; Vector2 movement = new Vector2(deltaX, _body.velocity.y); _body.velocity = movement; Vector3 max = _box.bounds.max; Vector3 min = _box.bounds.min; Vector2 corner1 = new Vector2(max.x, min.y - .1f); Vector2 corner2 = new Vector2(min.x, min.y - .2f); Collider2D hit = Physics2D.OverlapArea(corner1, corner2); bool grounded = false; if (hit != null) { grounded = true; } _body.gravityScale = grounded && deltaX == 0 ? 0 : 1; if (grounded && Input.GetKeyDown(KeyCode.Space)) { _body.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse); } MovingPlatform platform = null; if (hit != null) { platform = hit.GetComponent <MovingPlatform>(); } if (platform != null) { transform.parent = platform.transform; } else { transform.parent = null; } _anim.SetFloat("speed", Mathf.Abs(deltaX)); Vector3 pScale = Vector3.one; if (platform != null) { pScale = platform.transform.localScale; } if (deltaX != 0) { transform.localScale = new Vector3(Mathf.Sign(deltaX) / pScale.x, 1 / pScale.y, 1); } if (!Mathf.Approximately(deltaX, 0)) { transform.localScale = new Vector3(Mathf.Sign(deltaX), 1, 1); } }
// Update is called once per frame void Update() { if (enemyStats.Health <= 0) { Destroy(this.gameObject); } playerRb2d = GameObject.FindGameObjectWithTag("Player").GetComponent <Rigidbody2D>(); isGrounded = Physics2D.OverlapArea(groundCheck1.position, groundCheck2.position, groundMask); Collider2D[] collider = Physics2D.OverlapCircleAll(transform.position, maxRange); foreach (Collider2D col in collider) { if (col.tag == "Player") { player = GameObject.FindGameObjectWithTag("Player").GetComponent <Transform>(); heading = transform.position - player.position; distance = heading.x; if (distanceFromHome < maxDistanceFromHome) { StartCoroutine("Attack"); } } } Vector2 vectorBeetwenHome = transform.position - homePosition.position; distanceFromHome = vectorBeetwenHome.magnitude; if (!isCourotinePlay) { if (isGrounded) { if (distanceFromHome >= maxDistanceFromHome / 4) { transform.Translate(Vector2.right * -Mathf.Sign(vectorBeetwenHome.x) * returnSpeed * Time.deltaTime); } } } if (!isGrounded) { if (rb2d.velocity.y <= 0.5f) { rb2d.gravityScale = 6; } else { rb2d.gravityScale = oldGravityScale; } transform.Translate(Vector2.right * -heading * 2 * Time.deltaTime); } }
void FixedUpdate() { Vector2 pos = transform.position; Vector2 groundCheck = new Vector2(pos.x, pos.y - (m_centerY * transform.localScale.y)); Vector2 groundArea = new Vector2(m_CapsuleCollider2D.size.x * 0.49f, 0.026f); m_isGround = Physics2D.OverlapArea(groundCheck + groundArea, groundCheck - groundArea, whatIsGround); m_animator.SetBool("isGround", m_isGround); }