So the player who makes the server can move look anywhere do anything, so i added a second player and i could only look up and down not move or look anywhere else, here are my 3 scripts
PlayerController:
using UnityEngine;
[RequireComponent(typeof(PlayerMotor))]
public class PlayerController : MonoBehaviour
{
[SerializeField]
private float speed = 5f;
[SerializeField]
private float lookSensitivity = 3;
private PlayerMotor motor;
void Start()
{
motor = GetComponent<PlayerMotor>();
}
void Update()
{
float _xMov = Input.GetAxisRaw("Horizontal");
float _zMov = Input.GetAxisRaw("Vertical");
Vector3 _movHorizontal = transform.right * _xMov;
Vector3 _movVertical = transform.forward * _zMov;
// final movement vector
Vector3 _velocity = (_movHorizontal + _movVertical).normalized * speed;
//Apply movement
motor.Move(_velocity);
//Calculate rotation as a 3D vector (turning around)
float _yRot = Input.GetAxisRaw("Mouse X");
Vector3 _rotation = new Vector3(0f, +_yRot, 0f) * lookSensitivity;
//Apply rotation
motor.Rotate(_rotation);
//Calculate camera rotation as a 3D vector (turning around)
float _xRot = Input.GetAxisRaw("Mouse Y");
Vector3 _cameraRotation = new Vector3(_xRot, 0f, 0f) * lookSensitivity;
//Apply rotation
motor.RotateCamera(_cameraRotation);
}
}
Player Motor:
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerMotor : MonoBehaviour {
[SerializeField]
private Camera Playercamera;
private Vector3 velocity = Vector3.zero;
private Vector3 rotation = Vector3.zero;
private Vector3 cameraRotation = Vector3.zero;
private Rigidbody rb;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
// Gets a movement vector
public void Move(Vector3 _velocity)
{
velocity = _velocity;
}
// Gets a rotationl vector
public void Rotate(Vector3 _rotation)
{
rotation = _rotation;
}
// Gets a rotationl vector for the camera
public void RotateCamera(Vector3 _cameraRotation)
{
cameraRotation = _cameraRotation;
}
// Run every physics iteration
void FixedUpdate()
{
PerformMovement();
PerformRotation();
}
// Peform movement based on velocity variable
void PerformMovement()
{
if (velocity != Vector3.zero)
{
rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
}
}
//Perform rotation
void PerformRotation ()
{
rb.MoveRotation(rb.rotation * Quaternion.Euler (rotation));
if (Playercamera !=null)
{
Playercamera.transform.Rotate(-cameraRotation);
}
}
}
PlayerSetup:
using UnityEngine;
using UnityEngine.Networking;
using Mirror;
public class PlayerSetup : NetworkBehaviour {
[SerializeField]
Behaviour[] componentsToDisable;
Camera sceneCamera;
void Start()
{
if (!isLocalPlayer)
{
for (int i = 0; i < componentsToDisable.Length; i++)
{
componentsToDisable[i].enabled = false;
}
} else
{
sceneCamera = Camera.main;
if(sceneCamera != null)
{
sceneCamera.gameObject.SetActive(false);
}
}
}
void OnDisable()
{
if (sceneCamera != null)
{
sceneCamera.gameObject.SetActive(true);
}
}
}