Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

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:
    124
    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:
    939
    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. }