Photon Pun 2 Multiplayer Controlling the Same Player

This page summarizes the projects mentioned and recommended in the original post on /r/Unity3D

InfluxDB - Power Real-Time Data Analytics at Scale
Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.
www.influxdata.com
featured
SaaSHub - Software Alternatives and Reviews
SaaSHub helps you find the best software and product alternatives
www.saashub.com
featured
  • Steamworks.NET

    Steamworks wrapper for Unity / C#

  • using Photon.Pun; using Photon.Realtime; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; using Hashtable = ExitGames.Client.Photon.Hashtable; //Steamworks.Net repo - https://github.com/rlabrecque/Steamworks.NET.git?path=/com.rlabrecque.steamworks.net#20.1.0 public class PlayerController : MonoBehaviourPunCallbacks, IDamageable { [SerializeField] Image healthbarImage; [SerializeField] GameObject ui; [SerializeField] GameObject cameraHolder; [SerializeField] float mouseSensitivity, sprintSpeed, walkSpeed, jumpForce, smoothTime; public Slider sensSlider; public GameObject musicSource; [SerializeField] Item[] items; int itemIndex; int previousItemIndex = -1; float verticalLookRotation; Vector3 smoothMoveVelocity; Rigidbody rb; PhotonView PV; const float maxHealth = 100f; float currentHealth = maxHealth; PlayerManager playerManager; GunSystem gunSystem; public AudioSource damageSound; [Header("Movement")] private float moveSpeed; public float groundDrag; [Header("Jumping")] public float jumpCooldown; public float airMultiplier; bool readyToJump; [Header("Keybinds")] public KeyCode sprintKey = KeyCode.LeftShift; [Header("Ground Check")] public float playerHeight; public LayerMask whatIsGround; bool grounded; [Header("Slope Handling")] public float maxSlopeAngle; private RaycastHit slopeHit; private bool exitingSlope; public Transform orientation; float horizontalInput; float verticalInput; Vector3 moveDirection; public float slip; public float noSlip; Collider collider; public MovementState state; public enum MovementState { walking, sprinting, air } void Awake() { PV = GetComponent(); rb = GetComponent(); collider = GetComponent(); } void Start() { playerManager = PhotonView.Find((int)PV.InstantiationData[0]).GetComponent(); if (PV.IsMine) { EquipItem(0); } else { Destroy(GetComponentInChildren().gameObject); Destroy(rb); Destroy(ui); Destroy(musicSource); } readyToJump = true; mouseSensitivity = PlayerPrefs.GetFloat("Sensitivity", mouseSensitivity); sensSlider.value = mouseSensitivity; } void Update() { grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround); if(!Pause.isPaused) { MyInput(); } if(!PV.IsMine) { return; } else { if (grounded) rb.drag = groundDrag; else rb.drag = 0; } if(!PV.IsMine) return; if(!Pause.isPaused) { Look(); } for(int i = 0; i < items.Length; i++) { if(Input.GetKeyDown((i + 1).ToString())) { ResetItemIndex(); EquipItem(i); items[i].GetComponent().enabled = true; break; } } if(Input.GetAxisRaw("Mouse ScrollWheel") > 0f) { if(itemIndex >= items.Length - 1) { ResetItemIndex(); EquipItem(0); items[0].GetComponent().enabled = true; } else { ResetItemIndex(); EquipItem(itemIndex + 1); items[itemIndex].GetComponent().enabled = true; } } else if(Input.GetAxisRaw("Mouse ScrollWheel") < 0f) { if(itemIndex <= 0) { ResetItemIndex(); EquipItem(items.Length - 1); items[items.Length - 1].GetComponent().enabled = true; } else { ResetItemIndex(); EquipItem(itemIndex - 1); items[itemIndex].GetComponent().enabled = true; } } items[itemIndex].Use(); if(transform.position.y < -3f) // Die if you fall out of the world { Die(); } } void FixedUpdate() { if (!PV.IsMine) return; if(!Pause.isPaused) { SpeedControl(); StateHandler(); MovePlayer(); NoSlip(); } } private void Look() { if (!PV.IsMine) return; transform.Rotate(Vector3.up * Input.GetAxisRaw("Mouse X") * mouseSensitivity); verticalLookRotation += Input.GetAxisRaw("Mouse Y") * mouseSensitivity; verticalLookRotation = Mathf.Clamp(verticalLookRotation, -90f, 90f); cameraHolder.transform.localEulerAngles = Vector3.left * verticalLookRotation; } private void MyInput() { if (!PV.IsMine) return; horizontalInput = Input.GetAxisRaw("Horizontal"); verticalInput = Input.GetAxisRaw("Vertical"); // when to jump if(Input.GetButtonDown("Jump") && readyToJump && grounded) { readyToJump = false; Jump(); Invoke(nameof(ResetJump), jumpCooldown); } } void NoSlip() { if(rb.velocity.x == 0 && rb.velocity.y == 0 && rb.velocity.z == 0) { collider.material.dynamicFriction = noSlip; collider.material.staticFriction = noSlip; } else { collider.material.dynamicFriction = slip; collider.material.staticFriction = slip; } } private void StateHandler() { // Mode - Sprinting if(grounded && Input.GetKey(sprintKey) || Input.GetButton("XBOX_LJP")) { state = MovementState.sprinting; moveSpeed = sprintSpeed; } // Mode - Walking else if (grounded) { state = MovementState.walking; moveSpeed = walkSpeed; } // Mode - Air else { state = MovementState.air; } } private void MovePlayer() { // calculate movement direction moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput; // on slope if (OnSlope() && !exitingSlope) { rb.AddForce(GetSlopeMoveDirection() * moveSpeed * 20f, ForceMode.Force); if (rb.velocity.y > 0) rb.AddForce(Vector3.down * 80f, ForceMode.Force); } // on ground else if(grounded) rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force); // in air else if(!grounded) rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force); // turn gravity off while on slope rb.useGravity = !OnSlope(); } private void SpeedControl() { // limiting speed on slope if (OnSlope() && !exitingSlope) { if (rb.velocity.magnitude > moveSpeed) rb.velocity = rb.velocity.normalized * moveSpeed; } // limiting speed on ground or in air else { Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z); // limit velocity if needed if (flatVel.magnitude > moveSpeed) { Vector3 limitedVel = flatVel.normalized * moveSpeed; rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z); } } } private void Jump() { exitingSlope = true; // reset y velocity rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z); rb.AddForce(transform.up * jumpForce, ForceMode.Impulse); } private void ResetJump() { readyToJump = true; exitingSlope = false; } private bool OnSlope() { if(Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHeight * 0.5f + 0.3f)) { float angle = Vector3.Angle(Vector3.up, slopeHit.normal); return angle < maxSlopeAngle && angle != 0; } return false; } private Vector3 GetSlopeMoveDirection() { return Vector3.ProjectOnPlane(moveDirection, slopeHit.normal).normalized; } void EquipItem(int _index) { if(_index == previousItemIndex) return; itemIndex = _index; items[itemIndex].itemGameObject.SetActive(true); if(previousItemIndex != -1) { items[previousItemIndex].itemGameObject.SetActive(false); } previousItemIndex = itemIndex; if(PV.IsMine) { Hashtable hash = new Hashtable(); hash.Add("itemIndex", itemIndex); PhotonNetwork.LocalPlayer.SetCustomProperties(hash); } } public override void OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps) { if(!PV.IsMine && targetPlayer == PV.Owner) { EquipItem((int)changedProps["itemIndex"]); } } public void TakeDamage(float damage) { PV.RPC("RPC_TakeDamage", RpcTarget.All, damage); } [PunRPC] void RPC_TakeDamage(float damage) { if(!PV.IsMine) return; damageSound.Play(); currentHealth -= damage; healthbarImage.fillAmount = currentHealth / maxHealth; if(currentHealth <= 0) { Die(); } } void Die() { playerManager.Die(); } public void Sensitivity() { mouseSensitivity = sensSlider.value; PlayerPrefs.SetFloat("Sensitivity", mouseSensitivity); } void ResetItemIndex() { for (int j = 0; j < items.Length; j++) { items[j].GetComponent().enabled = false; } } }

  • InfluxDB

    Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.

    InfluxDB logo
NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts

  • How do you get more than four controllers to work with Steam Input and the new input system?

    1 project | /r/Unity3D | 9 May 2023
  • Should I review my own game? (Still not published on Steam)

    1 project | /r/gamedev | 22 Aug 2022
  • Networking solution for a turn based strategy in Unity?

    1 project | /r/gamedev | 24 Jun 2022
  • Steamworks multiplayer - Godot vs Unity

    1 project | /r/godot | 14 Apr 2022
  • Steamworks.NET issue with "ExchangeItems"

    1 project | /r/Unity3D | 2 Apr 2022