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

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

Detecting the player and preventing mobility

In order for the AI to shoot at the player, it needs to know where it is and how to aim at it. In this section, you will learn how to detect the player and make the AI aim at it:

Open the CPlayer.cpp file and navigate to the Update() method. Since the AI and player are sharing the same class, we need to branch the logic based on whether we are currently the AI or the player. If we are the AI, we need to aim our weapon at the player. It should look similar to the following:

//We Are AI
if( !m_bClient )
{
  //Get The Player.
  auto pPlayer = static_cast< CPlayer* >( gEnv->pGame->GetIGameFramework()->GetClientActor() );

  //Get The Direction We Would Need To Aim To Hit The Player.
  auto ShootDir = ( pPlayer->GetEntity()->GetWorldPos() - GetEntity()->GetWorldPos() ).normalized();

  //Get The Weapon's Entity.
  auto pWeaponEnt = m_pWeapon->GetEntity();
  if( pWeaponEnt )
  {
    //Point The Weapon At The Target.
    pWeaponEnt->SetRotation( quaternion::CreateRotationVDir( ShootDir ), ENTITY_XFORM_ROT );
  }

}

Notice that we get the direction the AI needs to aim at by getting the difference between the AI's position and the player's position. We then instruct the weapon to set its rotation to the direction that we calculated.

It is important for our game that the AI doesn't move around. Unfortunately, our Player class is set up to move when input is detected. To prevent the AI from moving, simply wrap the movement and rotation processing calls with an if statement. In the CPlayer class's Update() method, wrap the movement and rotation processing code with an if statement. It should look like this:

//Only Process Movement And Rotation If We Are NOT An AI (The Player).
if( m_bClient )
{
  //Process Player Movement.
  ProcessMovement( ctx.fFrameTime );

  //Process Player Rotation.
  ProcessRotation( ctx.fFrameTime );

}

Notice the m_bClient variable; it is the only variable that needs to be considered in order to detect between the player and AI.

主站蜘蛛池模板: 冕宁县| 马公市| 尖扎县| 綦江县| 乌鲁木齐县| 鄂州市| 开原市| 陇南市| 阳西县| 惠水县| 静安区| 德安县| 丹阳市| 鹤岗市| 克东县| 九江县| 天峨县| 霍山县| 斗六市| 叙永县| 海安县| 吉隆县| 广水市| 岳西县| 秀山| 扶余县| 色达县| 福建省| 民勤县| 普安县| 八宿县| 咸阳市| 师宗县| 威海市| 柏乡县| 瓮安县| 法库县| 南和县| 喀喇沁旗| 洞口县| 库尔勒市|