75 lines
1.9 KiB
C#
75 lines
1.9 KiB
C#
using Unity.Netcode;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
using Unity.Collections;
|
|
|
|
public class PlayerController : NetworkBehaviour
|
|
{
|
|
[SerializeField] private float moveSpeed = 5f;
|
|
|
|
[SerializeField] private Camera playerCamera;
|
|
|
|
[SerializeField] private float mouseSensitivity = 2f;
|
|
[SerializeField] private Transform cameraRoot;
|
|
|
|
float verticalRotation = 0f;
|
|
|
|
public override void OnNetworkSpawn()
|
|
{
|
|
if (playerCamera != null)
|
|
{
|
|
playerCamera.enabled = IsOwner;
|
|
playerCamera.GetComponent<AudioListener>().enabled = IsOwner;
|
|
}
|
|
|
|
if (IsOwner)
|
|
{
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
Cursor.visible = false;
|
|
}
|
|
|
|
if (IsOwner)
|
|
{
|
|
GetComponent<Renderer>().material.color = Color.blue;
|
|
}
|
|
else
|
|
{
|
|
GetComponent<Renderer>().material.color = Color.red;
|
|
}
|
|
|
|
if (IsOwner)
|
|
{
|
|
transform.position = new Vector3(Random.Range(-10f, 10f), 1f, Random.Range(-10f, 10f));
|
|
}
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
if(!IsOwner) return;
|
|
|
|
HandleMovement();
|
|
HandleRotation();
|
|
}
|
|
|
|
private void HandleMovement()
|
|
{
|
|
float _moveX = Input.GetAxis("Horizontal");
|
|
float _moveZ = Input.GetAxis("Vertical");
|
|
|
|
Vector3 move = transform.right * _moveX + transform.forward * _moveZ;
|
|
transform.position += move * moveSpeed * Time.deltaTime;
|
|
}
|
|
|
|
private void HandleRotation()
|
|
{
|
|
float _mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
|
|
float _mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity;
|
|
|
|
transform.Rotate(Vector3.up * _mouseX);
|
|
|
|
verticalRotation -= _mouseY;
|
|
verticalRotation = Mathf.Clamp(verticalRotation, -90f, 90f);
|
|
cameraRoot.localRotation = Quaternion.Euler(verticalRotation, 0f, 0f);
|
|
}
|
|
}
|