private void FixedUpdate() { // 地面との交差判定 Vector2 point = groundChecker.transform.position; point += groundChecker.offset; var result = Physics2D.OverlapBox( point, groundChecker.size, groundChecker.transform.rotation.eulerAngles.z, groundLayer); // 接地している状態 if (result) { isFloating = false; } // 接地していない状態 else { isFloating = true; } }
void FixedUpdate() { playerNear = Physics2D.OverlapBox(playerCheck.position, playerCheckSize, 0f, whatIsPlayer); inWater = Physics2D.OverlapCircle(waterCheck.position, waterCheckRadius, whatIsWater); Vector3 currentpos = transform.position; if (playerNear && inWater) { rb.GetComponent <Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic; Vector2 direction = (Vector2)target.position - rb.position; direction.Normalize(); float rotateAmount = Vector3.Cross(direction, transform.right).z; rb.angularVelocity = -rotateAmount * rotateSpeed; rb.velocity = transform.right * speed; } else { rb.transform.position = Vector3.MoveTowards(currentpos, startPos, .1f); //rb.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static; } }
// Update is called once per frame void Update() { if (targetHeight > transform.position.y) { Collider2D memoryCollider = Physics2D.OverlapBox(new Vector2(transform.position.x, targetHeight), new Vector2(0.25f, 0.25f), 0f, memSpaceLayer); MemorySpace memSpace = memoryCollider.GetComponent <MemorySpace>(); memSpace.Unlock(); memSpace.OccupySpace(value); Collider2D playerCollider = Physics2D.OverlapBox(new Vector2(transform.position.x, targetHeight), new Vector2(0.25f, 0.25f), 0f, playerLayer); if (playerCollider != null) { GameManager gameManager = GameObject.FindObjectOfType <GameManager>(); gameManager.RegisterDeath(); print("Player Killed"); } Instantiate(particles, transform.position - (0.5f * Vector3.up), Quaternion.Euler(-10, 90, 0)); Instantiate(particles, transform.position - (0.5f * Vector3.up), Quaternion.Euler(-10, -90, 0)); Destroy(gameObject); } }
private void FixedUpdate() { //wallHit is a bool similar to the ground one in the player code. //Physics2D.OverlapBox is similar to Physics2D.OverlapCircle but uses a box //The next is a Vector 2 with the box's Width and Height which are floats that I made public so I could edit them in the editor. //The zero is the z value we don't need. //isGround is a LayerMask of everything that is ground. wallHit = Physics2D.OverlapBox(wallHitbox.position, new Vector2(wallHitWidth, wallHitHeight), 0, isGround); if (wallHit == true) { speed = speed * -1; } headHit = Physics2D.OverlapBox(headHitbox.position, new Vector2(headHitWidth, headHitHeight), 0, isPlayer); if (headHit == true) { gameObject.SetActive(false); float vol = Random.Range(volLowRange, volHighRange); source.PlayOneShot(stompClip); } }
private void BaseBuildingInput() { if (Input.GetMouseButtonDown(0)) { Vector2 position = mainCamera.ScreenToWorldPoint(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(origin: position, direction: Vector2.down, distance: Mathf.Infinity, layerMask: groundMask); if (hit.transform != null) { Collider2D col = Physics2D.OverlapBox(hit.point, Vector2.one, 0); if (col.transform == null) { chosenBasePosition.value = hit.point; } else { Debug.Log("Can't build here because something blocking your way"); } } } }
public List <(Vector2Int, Vector2Int)> GetSuccessors(Vector2Int state) { List <(Vector2Int, Vector2Int)> successors = new List <(Vector2Int, Vector2Int)>(); for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { Vector2Int nextAction = new Vector2Int(x, y); nextAction = nextAction * moveSpeed; Vector2Int nextState = state + nextAction; int layerMask = 1 << 6; Collider2D hit = Physics2D.OverlapBox(nextState, colliderSize, angle, layerMask); if (hit != null) { continue; } successors.Add((nextState, nextAction)); } } return(successors); }
IEnumerator bossSheepy() { Vector2 spawnPosition = new Vector2(Random.Range(-edgeOfScreen.x, edgeOfScreen.x), Random.Range(-edgeOfScreen.y / 2, edgeOfScreen.y / 2)); for (int i = 0; i < 3; i++) { Collider2D collisions = Physics2D.OverlapBox(spawnPosition, new Vector2(2.5f, 2.5f), 0f); if (collisions) { GameObject boss = Instantiate(sheeps[sheeps.Length - 1], spawnPosition, Quaternion.identity); yield return(new WaitForSeconds(1.5f)); boss.GetComponent <sheepDestroyer>().Destruction(); break; } else { yield return(new WaitForSeconds(1f)); } } }
protected override void Awake() { Collider2D hitCollider = Physics2D.OverlapBox(gameObject.transform.position, transform.localScale / 2, 0f, layerMask); //Check when there is a new collider coming into contact with the box if (hitCollider != null) { switch (hitCollider.tag) { case Constants.DESTRUTABLE_TAG: isDestroyWhenHit = true; hitCollider.gameObject.GetComponent <Destructable> ().Damaged(); break; } } //If isn't blocked if (!isBlocked) { this.gameObject.GetComponent <SpriteRenderer> ().enabled = true; isBlocked = false; } }
void Update() { var collidingObject = Physics2D.OverlapBox(transform.position, villagerSize / 2, 0.0f).gameObject; Tile tile = collidingObject.GetComponent <Tile>(); float localSpeed = tile.type == TileType.WATER ? waterSpeed : speed; if (tile.type == TileType.FIRE) { FuckingDie(); } if (alive) { float step = localSpeed * Time.deltaTime; var nearest = GetNearestVillage(); if (nearest) { transform.position = Vector2.MoveTowards(transform.position, nearest.position, step); } } }
void FixedUpdate() { bumpercheckhit = Physics2D.OverlapBox(bumpercheckbox.position, new Vector2(bumpercheckWidth, bumpercheckHeight), 0, Player); if (bumpercheck == true) { if (hitOnceCheck == false) { if (numberOfCoins >= 0) { playerControllerScript.count = playerControllerScript.count + 1; //This line adds an extra coin to the player's total numberOfCoins = numberOfCoins - 1; //this line removes 1 coin from the total in the box hitOnceCheck = true; // this line stops multiple coins from being collected Instantiate(coin, this.transform.position, this.transform.rotation); Debug.Log("I'veBeenHit"); } } } else { hitOnceCheck = false; // this resets once bumpercheck says the player isn't colliding } }
/// <summary> /// 傷害 /// </summary> private void Damage() { if (Input.GetKeyDown(KeyCode.Mouse0)) { anim.SetTrigger("攻擊觸發"); aud.PlayOneShot(audAtk, 0.5f); Collider2D hit = Physics2D.OverlapBox(transform.position + -transform.right * offsetAttack.x + transform.up * offsetAttack.y, sizeAttack, 0, 1 << 8); if (hit && hit.name == "蟲") { hit.GetComponent <Enemy_insect>().Death(); } else if (hit && hit.name == "圖驣") { print(hit.name); hit.GetComponent <Enemy_totem>().Death(); } else if (hit && hit.name == "Boss") { print(hit.name); hit.GetComponent <Enemy_Boss>().hp--; } } }
private void Update() { var tank = Physics2D.OverlapBox(transform.position, collisionSize, 0, tankMask); if (!tank) { return; } PlayerTank playerTank = tank.GetComponent <PlayerTank>(); EnemyTank enemyTank = tank.GetComponent <EnemyTank>(); HandleAnyTankPickup(playerTank, enemyTank); if (Type == PickUpType.Tank) { AudioManager.s_Instance.PlayFxClip(AudioManager.AudioClipType.LiveTaken); } else { AudioManager.s_Instance.PlayFxClip(AudioManager.AudioClipType.BonusTaken); } Destroy(gameObject); }
// Update is called once per frame void FixedUpdate() { var dc = gameObject.GetComponent <BoxCollider2D>(); var collide = Physics2D.OverlapBox(transform.position, dc.size, 0, LayerMask.GetMask("player")); if (collide != null) { countn += 1 * Time.fixedDeltaTime; if (countn >= count) { if (GetComponent <SpriteRenderer>().flipX == true) { var shoot = Instantiate(fball, transform.position - transform.right, transform.rotation); shoot.transform.Rotate(0, 0, 180); } else { Instantiate(fball, transform.position + transform.right, transform.rotation); } countn = 0; } } }
void GroundDetection() { Collider2D[] results = new Collider2D[maxHits]; Vector2 pos = this.transform.position; int numHits = Physics2D.OverlapBox(pos + groundBoxPos, groundBoxSize, 0, groundFilter, results); if (numHits > 0) { isGrounded = true; } if (isGrounded) { IsFalling = false; } if (isGrounded && !wasGroundedLastFrame) { justGotGrounded = true; } if (!isGrounded && wasGroundedLastFrame) { justNOTGrounded = true; } }
/// <summary> /// Make all Physics OverlapCircles and Raycast for correct function of Character. /// </summary> protected virtual void doPhysicCasts() { isGrounded = Physics2D.OverlapBox(groundChecker.position, new Vector2(0.3f, 0.3f), 0, whatIsFloor); if (rbd.velocity.y != 0) { isGrounded = false; } RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, 30, 1 << 15); if (hit.collider != null) { if (closerPlatform != hit.collider) { if (isIgnoringPlatforms) { Physics2D.IgnoreCollision(boxCollider, closerPlatform, false); Physics2D.IgnoreCollision(boxCollider, hit.collider, true); } closerPlatform = hit.collider; } } }
//Snap the building object to a 1x1 grid, to be more predictable. private void SnapToGrid() { float mousePosX = aimControl.mouseCoords.x; float mousePosY = aimControl.mouseCoords.y; float snapPointX = mousePosX + ((1 - mousePosX) % 1); float snapPointY = mousePosY + ((1 - mousePosY) % 1); float snapPointZ = 0; Vector3 position = new Vector3(snapPointX, snapPointY, snapPointZ); transparentPlate.position = position; Collider2D collider = Physics2D.OverlapBox(new Vector2(snapPointX, snapPointY), new Vector2(transparentPlate.localScale.x - 1, transparentPlate.localScale.y - 1), 0); if (collider != null) { canBuild = false; transparentPlate.gameObject.GetComponent <Renderer>().enabled = false; } else { canBuild = true; transparentPlate.gameObject.GetComponent <Renderer>().enabled = true; } }
void HandleMovement(float movement) { //rb.velocity = new Vector2(movement, rb.velocity.y); //Smooth movement - smoothVelocity Makes smother the movement Vector3 velocity = new Vector2(movement, rb.velocity.y); rb.velocity = Vector3.SmoothDamp(rb.velocity, velocity, ref V3velocity, smoothVelocity); if (jump) { GetComponent <Rigidbody2D>().AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse); myAnimator.SetTrigger("Jump_Parameter"); isGrounded = false; } else { Vector2 boxCenter = (Vector2)transform.position + Vector2.down * (playerSize.y + boxSize.y) * 0.5f; isGrounded = (Physics2D.OverlapBox(boxCenter, boxSize, 0f, mask) != null); myAnimator.SetBool("Land_Parameter", true); } myAnimator.SetFloat("Speed_Patameter", Mathf.Abs(movement)); }
void RightWallDetection() { if (!checkWall) { return; } Vector3 pos = this.transform.position + (Vector3)rightBoxPos; Collider2D[] results = new Collider2D[maxColliders]; int numColliders = Physics2D.OverlapBox(pos, rightBoxSize, 0, filter, results); if (numColliders > 0) { isRightWalled = true; } if (!wasRightWalledLastFrame && isRightWalled) { justGotRightWalled = true; } }
public bool SpawnObject() { Vector2 spawn = point; if (Physics2D.OverlapBox(spawn, new Vector2(EditorBase.helpLineInt / 2, EditorBase.helpLineInt / 2), 0) == null) { //在不是自由模式下,转换至合适网格 if (!ifFreeMode) { spawn = EditorBase.GridMidPoint(spawn); } GameObject parent = GameObject.Find("Terrain"); if (parent == null) { new GameObject("Terrain"); } var g = Instantiate(Select, spawn, Quaternion.identity); g.transform.parent = parent.transform; nowLeftTime = BuildDelta; return(true); } return(false); }
void FixedUpdate() { bool wasGrounded = collInfo.onGround; collInfo.Reset(); SetOverLapBoxOffsetAndDimension(); // Check for ground collision collInfo.onGround = Physics2D.OverlapBox((Vector2)transform.position + bottomOffset, widthOverlapBox, 0.0f, whatIsGround); if (!collInfo.onGround && groundedLastFrame) { groundedLastFrame = false; collInfo.timeLeftGround = Time.time; } groundedLastFrame = collInfo.onGround; if (!wasGrounded && collInfo.onGround) { OnLandEvent.Invoke(); } // Check for left wall collision collInfo.onWallLeft = Physics2D.OverlapBox((Vector2)transform.position + leftOffset, heightOverlapBox, 0.0f, whatIsWall); // Check for right wall collision collInfo.onWallRight = Physics2D.OverlapBox((Vector2)transform.position + rightOffset, heightOverlapBox, 0.0f, whatIsWall); collInfo.onWall = collInfo.onWallLeft || collInfo.onWallRight ? true : false; // Check to see if we hit a hazard collInfo.touchedHazard = (Physics2D.OverlapBox((Vector2)transform.position + bottomOffset, widthOverlapBox, 0.0f, whatIsHazard) || Physics2D.OverlapBox((Vector2)transform.position + aboveOffset, widthOverlapBox, 0.0f, whatIsHazard) || Physics2D.OverlapBox((Vector2)transform.position + leftOffset, heightOverlapBox, 0.0f, whatIsHazard) || Physics2D.OverlapBox((Vector2)transform.position + rightOffset, heightOverlapBox, 0.0f, whatIsHazard)); }
private void CmdCreateExplosions(Vector2 direction) { ContactFilter2D contactFilter = new ContactFilter2D(); Vector2 explosionDimensions = explosionPrefab.GetComponent <SpriteRenderer> ().bounds.size; Vector2 explosionPosition = (Vector2)this.gameObject.transform.position + (explosionDimensions.x * direction); for (int explosionIndex = 1; explosionIndex < explosionRange; explosionIndex++) { Collider2D[] colliders = new Collider2D[4]; Physics2D.OverlapBox(explosionPosition, explosionDimensions, 0.0f, contactFilter, colliders); bool foundBlockOrWall = false; foreach (Collider2D collider in colliders) { if (collider) { foundBlockOrWall = collider.tag == "Wall" || collider.tag == "Block"; if (collider.tag == "Block") { NetworkServer.Destroy(collider.gameObject); } if (foundBlockOrWall) { break; } } } if (foundBlockOrWall) { break; } GameObject explosion = Instantiate(explosionPrefab, explosionPosition, Quaternion.identity) as GameObject; NetworkServer.Spawn(explosion); Destroy(explosion, this.explosionDuration); explosionPosition += (explosionDimensions.x * direction); } }
private void Patrol() { if (isGrounded) { if (facingRight) { RaycastHit2D hitRay = Physics2D.Raycast(transform.position, Vector2.right, 1, ground); Collider2D frontBlocked = Physics2D.OverlapBox(new Vector2(transform.position.x + 1.5f, transform.position.y), new Vector2(0.5f, 0.5f), 0, ground); Collider2D grounded = Physics2D.OverlapBox(new Vector2(transform.position.x + 1, transform.position.y - 1), new Vector2(0.5f, 0.5f), 0, ground); if (!frontBlocked && grounded) { rb.velocity = Vector2.right * speed / 2; } else if (frontBlocked || !grounded) { facingRight = false; //Flip(); } } else if (!facingRight) { RaycastHit2D hitRay = Physics2D.Raycast(transform.position, Vector2.left, 1, ground); Collider2D hitBoxFront = Physics2D.OverlapBox(new Vector2(transform.position.x - 1.5f, transform.position.y), new Vector2(0.5f, 0.5f), 0, ground); Collider2D grounded = Physics2D.OverlapBox(new Vector2(transform.position.x - 1, transform.position.y - 1), new Vector2(0.5f, 0.5f), 0, ground); if (!hitBoxFront && grounded) { rb.velocity = Vector2.left * speed / 2; } else if (hitBoxFront || !grounded) { facingRight = true; //Flip(); } } } }
void RoundedEdges(int x, int y) { if (useRoundedEdges) { Collider2D hitWall = Physics2D.OverlapBox(new Vector2(x, y), hitSize, 0, wallMask); if (hitWall) { Collider2D hitTop = Physics2D.OverlapBox(new Vector2(x, y + 1), hitSize, 0, wallMask); Collider2D hitRight = Physics2D.OverlapBox(new Vector2(x + 1, y), hitSize, 0, wallMask); Collider2D hitBottom = Physics2D.OverlapBox(new Vector2(x, y - 1), hitSize, 0, wallMask); Collider2D hitLeft = Physics2D.OverlapBox(new Vector2(x - 1, y), hitSize, 0, wallMask); int bitVal = 0; if (!hitTop) { bitVal += 1; } if (!hitRight) { bitVal += 2; } if (!hitBottom) { bitVal += 4; } if (!hitLeft) { bitVal += 8; } if (bitVal > 0) { GameObject goEdge = Instantiate(roundedEdges[bitVal], new Vector2(x, y), Quaternion.identity) as GameObject; goEdge.name = roundedEdges[bitVal].name; goEdge.transform.SetParent(hitWall.transform); } } } }
private void RoundedEdges(int x, int y) { if (!roundedEdges) { return; } var position = new Vector2(x, y); var hitWall = Physics2D.OverlapBox(position, _hitSize, 0, _wallMask); if (!hitWall) { return; } var hitTop = Physics2D.OverlapBox(new Vector2(x, y + 1), _hitSize, 0, _wallMask); var hitRight = Physics2D.OverlapBox(new Vector2(x + 1, y), _hitSize, 0, _wallMask); var hitBottom = Physics2D.OverlapBox(new Vector2(x, y - 1), _hitSize, 0, _wallMask); var hitLeft = Physics2D.OverlapBox(new Vector2(x - 1, y), _hitSize, 0, _wallMask); var bitValue = 0; bitValue += hitTop ? 0 : 1; bitValue += hitRight ? 0 : 2; bitValue += hitBottom ? 0 : 4; bitValue += hitLeft ? 0 : 8; if (bitValue == 0) { return; } var edgePrefab = wallRoundedEdges[bitValue]; var goItem = Instantiate(edgePrefab, position, Quaternion.identity, hitWall.transform); goItem.name = edgePrefab.name; }
public void OnMouseUp() { if (!Manager.Instance.CanInteract()) { return; } dragging = false; height = 0f; int type = isMatrix ? 0 : 1; if (dragTime < 0.25f && !LeftArea(1.2f)) { UseCard(); return; } else { Collider2D hit = Physics2D.OverlapBox(transform.position, coll.bounds.size, 0, areaMask); if (hit) { CardHolder holder = hit.GetComponent <CardHolder> (); if (holder.Allows(type)) { holder.AddCard(this, false); return; } } AudioManager.Instance.PlayEffectAt(6, transform.position, 0.5f); } currentHolder.AddCard(this, true); }
//SmallFX = 0.1f, WallFrontY = 0.5f, WallBackX = 0.1f, WallBackY = 0.7f; //FX= 0.1f, fy= 1.7f, bx=0.1, by 1.9 void Update() { velocityX = GetComponent <Rigidbody2D>().velocity.x; velocityY = GetComponent <Rigidbody2D>().velocity.y; grounded = GetComponent <Rigidbody2D>().velocity.y < 1f || grounded?Physics2D.OverlapBox(downCheck.position, new Vector2(0.55f, 0.1f), 0, whatIsGround) : false; onPlatform = Physics2D.OverlapBox(downCheck.position, new Vector2(0.6f, 0.1f), 0, whatIsPlatform); inWater = Physics2D.OverlapBox(transform.position, new Vector2(0.6f, 0.1f), 0, whatIsWater) && Physics2D.OverlapBox(downCheck.position, new Vector2(0.6f, 0.1f), 0, whatIsWater); if (isSmall) { againstStep = Physics2D.OverlapBox(smallFront.position + new Vector3(0, -0.4f, 0), new Vector2(0.1f, 0.1f), 0, whatIsWall); againstLeft = isMirrored ? Physics2D.OverlapBox(smallFront.position, new Vector2(smallFrontSize.x, smallFrontSize.y), 0, whatIsWall) : Physics2D.OverlapBox(smallBack.position, new Vector2(smallBackSize.x, smallBackSize.y), 0, whatIsWall); againstRight = isMirrored ? Physics2D.OverlapBox(smallBack.position, new Vector2(smallBackSize.x, smallBackSize.y), 0, whatIsWall) : Physics2D.OverlapBox(smallFront.position, new Vector2(smallFrontSize.x, smallFrontSize.y), 0, whatIsWall); againstFront = Physics2D.OverlapBox(smallFront.position, new Vector2(0.1f, 0.5f), 0, whatIsWall); } else { againstStep = Physics2D.OverlapBox(frontCheck.position + new Vector3(0, -0.95f, 0), new Vector2(0.1f, 0.2f), 0, whatIsWall); againstLeft = isMirrored ? Physics2D.OverlapBox(frontCheck.position, new Vector2(wallFrontSize.x, wallFrontSize.y), 0, whatIsWall) : Physics2D.OverlapBox(backCheck.position, new Vector2(wallBackSize.x, wallBackSize.y), 0, whatIsWall); againstRight = isMirrored ? Physics2D.OverlapBox(backCheck.position, new Vector2(wallBackSize.x, wallBackSize.y), 0, whatIsWall) : Physics2D.OverlapBox(frontCheck.position, new Vector2(wallFrontSize.x, wallFrontSize.y), 0, whatIsWall); againstFront = Physics2D.OverlapBox(frontCheck.position, new Vector2(0.1f, 1.7f), 0, whatIsWall); } //NOTE: if first statement (before ?) is true, var equals first value (after ?), else it equals second value (after :). }
private void CreateRandomItems() { Debug.Assert(_doorPos.HasValue, "Exit door must be instantiated before placing items."); const int offset = 2; // TODO: Why though? for (var x = (int)minX - offset; x <= (int)maxX + offset; ++x) { for (var y = (int)minY - offset; y <= (int)maxY + offset; ++y) { // Note that the angle (of 0) is hugely important. If unspecified, all the // areas surrounding a floor tile (i.e., walls and other open floors) will be triggering // collisions as well. var hitFloor = Physics2D.OverlapBox(new Vector2(x, y), _hitSize, 0, _floorMask); if (hitFloor) { // Ensure we're not placing something onto the exit door. // ReSharper disable once PossibleInvalidOperationException var positionIsExitDoor = Vector2.Equals(hitFloor.transform.position, _doorPos.Value); if (positionIsExitDoor) { continue; } var hitTop = Physics2D.OverlapBox(new Vector2(x, y + 1), _hitSize, 0, _wallMask); var hitRight = Physics2D.OverlapBox(new Vector2(x + 1, y), _hitSize, 0, _wallMask); var hitBottom = Physics2D.OverlapBox(new Vector2(x, y - 1), _hitSize, 0, _wallMask); var hitLeft = Physics2D.OverlapBox(new Vector2(x - 1, y), _hitSize, 0, _wallMask); CreateRandomItem(hitFloor, hitTop, hitRight, hitBottom, hitLeft); CreateRandomEnemy(hitFloor, hitTop, hitRight, hitBottom, hitLeft); } RoundedEdges(x, y); } } }
void Start() { if (!wasBuilt) { Vector2 location = new Vector2(1f, 1f); Collider2D collider = Physics2D.OverlapBox(transform.position, location, 0f, LayerMask.GetMask("EmptyField"), -Mathf.Infinity, Mathf.Infinity); //Debug.Log(LayerMask.LayerToName(LayerMask.GetMask("EmptyField"))); if (collider.gameObject != null) { transform.SetParent(collider.gameObject.transform); transform.parent.GetComponent <emptyField>().isEmpty = false; } } if (wasBuilt) { if (whatResource == "food") { Controller.foodSupplyRate += IndividualOutput; Controller.NumOfFarms += 1; } else if (whatResource == "wood") { Controller.woodSupplyRate += IndividualOutput; Controller.NumOfLumberCamps += 1; } else if (whatResource == "metal") { Controller.metalSupplyRate += IndividualOutput; Controller.NumOfMines += 1; } else if (whatResource == "stone") { Controller.stoneSupplyRate += IndividualOutput; Controller.NumOfQuarries += 1; } } }
// Update is called once per frame void Update() { if (Globals.Inst.Player && Vector3.Distance(transform.position, Globals.Inst.Player.transform.position) > KillDistance) //check if the bullet is still within range of the player to stay alive { DestroyBullet(); } else { transform.position += transform.up.normalized * MovementSpeed * Time.deltaTime; //Move the bullet forward. Since we're not using a rigidbody we have to compensate for frame time ourself. } _lifetime += Time.deltaTime; if (_lifetime > Lifespan) { DestroyBullet(); } //check if the bullet hit anything Collider2D otherCollider; if (UseCircleCollider) { otherCollider = Physics2D.OverlapCircle(new Vector2(transform.position.x, transform.position.y), CirlceColliderRadius); } else { otherCollider = Physics2D.OverlapBox(new Vector2(transform.position.x, transform.position.y), new Vector2(BoxColliderWidth, BoxColliderHeight), transform.rotation.eulerAngles.z); } if (Creator && otherCollider && otherCollider != Creator.GetComponent <Collider2D>()) { if (otherCollider.gameObject.GetComponent <CombatEntity>() != null) { otherCollider.gameObject.GetComponent <CombatEntity>().HandleBullet(this); } //temp? below (destroy gameobject if it hits something, anything) DestroyBullet(); } }
private void Update() { horizontal = Mathf.Clamp(Input.GetAxisRaw("Horizontal"), -1, 1); vertical = Mathf.Clamp(Input.GetAxisRaw("Vertical"), -1, 1); if (Vector3.Distance(transform.position, movePoint.position) <= 0.05f && !isMoving) { Vector3 hitSize = Vector3.one * 0.5f; if (Mathf.Abs(horizontal) == 1) { if (!Physics2D.OverlapBox(movePoint.position + new Vector3(horizontal, 0f, 0f), hitSize, 0, obstacleMask)) { isMoving = true; movePoint.position += new Vector3(horizontal, 0f, 0f); sprite.localScale = new Vector2(flipX * horizontal, sprite.localScale.y); SetLastDirection(new Vector3(horizontal, 0f, 0f)); mover.MoveTo(movePoint.position); } } else if (Mathf.Abs(vertical) == 1) { if (!Physics2D.OverlapBox(movePoint.position + new Vector3(0f, vertical, 0f), hitSize, 0, obstacleMask)) { isMoving = true; movePoint.position += new Vector3(0f, vertical, 0f); SetLastDirection(new Vector3(0f, vertical, 0f)); mover.MoveTo(movePoint.position); } } } if (Mathf.Abs(horizontal) + Mathf.Abs(vertical) == 0) { isMoving = false; } //} }