//--------------------------------------------------------------------------- void FlipCurrentMovement() { if (m_currentMoveDirection == PigInitialDirection.Left) { m_currentMoveDirection = PigInitialDirection.Right; } else { m_currentMoveDirection = PigInitialDirection.Left; } }
//--------------------------------------------------------------------------- public void Respawn() { transform.position = m_pigHomePoint; m_currentPigAlertState = AlertState.NotAlert; m_currentMoveDirection = m_initialMoveDirection; //m_isFacingRight = m_isFacingRightHolder; m_movement = new Vector2(0f, rigidbody2D.velocity.y); if (!m_isFacingRight || m_initialMoveDirection == PigInitialDirection.Left) { Flip(); } m_anim.SetBool("isAlert", false); m_anim.SetBool("caughtPlayer", false); this.gameObject.layer = 19; }
// Use this for initialization void Start() { Physics2D.IgnoreLayerCollision(19, 19, true); Physics2D.IgnoreLayerCollision(19, 20, true); //-------------------------------------------------- // ***Moving Code*** //Flip the art if facing left on start if (!m_isFacingRight || m_initialMoveDirection == PigInitialDirection.Left) { Flip(); } //Set default values for movement m_movement.x = 0.0f; m_movement.y = 0.0f; //-------------------------------------------------- //Get pointer to the animator m_anim = GetComponent <Animator>(); //-------------------------------------------------- m_audioSource = GetComponent <AudioSource>(); //-------------------------------------------------- //Get pointer to the sprite m_sprite = gameObject.GetComponent <SpriteRenderer> (); //If not startEnabled set the animator variable wait //to freeze it if (!m_isStartEnabled) { m_anim.SetBool("wait", true); } //set playerIsNear to true to keep it from being set to false automatically m_isPlayerNear = true; if (m_pigBehavior == PatrolBehaviors.Patrol) { m_isStartEnabled = true; } m_pigHomePoint = transform.position; m_currentMoveDirection = m_initialMoveDirection; m_layerMask = 1 << 8 | 1 << 11; }
// Update is called once per frame void Update() { //If not startEnabled freeze it this AI if (m_pigBehavior == PatrolBehaviors.Patrol && m_currentPigAlertState == AlertState.NotAlert) { //Set the movement variable m_movement = new Vector2 ( m_pigInitialMovementSpeed.x * m_currentDirection.x, rigidbody2D.velocity.y ); } else if (m_currentPigAlertState == AlertState.ChasePlayer) { if (!m_hasPlayedAlertSound) { PlaySound(m_alertSound); m_hasPlayedAlertSound = true; } m_movement = new Vector2 ( m_pigAlertMovementSpeed.x * m_currentDirection.x, rigidbody2D.velocity.y ); this.gameObject.layer = 20; m_anim.SetBool("isAlert", true); m_isFacingHomePoint = false; } else if (m_currentPigAlertState == AlertState.LostPlayer) { this.gameObject.layer = 19; m_anim.SetBool("isAlert", false); m_movement = new Vector2(0f, rigidbody2D.velocity.y); } else if (m_currentPigAlertState == AlertState.CaughtPlayer) { m_anim.SetBool("caughtPlayer", true); m_movement = new Vector2(0f, rigidbody2D.velocity.y); } else if (m_currentPigAlertState == AlertState.BackToStart) { if (!m_isFacingHomePoint) { float direction = m_pigHomePoint.x - transform.position.x; if ((direction < 0f && m_currentDirection.x > 0f) || (direction > 0f && m_currentDirection.x < 0f)) { Flip(); FlipCurrentMovement(); } m_isFacingHomePoint = true; } // float step = ( 0.5f * m_pigInitialMovementSpeed.x) * 2f * Time.deltaTime; // transform.position = Vector3.MoveTowards(transform.position, m_pigHomePoint, step); if (m_currentDirection.x * (m_pigHomePoint.x - transform.position.x) <= 0f) { m_currentPigAlertState = AlertState.NotAlert; m_movement = new Vector2(0f, rigidbody2D.velocity.y); m_currentMoveDirection = m_initialMoveDirection; if ((!m_isFacingRight && m_currentMoveDirection == PigInitialDirection.Right) || (m_isFacingRight && m_currentMoveDirection == PigInitialDirection.Left)) { //FlipCurrentMovement(); Flip(); } m_isFacingHomePoint = false; } else { m_movement = new Vector2 ( m_pigInitialMovementSpeed.x * m_currentDirection.x, 0f ); } } //Set the speed variable in our animator to our //x speed m_anim.SetFloat("speed", Mathf.Abs(m_movement.x)); // Raycast for pig LOS RaycastHit2D hit = Physics2D.Raycast(transform.position, m_currentDirection, Mathf.Infinity, m_layerMask); if (hit.collider.gameObject.tag == "Player" && m_currentPigAlertState != AlertState.CaughtPlayer) { if (hit.collider.gameObject.GetComponent <Player_Stealthed>().isLit) { //Debug.DrawRay(transform.position, m_currentDirection * hit.distance, Color.white); m_currentPigAlertState = AlertState.ChasePlayer; m_currentAlertStateTimer = 0.0f; } } if (hit.collider.gameObject.tag == "Player" && m_currentPigAlertState == AlertState.ChasePlayer) { m_canSeePlayer = true; } else { m_canSeePlayer = false; } }
//--------------------------------------------------------------------------- public void Respawn() { transform.position = m_pigHomePoint; m_currentPigAlertState = AlertState.NotAlert; m_currentMoveDirection = m_initialMoveDirection; //m_isFacingRight = m_isFacingRightHolder; m_movement = new Vector2(0f, rigidbody2D.velocity.y); if (!m_isFacingRight || m_initialMoveDirection == PigInitialDirection.Left) Flip(); m_anim.SetBool("isAlert", false); m_anim.SetBool("caughtPlayer", false); this.gameObject.layer = 19; }
// Update is called once per frame void Update () { //If not startEnabled freeze it this AI if ( m_pigBehavior == PatrolBehaviors.Patrol && m_currentPigAlertState == AlertState.NotAlert ) { //Set the movement variable m_movement = new Vector2 ( m_pigInitialMovementSpeed.x * m_currentDirection.x, rigidbody2D.velocity.y ); } else if ( m_currentPigAlertState == AlertState.ChasePlayer ) { if (!m_hasPlayedAlertSound) { PlaySound(m_alertSound); m_hasPlayedAlertSound = true; } m_movement = new Vector2 ( m_pigAlertMovementSpeed.x * m_currentDirection.x, rigidbody2D.velocity.y ); this.gameObject.layer = 20; m_anim.SetBool("isAlert", true); m_isFacingHomePoint = false; } else if ( m_currentPigAlertState == AlertState.LostPlayer ) { this.gameObject.layer = 19; m_anim.SetBool("isAlert", false); m_movement = new Vector2(0f, rigidbody2D.velocity.y); } else if ( m_currentPigAlertState == AlertState.CaughtPlayer ) { m_anim.SetBool("caughtPlayer", true); m_movement = new Vector2(0f, rigidbody2D.velocity.y); } else if ( m_currentPigAlertState == AlertState.BackToStart ) { if (!m_isFacingHomePoint) { float direction = m_pigHomePoint.x - transform.position.x; if ( ( direction < 0f && m_currentDirection.x > 0f ) || ( direction > 0f && m_currentDirection.x < 0f ) ) { Flip(); FlipCurrentMovement(); } m_isFacingHomePoint = true; } // float step = ( 0.5f * m_pigInitialMovementSpeed.x) * 2f * Time.deltaTime; // transform.position = Vector3.MoveTowards(transform.position, m_pigHomePoint, step); if ( m_currentDirection.x * ( m_pigHomePoint.x - transform.position.x ) <= 0f ) { m_currentPigAlertState = AlertState.NotAlert; m_movement = new Vector2(0f, rigidbody2D.velocity.y); m_currentMoveDirection = m_initialMoveDirection; if ((!m_isFacingRight && m_currentMoveDirection == PigInitialDirection.Right) || (m_isFacingRight && m_currentMoveDirection == PigInitialDirection.Left)) { //FlipCurrentMovement(); Flip(); } m_isFacingHomePoint = false; } else { m_movement = new Vector2 ( m_pigInitialMovementSpeed.x * m_currentDirection.x, 0f ); } } //Set the speed variable in our animator to our //x speed m_anim.SetFloat ("speed", Mathf.Abs (m_movement.x)); // Raycast for pig LOS RaycastHit2D hit = Physics2D.Raycast(transform.position, m_currentDirection, Mathf.Infinity, m_layerMask); if ( hit.collider.gameObject.tag == "Player" && m_currentPigAlertState != AlertState.CaughtPlayer ) { if ( hit.collider.gameObject.GetComponent<Player_Stealthed>().isLit ) { //Debug.DrawRay(transform.position, m_currentDirection * hit.distance, Color.white); m_currentPigAlertState = AlertState.ChasePlayer; m_currentAlertStateTimer = 0.0f; } } if ( hit.collider.gameObject.tag == "Player" && m_currentPigAlertState == AlertState.ChasePlayer) { m_canSeePlayer = true; } else { m_canSeePlayer = false; } }
// Use this for initialization void Start () { Physics2D.IgnoreLayerCollision(19, 19, true); Physics2D.IgnoreLayerCollision(19, 20, true); //-------------------------------------------------- // ***Moving Code*** //Flip the art if facing left on start if (!m_isFacingRight || m_initialMoveDirection == PigInitialDirection.Left) Flip(); //Set default values for movement m_movement.x = 0.0f; m_movement.y = 0.0f; //-------------------------------------------------- //Get pointer to the animator m_anim = GetComponent<Animator>(); //-------------------------------------------------- m_audioSource = GetComponent<AudioSource>(); //-------------------------------------------------- //Get pointer to the sprite m_sprite = gameObject.GetComponent<SpriteRenderer> (); //If not startEnabled set the animator variable wait //to freeze it if (!m_isStartEnabled) m_anim.SetBool ("wait", true); //set playerIsNear to true to keep it from being set to false automatically m_isPlayerNear = true; if ( m_pigBehavior == PatrolBehaviors.Patrol ) { m_isStartEnabled = true; } m_pigHomePoint = transform.position; m_currentMoveDirection = m_initialMoveDirection; m_layerMask = 1 << 8 | 1 << 11; }