Commit e031d6f9 by alsunj

restore camera spawning

parent bfefbe74
......@@ -17,7 +17,6 @@ partial struct GoInGameClientSystem : ISystem
entityQueryBuilder.Dispose();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
EntityCommandBuffer entityCommandBuffer = new EntityCommandBuffer(Unity.Collections.Allocator.Temp);
......@@ -28,9 +27,15 @@ partial struct GoInGameClientSystem : ISystem
<RefRO<NetworkId>>().WithNone<NetworkStreamInGame>().WithEntityAccess())
{
entityCommandBuffer.AddComponent<NetworkStreamInGame>(entity);
Debug.Log("Connected!" + entity + "::" + networkId.ValueRO.Value);
Entity rpcEntity = entityCommandBuffer.CreateEntity();
GameObject playerCameraGO = new GameObject($"Camera{networkId.ValueRO.Value}");
playerCameraGO.AddComponent<Camera>();
// Assign the player entity to FollowPlayer script
FollowPlayer followScript = playerCameraGO.AddComponent<FollowPlayer>();
followScript.networkId = networkId.ValueRO.Value; // Store networkId instead of dire
entityCommandBuffer.AddComponent<GoInGameRequestRpc>(rpcEntity);
entityCommandBuffer.AddComponent<SendRpcCommandRequest>(rpcEntity);
}
......
......@@ -21,8 +21,6 @@ partial struct NetcodePlayerMovementSystem : ISystem
RefRW<PlayerSprintData>>()
.WithAll<Simulate>())
{
Debug.Log("Updating player movement and camera follow");
float3 moveVector = new float3(netcodePlayerInput.ValueRO.inputVector.x, 0,
netcodePlayerInput.ValueRO.inputVector.y);
float moveSpeed = sprintData.ValueRO.isSprinting
......
fileFormatVersion: 2
guid: e1235eb5f633291448b5153c24f6076b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using Unity.Entities;
using Unity.Transforms;
using Unity.Mathematics;
using Unity.NetCode;
public class FollowPlayer : MonoBehaviour
{
public int networkId; // Store NetworkId instead of direct entity reference
private EntityManager entityManager;
private Entity targetEntity;
private bool entityFound = false;
void Start()
{
entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
transform.rotation = Quaternion.Euler(40, 0, 0);
}
void LateUpdate()
{
// If entity hasn't been found yet, try to find it
if (!entityFound)
{
// Find the player entity that matches this camera's networkId
var query = entityManager.CreateEntityQuery(typeof(GhostOwner), typeof(LocalTransform));
var entities = query.ToEntityArray(Unity.Collections.Allocator.Temp);
foreach (var entity in entities)
{
var ghostOwner = entityManager.GetComponentData<GhostOwner>(entity);
if (ghostOwner.NetworkId == networkId)
{
targetEntity = entity;
entityFound = true;
break;
}
}
entities.Dispose();
}
// If entity found, update camera position
if (entityFound && entityManager.Exists(targetEntity))
{
LocalTransform playerTransform = entityManager.GetComponentData<LocalTransform>(targetEntity);
float3 playerPosition = playerTransform.Position;
// Define smooth damp velocity
Vector3 velocity = Vector3.zero;
// Use SmoothDamp for a more natural movement transition
transform.position = Vector3.SmoothDamp(transform.position, playerPosition + new float3(0, 7, -9),
ref velocity, 0.05f);
// Use Slerp for smoother rotation towards player
// Quaternion targetRotation = Quaternion.LookRotation((Vector3)playerPosition - transform.position);
// transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 5f);
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: f45e368f34c208043ab6110995f28d03
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment