- Home /
Updating collision on both server and client
How does collision work on network. every time i seem to fire on the server side it destroys the cube that ive targeted, but over on the client side it says ive destroyed the ones in the middle.
this is my script for destroy cube using UnityEngine; using System.Collections;
public class DestroyCube: MonoBehaviour
{
private Game game;
void Start ()
{
game = (Game)GameObject.Find("GameManager").GetComponent("Game");
}
void OnTriggerEnter(Collider other)
{
if (Network.peerType == NetworkPeerType.Server)
{
if (other.tag == "Bullet")
{
string shipName = ((Bullet)other.GetComponent("Bullet")).getFiredBy();
Debug.Log("Bullet fired by " + shipName + " has hit " + gameObject.name);
GameObject ship = GameObject.Find(shipName);
// remove the bullet from the bullet list
game.removeBullet(other.gameObject);
// tell all clients to destroy the bullet
game.getNetworkView().RPC("DestroyBullet", RPCMode.Others, other.gameObject.name);
// destroy bullet on server
Destroy(other.gameObject);
// tell all clients to destroy the cube
game.getNetworkView().RPC("DestroyCube", RPCMode.Others, gameObject.name);
// destroy cube on server
Destroy(gameObject);
}
}
}
}
which is then called from a RPC Function from another script
[RPC] public void DestroyCube(string cubeName) { GameObject cube = GameObject.Find("DestroyableCube");
if (cube != null)
{
Debug.Log("Destroying Cube " + cubeName);
DestroyCube cubeScript = (DestroyCube)cube.GetComponent("DestroyCube");
Destroy(cube);
}
}
would i have to do something similar in the way my bullets get removed
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Game : $$anonymous$$onoBehaviour
{
public bool running;
public List<GameObject> bullets;
public List<GameObject> newBullets;
public List<GameObject> cubes;
public List<GameObject> newCubes;
public GameObject DestroyableCube;
private Rect bulletCountPos, resetGamePos, startGamePos, gameRunningPos;
// Use this for initialization
void Start ()
{
resetGamePos = new Rect(10, 70, 150, 20);
startGamePos = new Rect(10, 90, 150, 20);
bullets = new List<GameObject>();
newBullets = new List<GameObject>();
bulletCountPos = new Rect(Screen.width - 200, 10, 200, 20);
gameRunningPos = new Rect(Screen.width - 200, 30, 200, 20);
}
void Update ()
{
if (running)
{
if (Network.peerType == NetworkPeerType.Server)
{
//Debug.Log("Updating bullet list");
Bullet bulletScript;
newBullets = new List<GameObject>(bullets);
bullets.Clear();
// check all local bullet flight times
// Network.Destroy any over time
foreach(GameObject bullet in newBullets)
{
// check if bullet still exists (because the collision trigger messages can run during this update loop)
if (bullet == null) continue;
bulletScript = (Bullet)bullet.GetComponent("Bullet");
if (bulletScript.currentFlightTime < bulletScript.maxFlightTime)
{
bullets.Add(bullet);
}
else
{
// tell all clients to destroy the bullet
Debug.Log("Ordering clients to remove " + bullet.name);
networkView.RPC("DestroyBullet", RPC$$anonymous$$ode.Others, bullet.name);
// destroy local bullet on server
Destroy(bullet);
}
}
// clear the newBullets list
newBullets.Clear();
}
}
}
Answer by Mint92 · Apr 28, 2014 at 01:41 PM
Silly mistake, i needed to give each cube there own ID number. works fine now
Your answer
Follow this Question
Related Questions
View ID AllocatedID: X not found during lookup. 1 Answer
RPC call problem : local variable unvaluated ? 1 Answer
Why isn't my RPC shooting code working? 0 Answers
RPC Call to Another GameObject's Function? 1 Answer
Networking Error! 0 Answers