/// <summary> /// Moves the character. /// </summary> override public bool DoMove() { CheckForPlayer(); switch (state) { case SnapperState.CHARGING: enemy.Translate(0, TimeManager.FrameTime * speed, true); if (autoRetractAtTop && enemy.transform.position.y >= endingYPos) { state = SnapperState.RETRACTING; } break; case SnapperState.RETRACTING: enemy.Translate(0, TimeManager.FrameTime * -retractSpeed, true); if (enemy.transform.position.y <= startingYPos) { state = SnapperState.IDLE; } break; } if (enemy.transform.position.y > endingYPos) { enemy.Translate(0, endingYPos - enemy.transform.position.y, true); } if (enemy.transform.position.y < startingYPos) { enemy.Translate(0, startingYPos - enemy.transform.position.y, true); } return(true); }
/// <summary> /// Checks for the player and updates state. /// </summary> virtual protected void CheckForPlayer() { RaycastHit2D hit = Physics2D.Raycast(transform.position, new Vector3(0, 1, 0), sightRange, sightLayers); if (hit.collider != null) { Character character = null; ICharacterReference characterRef = (ICharacterReference)hit.collider.gameObject.GetComponent(typeof(ICharacterReference)); if (characterRef == null) { character = hit.collider.gameObject.GetComponent <Character> (); } else { character = characterRef.Character; } if (character != null) { if (state == SnapperState.IDLE || !mustFullyRetract) { state = SnapperState.CHARGING; } } else { if (state == SnapperState.CHARGING && enemy.transform.position.y >= endingYPos) { state = SnapperState.RETRACTING; } } } else { if (state == SnapperState.CHARGING && enemy.transform.position.y >= endingYPos) { state = SnapperState.RETRACTING; } } }