官术网_书友最值得收藏!

  • CRYENGINE Game Development Blueprints
  • Richard Gerard Marcoux III Chris Goodswen Riham Toulan Sam Howels
  • 243字
  • 2021-07-16 20:21:18

Shooting the player

Now that we have added player detection to our AI, it is ready to shoot at the player now. To accomplish this, we will need to use a method that gets called every n seconds so as to prevent shooting rapidly during every frame. Luckily, for the purposes of this book, I've created a lightweight callback timer that doesjust this. So let's get started.

Open the CPlayer.cpp file and navigate to the PostInit() method. Add a callback method to the timer that shoots the player. It should look similar to the following:

//We Are AI
if( !m_bClient )
{
  //Add Callback To Timer.  Try To Shoot The Player Every 2 Seconds.
  m_CallBackTimer += [ this ] ( double dDelta )
  {
    //Get The Player
    auto pPlayer = gEnv->pGame->GetIGameFramework()->GetClientActor();

    //Get The Distance The We Are From The Player/
    auto Len = GetEntity()->GetWorldPos().GetDistance( pPlayer->GetEntity()->GetWorldPos() );

    //If We Are Less Than 15 Meters, Then Shoot The Player.
    if( Len < 15 )
    {
      //Unlimited Ammo For AI.
      m_pWeapon->AddAmmo( 1 );

      //Shoot At The Target, With A Bullet Speed Multiplier Of 10.
      m_pWeapon->Shoot( 10 );
    }
  };
}

Notice how we first check to see if we are an AI. We then proceed to add a callback method in the form of a C++11 Lambda, which will get called every time the timer reaches its interval (which is currently set at 2 seconds). In the callback, we simply add ammo to the AI to give it unlimited ammunition and shoot the weapon.

主站蜘蛛池模板: 阜康市| 江源县| 平舆县| 余庆县| 常山县| 阿图什市| 濮阳市| 韩城市| 奈曼旗| 柳州市| 盐山县| 十堰市| 古丈县| 米易县| 从江县| 保亭| 新河县| 侯马市| 丰城市| 开封县| 阳新县| 锦州市| 阜新市| 宁城县| 稻城县| 赫章县| 高清| 甘谷县| 油尖旺区| 尼勒克县| 剑阁县| 佛冈县| 怀化市| 木兰县| 墨玉县| 全州县| 西城区| 乌兰县| 新野县| 伊宁县| 弋阳县|