- Home /
Help with player variables (prefab/network)
Hi guys, i'm doing a multiplayer game and i have some problems when i try to access to the other player variables. I can't read anything of this variables cause their returns me 0 always.
This is caused because i have to configure it with [clientrpc] or network.rpc? How i can get the varibles of the other player? I don't understand it, someone can explain me how i should do it?
There is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class MovementMulti : NetworkBehaviour {
private float aumentoA = 0.1f;
[Header("Movement Variables")]
[SerializeField]
public float vX;
[SerializeField]
public float vZ;
[Header("Camera Position Variables")]
[SerializeField]
float cameraHeight = 15f;
Rigidbody rg;
public int id;
bool bounced = true;
public float rX;
public float rZ;
public float potencia;
Transform mainCamera;
Vector3 cameraOffset;
// Use this for initialization
void Start()
{
if (isLocalPlayer) {
cameraOffset = new Vector3(0f, cameraHeight, 0f);
mainCamera = Camera.main.transform;
MoveCamera();
rg = GetComponent<Rigidbody>();
id = Random.Range(0, 1000000000);
}
}
void MoveCamera() {
mainCamera.position = transform.position;
mainCamera.rotation = transform.rotation;
mainCamera.Translate(cameraOffset);
mainCamera.LookAt(transform);
}
private void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Player")
{
Debug.Log(this.gameObject.name);
Debug.Log(other.gameObject.name);
if (id > other.gameObject.GetComponent<MovementMulti>().id && bounced) // <--IT RETURNS 0
{
bounced = false;
float aux = vZ;
float aux2 = vX;
vZ = other.gameObject.GetComponent<MovementMulti>().vZ;//IT RETURNS 0
vX = other.gameObject.GetComponent<MovementMulti>().vX; //IT RETURNS 0
other.gameObject.GetComponent<MovementMulti>().vZ = aux;
other.gameObject.GetComponent<MovementMulti>().vX = aux2;
}
}
}
private void OnCollisionExit(Collision other) {
bounced = true;
}
// Update is called once per frame
void Update()
{
float x = Input.acceleration.x; //x negativa izquierda
float y = Input.acceleration.y; //y negativa abajo
float aumentoX = aumentoA * x;
float aumentoY = aumentoA * y;
if (x > 0)
{
vX += aumentoX;
}
else if (x < 0)
{
vX += aumentoX;
}
if (y > 0)
{ // Up
vZ += aumentoY;
}
else if (y < 0)
{ // Down
vZ += aumentoY;
}
//Dependiendo de los valores del eje se moverá de una forma u o de otra.
transform.Translate(Vector3.right * vX * Time.deltaTime);
transform.Translate(Vector3.forward * vZ * Time.deltaTime);
MoveCamera();
}
}
Answer by XxSimBaaxX · May 22, 2017 at 07:27 AM
I am not that good in terms of scripting, but I think your problem is that the variable's values only change at the local player only and are not synchronizing with the host and then to the clients, you probably have to add the [SyncVar] at every variable you want to synchronize with the host and then add a a [ClientRpc] function that is called every frame and sync the values again but now from the host to the rest of the players, that will create a loop which first syncs the variables from the local player to the host and then from the host back to the rest of the clients. note that you cant call [ClientRpc] on the local player, you can only call it from the host (you can add an if statement for isServer is true). sorry if its a bit complicated I am bad at explaining xD