Hey so I am making a game with one Player prefab that clones itself for each player joined, the ball also has a camera that follows it but the problem I have is that it will follow the host but not the client, here is my script:
void LateUpdate()
{
// Find the player's ball based on ownership
if (NetworkManager.Singleton != null && NetworkManager.Singleton.ConnectedClients.TryGetValue(NetworkManager.Singleton.LocalClientId, out var networkClient))
{
var playerNetwork = networkClient.PlayerObject.GetComponent<PlayerNetwork>();
if (playerNetwork != null)
{
CameraTarget = playerNetwork.BallTransform;
}
}
// Check if we still have a valid target
if (CameraTarget != null)
{
if (CameraMode == CameraModes.Follow)
{
// Get the player's network script if it's available
playerNetwork = CameraTarget.GetComponent<PlayerNetwork>();
// Update desired position based on player's ball transform
if (playerNetwork != null)
{
desiredPosition = playerNetwork.BallTransform.position + Quaternion.Euler(ElevationAngle, OrbitalAngle, 0f) * new Vector3(0, 0, -FollowDistance);
}
}
else if (CameraMode == CameraModes.Isometric)
{
desiredPosition = CameraTarget.position + Quaternion.Euler(ElevationAngle, OrbitalAngle, 0f) * new Vector3(0, 0, -FollowDistance);
}
else
{
// Free Camera implementation
}
if (MovementSmoothing == true)
{
cameraTransform.position = Vector3.SmoothDamp(cameraTransform.position, desiredPosition, ref currentVelocity, MovementSmoothingValue * Time.fixedDeltaTime);
}
else
{
cameraTransform.position = desiredPosition;
}
if (RotationSmoothing == true)
cameraTransform.rotation = Quaternion.Lerp(cameraTransform.rotation, Quaternion.LookRotation(CameraTarget.position - cameraTransform.position), RotationSmoothingValue * Time.deltaTime);
else
{
cameraTransform.LookAt(CameraTarget);
}
}
GetPlayerInput();
// Find the player ball with the name "Player(Clone)"
GameObject playerBall = GameObject.Find("Player(Clone)");
// Check if we found the player ball
if (playerBall != null)
{
// Set the player ball as the CameraTarget
CameraTarget = playerBall.transform;
if (CameraMode == CameraModes.Isometric)
{
desiredPosition = CameraTarget.position + Quaternion.Euler(ElevationAngle, OrbitalAngle, 0f) * new Vector3(0, 0, -FollowDistance);
}
else if (CameraMode == CameraModes.Follow)
{
desiredPosition = CameraTarget.position + CameraTarget.TransformDirection(Quaternion.Euler(ElevationAngle, OrbitalAngle, 0f) * (new Vector3(0, 0, -FollowDistance)));
}
else
{
// Free Camera implementation
}
if (MovementSmoothing == true)
{
cameraTransform.position = Vector3.SmoothDamp(cameraTransform.position, desiredPosition, ref currentVelocity, MovementSmoothingValue * Time.fixedDeltaTime);
}
else
{
cameraTransform.position = desiredPosition;
}
if (RotationSmoothing == true)
cameraTransform.rotation = Quaternion.Lerp(cameraTransform.rotation, Quaternion.LookRotation(CameraTarget.position - cameraTransform.position), RotationSmoothingValue * Time.deltaTime);
else
{
cameraTransform.LookAt(CameraTarget);
}
}
}
I also have another problem, the host ball moves normal but when a client joins it is super fast. Please let me know why this is happening.