void GravityGunOn() { // Check if box is in front of us. var boxInRange = Raycast(new Vector2(Direction * 0.5f, 0), Direction * Vector2.right, GravityGunRange, LayersManager.GetLayerMaskObjects(WorldsController.PlayerCurrentWorld)); if (boxInRange && boxInRange.transform.gameObject.tag == "Box") { GunNoiseSource?.Play(); GravityGunActive = true; SpriteRend.sprite = GravityGunSprite; // Grab box, turn off its colliders, save its position relative to player, expand player collider GrabbedBox = boxInRange.transform.gameObject.GetComponent <BoxController>(); GrabbedBox.ToggleGrabbed(); BoxHitbox = GrabbedBox.gameObject.GetComponent <BoxCollider2D>(); if (GrabbedBox.transform.position.x - gameObject.transform.position.x < 0) { BoxOffset = new Vector2(GrabbedBox.transform.position.x - gameObject.transform.position.x - MinDistance, GrabbedBox.transform.position.y - gameObject.transform.position.y); } else { BoxOffset = new Vector2(GrabbedBox.transform.position.x - gameObject.transform.position.x + MinDistance, GrabbedBox.transform.position.y - gameObject.transform.position.y); } // If player grabs a box but isn't on the ground OR has something over it, they can't pull it if (!IsGrounded && GrabbedBox.IsGrounded) { rigidBody.velocity = new Vector2(0, 0); rigidBody.gravityScale = 0; CanMove = false; } if (!GrabbedBox.IsGrabbable) { CanMove = false; } Debug.Log("Gravity gun on"); } }
void FixedUpdate() { IsGrounded = false; var CheckAbove = Raycast(new Vector2(0, Collider.size.y / 2 + 0.05f), Vector2.up, CloseDistance, LayersManager.GetLayerMaskObjects(CurrentWorld)); var CheckBelow = Raycast(new Vector2(0, -Collider.size.y / 2 - 0.01f), Vector2.down, CloseDistance, LayersManager.GetLayerMaskWorld(CurrentWorld)); var CheckLeft = Raycast(new Vector2(-Collider.size.x / 2 - 0.001f, 0), Vector2.left, CloseDistance, LayersManager.GetLayerMaskWorld(CurrentWorld)); var CheckRight = Raycast(new Vector2(Collider.size.x / 2 + 0.001f, 0), Vector2.right, CloseDistance, LayersManager.GetLayerMaskWorld(CurrentWorld)); if (CheckLeft) { TouchingLeft = true; } else { TouchingLeft = false; } if (CheckRight) { TouchingRight = true; } else { TouchingRight = false; } if (CheckBelow) { IsGrounded = true; } if (CheckAbove) { IsGrabbable = false; } else { IsGrabbable = true; } // If this box is being grabbed, we want it to stop the player if it hits a ceiling/wall if (BeingGrabbed) { CheckAbove = Raycast(new Vector2(0, Collider.size.y / 2 + 0.05f), Vector2.up, CloseDistance, LayersManager.GetLayerMaskWorld(CurrentWorld)); if (CheckAbove) { PlayerObject.GetComponent <PlayerController>().HaltPlayerJump(); } /*if (IsGrounded && !PlayerObject.GetComponent<PlayerController>().IsGrounded) * { * PlayerObject.GetComponent<PlayerController>().HaltPlayerJump(); * }*/ } }
void FixedUpdate() { //Check the environment to determine status PhysicsCheck(); //Process ground and air movements if (CanMove) { GroundMovement(); MidAirMovement(); } if (GravityGunAcquired) { GravityGunControl(); } // TESTING PURPOSES: // See the ray that determines whether a box is within gravity gun range. Raycast(new Vector2(Direction * 0.5f, 0), Direction * Vector2.right, GravityGunRange, LayersManager.GetLayerMaskObjects(WorldsController.PlayerCurrentWorld)); }