Files
Test-Multiplayer/Assets/Scripts/PlayerScript.cs
2026-01-16 12:16:18 +02:00

121 lines
4.8 KiB
C#

using Mirror;
using UnityEngine;
namespace QuickStart
{
public class PlayerScript : NetworkBehaviour
{
public TextMesh playerNameText;
public GameObject floatingInfo;
private Material playerMaterialClone;
[SyncVar(hook = nameof(OnNameChanged))]
public string playerName;
[SyncVar(hook = nameof(OnColorChanged))]
public Color playerColor = Color.white;
void OnNameChanged(string _Old, string _New)
{
playerNameText.text = playerName;
}
void OnColorChanged(Color _Old, Color _New)
{
playerNameText.color = _New;
playerMaterialClone = new Material(GetComponent<Renderer>().material);
playerMaterialClone.color = _New;
GetComponent<Renderer>().material = playerMaterialClone;
}
public override void OnStartLocalPlayer()
{
Camera.main.transform.SetParent(transform);
Camera.main.transform.localPosition = new Vector3(0, 0, 0);
floatingInfo.transform.localPosition = new Vector3(0, -0.3f, 0.6f);
floatingInfo.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
string name = "Player" + Random.Range(100, 999);
Color color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
CmdSetupPlayer(name, color);
}
[Command]
public void CmdSetupPlayer(string _name, Color _col)
{
// player info sent to server, then server updates sync vars which handles it on all clients
playerName = _name;
playerColor = _col;
}
/*
PSEUDOCODE / PLAN (detailed):
1. If this is not the local player:
- Keep existing behavior: make the floatingInfo look at the main camera every frame.
- Return early so non-local players do not process input or move.
2. If this is the local player:
- Read keyboard input from the Unity Input System (preferred):
- Use UnityEngine.InputSystem.Keyboard.current to check key states.
- Map WASD (and arrow keys as optional) to movement:
- W or UpArrow -> move forward (+z)
- S or DownArrow -> move backward (-z)
- A or LeftArrow -> rotate left (-y)
- D or RightArrow -> rotate right (+y)
- Movement magnitudes:
- Rotation speed: 110 degrees per second (same as original)
- Forward/back speed: 4 units per second (same as original)
- Multiply by Time.deltaTime for frame-rate independence.
- If the Input System is not available (Keyboard.current == null), fall back to the legacy Input.GetAxis calls to preserve behavior on setups that haven't enabled the new system.
- Apply transform.Rotate for yaw (y-axis) using computed rotation value.
- Apply transform.Translate for forward/back movement in local Z using computed move value.
3. Keep the method minimal and efficient:
- Avoid allocations in Update.
- Use direct boolean checks (isPressed) on the keyboard.
- Preserve original movement multipliers and semantics.
*/
void Update()
{
Debug.Log("Update");
if (!isLocalPlayer)
{
// make non-local players run this
if (Camera.main != null)
floatingInfo.transform.LookAt(Camera.main.transform);
return;
}
float rotationDelta = 0f;
float forwardDelta = 0f;
var kb = UnityEngine.InputSystem.Keyboard.current;
if (kb != null)
{
// Rotation: A/D or Left/Right arrows
if (kb.aKey.isPressed || kb.leftArrowKey.isPressed) rotationDelta = -1f;
if (kb.dKey.isPressed || kb.rightArrowKey.isPressed) rotationDelta = 1f;
// Forward/Backward: W/S or Up/Down arrows
if (kb.wKey.isPressed || kb.upArrowKey.isPressed) forwardDelta = 1f;
if (kb.sKey.isPressed || kb.downArrowKey.isPressed) forwardDelta = -1f;
rotationDelta *= Time.deltaTime * 110.0f; // degrees per second
forwardDelta *= Time.deltaTime * 4.0f; // units per second
}
else
{
// Fallback to legacy input if the new Input System isn't available
rotationDelta = Input.GetAxis("Horizontal") * Time.deltaTime * 110.0f;
forwardDelta = Input.GetAxis("Vertical") * Time.deltaTime * 4.0f;
}
transform.Rotate(0f, rotationDelta, 0f, Space.Self);
transform.Translate(0f, 0f, forwardDelta, Space.Self);
}
}
}