/// <summary> /// Checks if there is ground beneath this GameObject. /// If there isn´t, we rotate the gameObject /// </summary> protected override void Check() { EnemyEngine enemyEngine = GetComponent <EnemyEngine>(); List <RaycastHit2D> rayCastInfo = new List <RaycastHit2D>(); ContactFilter2D contactFilter2D = new ContactFilter2D(); int sensorRay = Physics2D.Raycast(raycastEmitter.transform.position, Vector2.down, contactFilter2D, rayCastInfo, 1); Debug.DrawRay(raycastEmitter.transform.position, Vector2.down, Color.green); int i = 0; bool floorFound = false; while (i < sensorRay) { if (rayCastInfo[i].collider.gameObject.layer == enemyEngine.GetGroundLayer()) { floorFound = true; } i++; } //If my ray did NOT detect any floor, we change dir if (!floorFound) { ChangeDir(); } }
// Update is called once per frame void Update() { MoveForward(); EnemyEngine enemyEngine = GetComponent <EnemyEngine>(); List <RaycastHit2D> bumperRay = new List <RaycastHit2D>(); ContactFilter2D contactFilter2D = new ContactFilter2D(); Vector2 dir = (pMovementSpeed < 0) ? Vector2.left : Vector2.right; int PlayerRayCount = Physics2D.BoxCast(viewPoint.transform.position, new Vector2(detectionDistance, 0.5f), 0f, dir, contactFilter2D, bumperRay, detectionDistance); int i = 0; bool stop = false; while (i < PlayerRayCount && !stop) { if (bumperRay[i].collider.gameObject.layer != enemyEngine.GetGroundLayer() && bumperRay[i].collider.gameObject.name != "BaseEnemy") { ChangeDir(); Renderer rend = GetComponent <Renderer>(); if (pMovementDir < 0) { viewPoint.transform.position = new Vector2(transform.position.x + rend.bounds.extents.x, transform.position.y); } else { viewPoint.transform.position = new Vector2(transform.position.x - rend.bounds.extents.x, transform.position.y); } stop = true; } i++; } }
private void OnTriggerExit2D(Collider2D collision) { EnemyEngine enemyEngine = GetComponent <EnemyEngine>(); if (enemyEngine != null) { if (sensorActive) { if (!collision.gameObject.layer.Equals(enemyEngine.GetGroundLayer())) { sensorActive = false; OnSensorExit(); } } } }
private void OnTriggerEnter2D(Collider2D collision) { EnemyEngine enemyEngine = GetComponent <EnemyEngine>(); if (enemyEngine != null) { if (!collision.gameObject.layer.Equals(enemyEngine.GetGroundLayer())) { sensorActive = true; OnSensorActive(); } } else { Debug.LogWarning("No components detected! A sensor can´t work without at least one"); } }
private void Update() { if (canJump) { GetComponent <Rigidbody2D>().velocity = jumper.GetMovement(); //Jump! if (updatePlayerPosition) { //Where do we jump now to get to the target? jumperModifier = (transform.position.x > enemyEngine.GetTargetPosition().x) ? 1 : -1; RotateToTarget(); updatePlayerPosition = false; StartCoroutine(SetTargetPositionAfterSeconds()); } GetComponent <Rigidbody2D>().velocity += bullet.GetMovement() * jumperModifier; //Move towards target canJump = false; //We shall not jump until next timer states so } else { if (Time.time - lastJumpTimer > jumpDelay) { //Cast a 2-width box into the ground. The box is so that the entity doesn´t get stuck in platform´s edges //((which used to happen and was pretty annoying)) EnemyEngine enemyEngine = GetComponent <EnemyEngine>(); List <RaycastHit2D> groundRay = new List <RaycastHit2D>(); ContactFilter2D contactFilter2D = new ContactFilter2D(); int groundRayCount = Physics2D.BoxCast(groundPoint.transform.position, new Vector2(2, groundCheckRadius), 0f, Vector2.down, contactFilter2D, groundRay, groundCheckRadius); int i = 0; bool stop = false; while (i < groundRayCount && !stop) { if (groundRay[i].collider.gameObject.layer == enemyEngine.GetGroundLayer()) { canJump = true; lastJumpTimer = Time.time; stop = true; } i++; } } } }
//Is this entity touching the ground? public void CheckIfGrounded() { EnemyEngine enemyEngine = GetComponent <EnemyEngine>(); List <RaycastHit2D> groundRay = new List <RaycastHit2D>(); ContactFilter2D contactFilter2D = new ContactFilter2D(); int groundRayCount = Physics2D.BoxCast(groundPoint.transform.position, new Vector2(2, groundCheckRadius), 0f, Vector2.down, contactFilter2D, groundRay, groundCheckRadius); int i = 0; bool stop = false; while (i < groundRayCount && !stop) { if (groundRay[i].collider.gameObject.layer == enemyEngine.GetGroundLayer()) { canJump = true; lastJumpTimer = Time.time; } i++; } }
// Update is called once per frame void Update() { CheckForDeactivateStateChange(); List <RaycastHit2D> rayCastInfo = new List <RaycastHit2D>(); ContactFilter2D contactFilter2D = new ContactFilter2D(); int sensorRay = Physics2D.Raycast(transform.position, LineDirection, contactFilter2D, rayCastInfo, LineDistance); Debug.DrawRay(transform.position, LineDirection * LineDistance, Color.green); int i = 0; bool stop = false; while (i < sensorRay && !stop) { EnemyEngine enemyEngine = GetComponent <EnemyEngine>(); if (enemyEngine != null) { if (rayCastInfo[i].collider.gameObject.layer == enemyEngine.GetGroundLayer() && !SeeThroughWalls) { //Stop looking stop = true; } else { if (rayCastInfo[i].collider.gameObject.layer == enemyEngine.GetPlayerLayer()) { OnSensorDetection(); //Stop looking stop = true; } } } i++; } }