- Home /
Multiplayer Moving Bullet
Hi, I am trying to make a simple multiplayer bullet that moves through space, the graphics are working, anyway, the player isn't getting destroyed when he gets hit: using System.Collections;
public class LaserScript : MonoBehaviour {
public float velocity;
public float radius;
public float distance;
public GameObject particles;
public MeshRenderer[] meshes;
public Light laserLight;
void Start () {
Destroy(gameObject, 10f);
rigidbody.velocity = transform.forward * velocity;
if (!networkView.isMine) {
enabled = false;
}
}
void Update () {
SphereCastForward();
}
[RPC]
void SphereCastForward () {
RaycastHit hit;
if (Physics.SphereCast(transform.position, radius, transform.forward, out hit, distance)) {
foreach (MeshRenderer mesh in meshes) {
mesh.enabled = false;
}
laserLight.enabled = false;
Instantiate(particles, transform.position, transform.rotation);
Destroy(gameObject);
if (hit.transform.tag == "Head") {
hit.transform.SendMessageUpwards("ApplyDamage", SendMessageOptions.DontRequireReceiver);
}
}
}
}
So, what could be wrong? This is the mensage that is sent,a long with the part that shoots the bullet:
void Update () {
waitTilNextFire -= Time.deltaTime;
if (Input.GetMouseButton(0) && waitTilNextFire <= 0f) {
Network.Instantiate (laser, laserSpawner.position, laserSpawner.rotation, 0);
waitTilNextFire = fireSpeed;
}
}
void ApplyDamage () {
Network.Destroy(transform.root.gameObject);
}
And if you need the network manager: http://pastebin.com/ryhrfhFq
And here's a script that I got from quil18creates tutorial where he uses photon and tried putting it into unity default networking:
using UnityEngine;
using System.Collections;
public class NetworkMovement : MonoBehaviour {
Vector3 realPos = Vector3.zero;
Quaternion realRot = Quaternion.identity;
void Update () {
if (!networkView.isMine) {
transform.position = Vector3.Lerp(transform.position, realPos, 0.1f);
transform.rotation = Quaternion.Lerp(transform.rotation, realRot, 0.1f);
}
}
void OnSerializeNetworkView (BitStream stream) {
if (stream.isWriting) {
realPos = transform.position;
realRot = transform.rotation;
stream.Serialize(ref realPos);
stream.Serialize(ref realRot);
}
else {
stream.Serialize(ref realPos);
stream.Serialize(ref realRot);
}
}
}
I fail to see any code related to networking except for the Network.Destroy
which obviously has nothing to do with your error. Please post the code relevant to the error message and delete the rest.
The Spherecast function should be related to network, since it has [RPC] or am I wrong?
$$anonymous$$aking a function as RPC
simply makes it callable across the network. What you do in your function doesn't have to be related to networking in any way shape or form.
I once made a simple networking script where when you shoot, it would call a new function and shoot a raycast forward, and thens end a message to what it hit, that code was working across the network simply putting [RPC] on it, so what should I change so this works?
As I said, I can only help you once you post all relevant networking code. In the "script" you posted above there is NO networking code what so ever.
Answer by Benproductions1 · Mar 05, 2014 at 10:59 AM
I suggest you brush up on how you use RPC's in Unity. You need to use Network.RPC
in order for the RPC to be called on other clients.
If you call the function directly you're only doing that, calling the function.
How should I buffer it? RPC$$anonymous$$ode.AllBuffered? And I quite don't udnerstand that, the graphics part were all working, teh meshes and the light was being disabled correctly, but the message wasn't being sent? I mean I disabled the script if the networkView.is$$anonymous$$ine is false, so why does the graphics part work and the message doesn't?
Player still not getting destroyed, I also get a lot of this errors: https://www.dropbox.com/s/re3gve1c7eo1qlf/Captura%20de%20tela%202014-03-05%2011.36.19.png
@$$anonymous$$adJohny I never said anything about buffering. I merely said you need to use Network.RPC
to call your function across the network, because simply calling it directly doesn't do so.
Those errors have to do with your network views. You either don't have one attached on all clients or they all have different ID's.
Your answer
Follow this Question
Related Questions
How To Deal With Lingering Prefabs in Multiplayer Scene ? 0 Answers
Networking RPC sends to wrong target 0 Answers
How can i send a Player list to every Client 0 Answers
RPC and multiplayer networking 2 Answers
Acess server stuff by the client 0 Answers