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

Making a camera follow the player

At this point, I advise you to test our game in the editor. You can do this by right-clicking the solution in Visual Studio and clicking on Build. Once our game is compiled, you can launch the CRYENGINE editor as usual, and it will automatically load our game. It's wonderful. So far, we have a completely playable character, but what happens when our player moves out of the camera's view? As you can see, we need a way for the player to move around but always stay in view. We can easily accomplish this by creating a following camera. The steps are as follows:

  1. The CPlayer class has camera code that we will not be using, so we need to remove such code. To do this, modify the PostInit() method in the CPlayer class so that all the camera-related code is removed. It should look like this:
    void CPlayer::PostInit( IGameObject * pGameObject )
    {
      //Allow This Instance To Be Updated Every Frame.
      pGameObject->EnableUpdateSlot( this, 0 );
    
      //Allow This Instance To Be Post-Updated Every Frame.
      pGameObject->EnablePostUpdates( this );
    
      //Register For Game Object Events.
      RegisterForGOEvents();
    
      //If We Are The Client Actor Than Notify The Game Of Us Being Spawned.
      if( m_bClient )
        ( ( CGASGame* )gEnv->pGame )->OnClientActorSpawned( this );
    }

    Now that we have removed all the camera code, let's add in the appropriate code to simulate a following camera.

  2. At the very bottom of the ProcessMovement() method in the CPlayer class, add the following code:
    //Makes Sure That The Camera's Position Is Always At The Same X Position As The Player But Offset In The Y And Z Axis.
    m_pCamera->SetPos( pEntity->GetPos() + Vec3( 0, -10, 2 ) );
    
    //Make Sure To Always Look At The Player.
    m_pCamera->SetRot( Vec3( RAD2DEG( Ang3( Quat::CreateRotationVDir( pEntity->GetPos() - m_pCamera->GetPos() ) ) ) ) );
  3. The default starter-kit code has the camera following the player pretty quickly. In GAS, we want the camera to follow the player more slowly, which adds a nice cinematic effect. To have a more lazy camera, let's modify the following function calls in the Init() method of the CPlayer class.

    We will change the following snippet:

    //Sets The Movement Speed Of The Player Camera.
    m_pCamera->SetMovementSpeed( 10.0f );
    
    //Sets The Rotation Speed Of The Player Camera.
    m_pCamera->SetRotationSpeed( 10.0f );

    Let's replace the previous snippet with this:

    //Sets The Movement Speed Of The Player Camera.
    m_pCamera->SetMovementSpeed( 2.5f );
    
    //Sets The Rotation Speed Of The Player Camera.
    m_pCamera->SetRotationSpeed( 2.5f );

The big picture

Let's look at the big picture so that we can get a clear view of the full flow and geography of the code. In every frame, the player's movement is updated. In order to keep the player in view, we tell the camera to follow the player's x position, while maintaining a constant offset in the y and z axis. To keep the player at the center of the screen, we instruct the camera to always look at the player. This concludes the section on making a camera follow the player.

主站蜘蛛池模板: 司法| 台前县| 承德县| 靖边县| 灵川县| 菏泽市| 乌拉特中旗| 济南市| 沙河市| 丹东市| 迭部县| 毕节市| 阿拉善右旗| 长顺县| 平果县| 外汇| 贡觉县| 博野县| 庐江县| 广饶县| 延津县| 丹寨县| 苏尼特左旗| 会昌县| 科技| 巴彦淖尔市| 宜宾县| 北碚区| 乐业县| 韩城市| 龙游县| 新化县| 马边| 鄢陵县| 通渭县| 阿尔山市| 怀宁县| 新闻| 轮台县| 呈贡县| 霍州市|