Hi, I have a question about my script,
I was wondering if I needed to use an [RPC] to show the other players and the server that my player is cloaking, but every time I cloak it uses the energy and other players can’t see me cloak, I know that the “Graphics” disappear because the shadow disappears on the client’s computer but for other players nothing happens.
Here is the script :
using UnityEngine;
using System.Collections;
/// <summary>
/// This script is attached to the player.
///
/// This script accesses the BlinkEnergy script.
/// </summary>
public class Cloaking : MonoBehaviour {
// <Variables>
// Particle effect.
public GameObject cloakEffect;
// Time cloak lasts for
private float activeTime = 6;
// Quick reference
private Transform playerGraphics;
private Transform myTransform;
// Used to affect the energy.
private BlinkEnergy energyScript;
private float cloakCost = 80;
// Determine where the cloakEffect should be instantiated.
private Vector3 startPosition = new Vector3();
// <Variables END>
// Use this for initialization
void Start () {
if (GetComponent<NetworkView> ().isMine == true) {
myTransform = transform;
playerGraphics = myTransform.FindChild ("Graphics");
// Access the BlinkEnergy script.
energyScript = myTransform.GetComponent<BlinkEnergy> ();
}
else
{
enabled = false;
}
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Cloak") && Cursor.visible == false && energyScript.blink >= cloakCost)
{
energyScript.blink = energyScript.blink - cloakCost;
startPosition = myTransform.position;
DisableAndStartTimer();
}
}
void DisableAndStartTimer()
{
//Disable the player's graphics so that they dissappear for a moment.
playerGraphics.GetComponent<Renderer>().enabled = false;
//Run a Coroutine that will limit the time that the player can go invisible for.
StartCoroutine(RunForAMoment());
}
IEnumerator RunForAMoment ()
{
yield return new WaitForSeconds(activeTime);
//Enable the graphics so that the player can be seen again.
playerGraphics.GetComponent<Renderer>().enabled = true;
//Send out an RPC across the network so that everyone sees the cloak effect.
GetComponent<NetworkView>().RPC("CloakEffect", RPCMode.OthersBuffered, startPosition);
}
[RPC]
void CloakEffect (Vector3 startPos)
{
Instantiate(cloakEffect, startPos, Quaternion.identity);
}
}