// Update is called once per frame void Update() { if (m_timerGodMode > 0) { m_timerGodMode -= Time.deltaTime; m_sprRender.color = Time.frameCount % 2 == 0? Color.red : Color.white; if (m_movingBehaviour != null) { m_movingBehaviour.ApplyForce(m_lastDamageData.Force * m_movingBehaviour.MaxForce); } } else { m_sprRender.color = Color.white; } }
// Update is called once per frame void Update() { Vector3 vPlayerPos = m_player.transform.position; vPlayerPos.z = transform.position.z; Ray2D sightRay = new Ray2D(transform.position, vPlayerPos - transform.position); float distToTarget = Vector2.Distance(vPlayerPos, transform.position); float fSightBlockedDist = IsSightBlockedByBlockedTiles? RpgMapHelper.Raycast(sightRay, distToTarget) : -1f; // NOTE: fSightBlockedDist will be -1f if sight line is not blocked by blocked collision tile bool isPlayerSeen = distToTarget < SightDistance && fSightBlockedDist == -1f; if (isPlayerSeen) { m_pathFindingBehaviour.TargetPos = vPlayerPos; } bool isTargetReached = Vector2.Distance(m_pathFindingBehaviour.TargetPos, transform.position) <= MinDistToReachTarget; if (!isPlayerSeen && isTargetReached) { // Move around m_pathFindingBehaviour.enabled = false; vPlayerPos = transform.position; m_fAngOff += Random.Range(-AngRandOff, AngRandOff); Vector3 vOffset = Quaternion.AngleAxis(m_fAngOff, Vector3.forward) * (AngRandRadious * Vector3.right); vPlayerPos += vOffset; m_moving.Arrive(vPlayerPos); } else // Follow the player { // stop following the path when closed enough to target m_pathFindingBehaviour.enabled = !isTargetReached; if (!m_pathFindingBehaviour.enabled) { m_fAngOff += Random.Range(-AngRandOff, AngRandOff); Vector3 vOffset = Quaternion.AngleAxis(m_fAngOff, Vector3.forward) * (AngRandRadious * Vector3.right); vPlayerPos += vOffset; Debug.DrawLine(transform.position, m_player.transform.position, Color.blue); Debug.DrawRay(m_player.transform.position, vOffset, Color.blue); m_moving.Arrive(vPlayerPos); } } //+++avoid obstacles Vector3 vTurnVel = Vector3.zero; if (0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.RIGHT)) { vTurnVel.x = -m_moving.MaxSpeed; } else if (0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.LEFT)) { vTurnVel.x = m_moving.MaxSpeed; } if (0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.DOWN)) { vTurnVel.y = m_moving.MaxSpeed; } else if (0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.UP)) { vTurnVel.y = -m_moving.MaxSpeed; } if (vTurnVel != Vector3.zero) { m_moving.ApplyForce(vTurnVel - m_moving.Veloc); } //--- //fix to avoid flickering of the creature when collides with wall if (Time.frameCount % 16 == 0) //--- { if (!LockAnimDir) { UpdateAnimDir(); } } }