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?
I have the same doubt, and I want to create a raycast from the camera position.
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).
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.
Update camera’s position from the player entity.
[UpdateInWorld(UpdateInWorld.TargetWorld.Client)]
public class HybridMainCameraFollowPlayerSystem : SystemBase
{
protected override void OnUpdate()
{
// Camera position default.
var position = Camera.main.transform.position;
// Get the player entity.
var commandTargetComponentEntity = GetSingletonEntity<CommandTargetComponent>();
var commandTargetComponent = GetComponent<CommandTargetComponent>(commandTargetComponentEntity);
Entities.
WithAll<PlayerComponent>().
ForEach(
(Entity entity, in Translation translation) =>
{
// Only update when the player is the one that is controlled by the client's player.
if (entity == commandTargetComponent.targetEntity)
{
position.x = translation.Value.x;
// Leave y be.
position.z = translation.Value.z;
}
}
).
Run();
Camera.main.transform.position = position;
}
}