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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.229.111