fgamez1020
fgamez10202mo ago

Player positions

In Unity I have a multiplayer VR game where I have an arena or 4-sided table, it is a game that uses , when the game is created the person who created it appears within the game as an avatar, when the others 3 players join and they also have their own avatars, I want to know how I can show an avatar on each side of the table and make it like this in the local view of each player.
3 Replies
kapa.ai
kapa.ai2mo ago
Hi @fgamez1020. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To implement player positioning around a table in a multiplayer VR game using Normcore, you can utilize the RealtimeAvatarManager and custom positioning logic. Here's an approach to achieve this: 1. Use RealtimeAvatarManager to handle avatar instantiation: The RealtimeAvatarManager is responsible for creating avatars for each player in the room RealtimeAvatarManager documentation (https://normcore.io/documentation/realtime/xr/avatars). You can use this to instantiate avatars as players join. 2. Create a custom script to manage player positions: Implement a script that assigns positions to players as they join. This script should: - Keep track of available positions around the table - Assign a position to each player as they join - Update the local player's view to always see themselves at their assigned position 3. Modify the avatar instantiation process: Instead of letting RealtimeAvatarManager place avatars automatically, you'll want to control their positions. You can do this by: - Creating a custom prefab for your player avatar - Implementing a custom PlayerManager script that handles avatar instantiation and positioning Here's a basic example of how your PlayerManager script might look:
using UnityEngine;
using Normal.Realtime;

public class PlayerManager : MonoBehaviour
{
[SerializeField] private GameObject _camera = default;
[SerializeField] private Transform[] _playerPositions;

private Realtime _realtime;
private int _currentPlayerIndex = 0;

private void Awake()
{
_realtime = GetComponent<Realtime>();
_realtime.didConnectToRoom += DidConnectToRoom;
}
private void DidConnectToRoom(Realtime realtime)
{
GameObject playerGameObject = Realtime.Instantiate(
prefabName: "Player",
ownedByClient: true,
preventOwnershipTakeover: true,
useInstance: realtime
);

Player player = playerGameObject.GetComponent<Player>();

// Assign player position
if (_currentPlayerIndex < _playerPositions.Length)
{
player.transform.position = _playerPositions[_currentPlayerIndex].position;
player.transform.rotation = _playerPositions[_currentPlayerIndex].rotation;
_currentPlayerIndex++;
}

// Set up camera
ParentConstraint cameraConstraint = _camera.GetComponent<ParentConstraint>();
ConstraintSource constraintSource = new ConstraintSource { sourceTransform = player.cameraTarget, weight = 1.0f };
int constraintIndex = cameraConstraint.AddSource(constraintSource);

// Adjust camera position based on player's position
Vector3 cameraOffset = CalculateCameraOffset(player.transform.position);
cameraConstraint.SetTranslationOffset(constraintIndex, cameraOffset);
cameraConstraint.SetRotationOffset(constraintIndex, new Vector3(15.0f, 0.0f, 0.0f));
}

private Vector3 CalculateCameraOffset(Vector3 playerPosition)
{
// Calculate and return appropriate camera offset based on player position
// This will ensure each player sees the table from their perspective
// Implement your own logic here based on your table layout
}
}
using UnityEngine;
using Normal.Realtime;

public class PlayerManager : MonoBehaviour
{
[SerializeField] private GameObject _camera = default;
[SerializeField] private Transform[] _playerPositions;

private Realtime _realtime;
private int _currentPlayerIndex = 0;

private void Awake()
{
_realtime = GetComponent<Realtime>();
_realtime.didConnectToRoom += DidConnectToRoom;
}
private void DidConnectToRoom(Realtime realtime)
{
GameObject playerGameObject = Realtime.Instantiate(
prefabName: "Player",
ownedByClient: true,
preventOwnershipTakeover: true,
useInstance: realtime
);

Player player = playerGameObject.GetComponent<Player>();

// Assign player position
if (_currentPlayerIndex < _playerPositions.Length)
{
player.transform.position = _playerPositions[_currentPlayerIndex].position;
player.transform.rotation = _playerPositions[_currentPlayerIndex].rotation;
_currentPlayerIndex++;
}

// Set up camera
ParentConstraint cameraConstraint = _camera.GetComponent<ParentConstraint>();
ConstraintSource constraintSource = new ConstraintSource { sourceTransform = player.cameraTarget, weight = 1.0f };
int constraintIndex = cameraConstraint.AddSource(constraintSource);

// Adjust camera position based on player's position
Vector3 cameraOffset = CalculateCameraOffset(player.transform.position);
cameraConstraint.SetTranslationOffset(constraintIndex, cameraOffset);
cameraConstraint.SetRotationOffset(constraintIndex, new Vector3(15.0f, 0.0f, 0.0f));
}

private Vector3 CalculateCameraOffset(Vector3 playerPosition)
{
// Calculate and return appropriate camera offset based on player position
// This will ensure each player sees the table from their perspective
// Implement your own logic here based on your table layout
}
}
This script is based on the example from the Creating a Player Controller guide (https://normcore.io/documentation/guides/creating-a-player-controller#making-it-multiplayer), but modified to handle custom positioning. Remember to: 1. Set up your _playerPositions array in the Unity Inspector with Transform objects representing each position around the table. 2. Implement the CalculateCameraOffset method to ensure each player's camera is positioned correctly relative to their avatar and the table. 3. Adjust the RealtimeAvatarManager settings to prevent it from automatically spawning avatars, as you're handling this manually. This approach should allow you to position players around the table and ensure each player sees the game from their perspective. You may need to fine-tune the positioning and camera settings based on your specific game layout and requirements. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@1053407831916822528> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Mechabit
Mechabit2mo ago
easy method is to store an array of spawn points and then use clientID to spawn the avatar in that position simple as this void Start(){ if(realtimeView.isOwnedLocallySelf) transform.position = spawnPoints[realtime.clientID].position)}

Did you find this page helpful?