Player Controller for Unity Photon Pun 2
I have made this controller for a project in unity... actually the players can join the room and are instantiaded with photonnetwork.instantiate when joining the room but the controls and the cameras are messed up, i can control one player but seeing it from the other players camera.... any ideas what im doing wrong?
using UnityEngine; using Photon.Pun; public class Player : MonoBehaviourPun { public float speed = 3f; public float jumpHeight = 2f; public float rotSpeed = 400f; public float gravity = -9.81f; public Transform camPivot; public Transform shootOrigin; public Camera cam; private float xRot = 0f; private float verticalSpeed = 0f; private CharacterController controller; void Start() { TryGetComponent(out controller); Cursor.lockState = CursorLockMode.Locked; if (!photonView.IsMine) { cam.enabled = false; } } void Update() { if (photonView.IsMine) { MovementAndRotation(); if (Input.GetKeyDown(KeyCode.Space)) { Jump(); } } } void Jump() { verticalSpeed = Mathf.Sqrt(-2f * gravity * jumpHeight); } void MovementAndRotation() { Vector3 _input = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical")); _input = transform.TransformDirection(_input); verticalSpeed += gravity * Time.deltaTime; Vector3 _verticalMovement = new Vector3(0f, verticalSpeed, 0f); float _speedMultiplier = Input.GetKey(KeyCode.LeftShift) ? 2f : 1f; controller.Move((_input.normalized * speed * _speedMultiplier + _verticalMovement) * Time.deltaTime); transform.Rotate(0f, Input.GetAxisRaw("Mouse X") * rotSpeed * Time.deltaTime, 0f); xRot -= Input.GetAxisRaw("Mouse Y") * rotSpeed * Time.deltaTime; xRot = Mathf.Clamp(xRot, -90f, 90f); camPivot.localEulerAngles = new Vector3(xRot, camPivot.localEulerAngles.y, camPivot.localEulerAngles.z); if (photonView.IsMine) { photonView.RPC("SyncTransform", RpcTarget.OthersBuffered, transform.position, transform.rotation); } } [PunRPC] void SyncTransform(Vector3 _position, Quaternion _rotation) { transform.position = _position; transform.rotation = _rotation; } private void OnValidate() { if (verticalSpeed <= 0f) { verticalSpeed = 0f; } } }
I tried enabling and disabling the cameras with different methods, i tried "assigning" a camera to each player but all the time its the same.
Answers
-
Just an idea...I'm still kind of new but this forum is pretty dead and I'd hate to see people walk away ^^.
What I did but I will eventually change is just have the camera as a child of the player controller. And in the player controller just manually assign in the inspector the camera you want to use.
Eventually I am going to change the above to probably the PlayerManager instanitating the player controller as well as their own camera.
Hope this helps. I'll try and respond!
0