Search Unity

Question How to attach a camera to a ghost/prefab when using Unity NetCode?

Discussion in 'NetCode for ECS' started by Edvard-D, May 24, 2020.

  1. Edvard-D

    Edvard-D

    Joined:
    Jun 14, 2012
    Posts:
    129
    I'm working on a first person game and want the camera's perspective to be based on the player entity's location. Adding a camera component doesn't seem to work, I'm guessing because it's not a DOTS component? What's the best way to go about this?
     
    efpoman likes this.
  2. efpoman

    efpoman

    Joined:
    Mar 24, 2020
    Posts:
    6
    I have the same doubt, and I want to create a raycast from the camera position.
     
  3. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992
    Did you have a look at teh DOTS Sample (https://unity.com/fr/releases/2019-3/dots-sample) ?

    I think that the Hybrid Renderer (V2 ?) supports the camera.

    The idea I think is to let your camera as a game object and copy the position of the player entity (or a child to position the camera where your want) every frame in a monobehavior update (with entity manager and on the main thread).
     
  4. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    This is the code I currently use in a multiplayer game. It's very simple, but works, and is probably a good starting point for fancier stuff. It simply takes the player's translation and updates the Camera.main's position. (Note: It's a top-down game, which is why I only update x and z). If your game is not multiplayer, remove all client/command target component stuff.
    Code (CSharp):
    1. [UpdateInWorld(UpdateInWorld.TargetWorld.Client)]
    2. public class HybridMainCameraFollowPlayerSystem : SystemBase
    3. {
    4.     protected override void OnUpdate()
    5.     {
    6.         // Camera position default.
    7.         var position = Camera.main.transform.position;
    8.  
    9.         // Get the player entity.
    10.         var commandTargetComponentEntity = GetSingletonEntity<CommandTargetComponent>();
    11.         var commandTargetComponent       = GetComponent<CommandTargetComponent>(commandTargetComponentEntity);
    12.  
    13.         Entities.
    14.             WithAll<PlayerComponent>().
    15.             ForEach(
    16.                     (Entity entity, in Translation translation) =>
    17.                     {
    18.                         // Only update when the player is the one that is controlled by the client's player.
    19.                         if (entity == commandTargetComponent.targetEntity)
    20.                         {
    21.                             position.x = translation.Value.x;
    22.                             // Leave y be.
    23.                             position.z = translation.Value.z;
    24.                         }
    25.                     }
    26.                    ).
    27.             Run();
    28.  
    29.         Camera.main.transform.position = position;
    30.     }
    31. }