This post has been wikified, any user with enough reputation can edit it.
Question by
Yalcin2 · Jun 14, 2018 at 08:10 PM ·
multiplayerunity 2dphotonmultiplayer-networkingunity multiplayer
Unity Photon how to show arm/gun movement with mouse to other players.,Unity Photon: How to show arm movement to other players
I am currently trying to make a multiplayer game to further my knowledge on C#. When I join the game/room, my player`s gun, which is stuck to the player, is rotating with the mouse as an input. The problem is showing the other players on the photon network the gun rotation of my player.
Script (attached to the player`s gun):
public PlayerMove playerMove;
Vector3 difference;
// Update is called once per frame
void Update () {
if (playerMove.photonView.isMine)
{
RotateArm();
}
else
{
}
}
private void RotateArm()
{
difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
difference.Normalize();
float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(rotZ , Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, 5f * Time.deltaTime);
}
I cannot figure out and understand what I need to put inside the else statement inside the update... Any help would be appreciated thanks :)
Comment
Best Answer
Answer by Yalcin2 · Jun 15, 2018 at 02:09 PM
UPDATE: I managed to fix my problem by adding a script which streams and receives the gun`s rotation from the client player to the other players.
public PlayerMove playerMove;
private int rotationOffset = 0;
private Quaternion gunPos;
private Vector3 difference;
// Update is called once per frame
void Update () {
if (playerMove.photonView.isMine)
{
RotateArm();
}
else
{
SmoothNetMovement();
}
}
private void RotateArm()
{
difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
difference.Normalize();
float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotZ + rotationOffset);
//Quaternion rotation = Quaternion.AngleAxis(rotZ , Vector3.forward);
//transform.rotation = Quaternion.Slerp(transform.rotation, rotation, 5f * Time.deltaTime);
}
private void SmoothNetMovement()
{
transform.rotation = Quaternion.Lerp(transform.rotation, gunPos, Time.deltaTime * 8);
}
private void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(transform.rotation);
}
else
{
gunPos = (Quaternion)stream.ReceiveNext();
}
}