// Update is called once per frame void Update() { m_pathFindingBehaviour.TargetPos = m_player.transform.position; Vector3 vTarget = m_player.transform.position; vTarget.z = transform.position.z; // stop folling the path when closed enough to target m_pathFindingBehaviour.enabled = (vTarget - transform.position).magnitude > MinDistToReachTarget; if (!m_pathFindingBehaviour.enabled) { m_fAngOff += Random.Range(-AngRandOff, AngRandOff); Vector3 vOffset = Quaternion.AngleAxis(m_fAngOff, Vector3.forward) * (AngRandRadious * Vector3.right); vTarget += vOffset; Debug.DrawLine(transform.position, m_player.transform.position, Color.blue); Debug.DrawRay(m_player.transform.position, vOffset, Color.blue); m_moving.Arrive(vTarget); } //+++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(); } } }
// Update is called once per frame void Update() { m_pathFindingBehaviour.TargetPos = m_player.transform.position; Vector3 vTarget = m_player.transform.position; vTarget.z = transform.position.z; Ray2D sightRay = new Ray2D(transform.position, vTarget - transform.position); float distToTarget = (vTarget - transform.position).magnitude; float fSightBlockedDist = IsSightBlockedByBlockedTiles? RpgMapHelper.Raycast(sightRay, distToTarget) : -1f; // NOTE: fSightBlockedDist will be -1f if sight line is not blocked by blocked collision tile if (distToTarget >= SightDistance || fSightBlockedDist >= 0f) { // Move around m_pathFindingBehaviour.enabled = false; vTarget = transform.position; m_fAngOff += Random.Range(-AngRandOff, AngRandOff); Vector3 vOffset = Quaternion.AngleAxis(m_fAngOff, Vector3.forward) * (AngRandRadious * Vector3.right); vTarget += vOffset; m_moving.Arrive(vTarget); } else // Follow the player { // stop following the path when closed enough to target m_pathFindingBehaviour.enabled = (vTarget - transform.position).magnitude > MinDistToReachTarget; if (!m_pathFindingBehaviour.enabled) { m_fAngOff += Random.Range(-AngRandOff, AngRandOff); Vector3 vOffset = Quaternion.AngleAxis(m_fAngOff, Vector3.forward) * (AngRandRadious * Vector3.right); vTarget += vOffset; Debug.DrawLine(transform.position, m_player.transform.position, Color.blue); Debug.DrawRay(m_player.transform.position, vOffset, Color.blue); m_moving.Arrive(vTarget); } } //+++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(); } } }