- Home /
Question by
Emilyanis · Jun 13, 2014 at 05:39 AM ·
networkingshootingprojectilemultiplayer networkingtanks
Multiplayer tank game
Hi! I creating a multiplayer tank game, and I have some progress with tank/turret position and rotation in network, but now I stuck in shooting, I tried a lot of methods with RPC and another but it doesn't work. Console gives to me a lot of errors about allocatedID and etc.
Here some parts of coaxial MG shooting code:
function Update(){
if (networkView.isMine == true && on == true && Input.GetButton(fireKey) && Time.time > nextFire && pammo>0) {
networkView.RPC("Shoot", RPCMode.All);
}
@RPC
function Shoot(){
ammo -= 1;
if (ammo==0 && mags>0){
nextFire = Time.time + reloadTime;
if (View.outside == false){
audio.PlayOneShot(reloadSound);
}
}
if (ammo>0) {
nextFire = Time.time + fireRate;
}
var clone : Transform;
xRec = Random.Range(-0.5,0.5);
yRec = Random.Range(-0.5,0.5);
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.rigidbody.velocity = transform.TransformDirection(Vector3(xRec,yRec,bulletSpeed));
if (View.outside == false){
audio.PlayOneShot(inShootSound);
}
yield WaitForSeconds(distToListener/314);
if (View.outside == true){
outShootSound.Play();
outShootSoundDist.Play();
outShootSoundFar.Play();
}
}
And here some parts of bullet code:
function OnSerializeNetworkView (stream : BitStream, info : NetworkMessageInfo) {
var syncPosition : Vector3= Vector3.zero;
if (stream.isWriting) {
rot = transform.rotation;
syncPosition = transform.position;
stream.Serialize(syncPosition);
stream.Serialize(rot);
} else {
stream.Serialize(syncPosition);
stream.Serialize(rot);
transform.rotation = rot;
syncTime = 0f;
syncDelay = Time.time - lastSynchronizationTime;
lastSynchronizationTime = Time.time;
syncStartPosition = transform.position;
syncEndPosition = syncPosition;
}
}
function SyncedMovement() {
syncTime += Time.deltaTime;
transform.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
}
@RPC
function FixedUpdate () {
var hit : RaycastHit;
if (rigidbody.velocity.magnitude>0) {
if (Physics.Raycast(transform.position,transform.forward,hit,rigidbody.velocity.magnitude*Time.deltaTime)) {
transform.position = hit.point;
}
}
}
function OnCollisionEnter (collision : Collision) {
if (collision.transform.name != transform.name){
var contact : ContactPoint = collision.contacts[0];
var rotation = Quaternion.FromToRotation( Vector3.forward, contact.normal );
var explosion : Transform;
if (collision.collider.tag != 'Terrain') explosion=Network.Instantiate(metalEffect,contact.point,rotation,1);
if (collision.collider.tag == 'Terrain') explosion=Network.Instantiate(groundEffect,contact.point,rotation,1);
rand = Random.Range(0,impact.Length-1);
if (View.outside == true) explosion.audio.PlayOneShot(impact[rand]);
Network.RemoveRPCs(GetComponent(NetworkView).viewID);
Network.Destroy(GetComponent(NetworkView).viewID);
}
}
P.S Sorry for my bad english)
Comment
Sorry for this) I just want to see some multiplayer shooting example, how does it works, how bullet synchronizes with other players, and something else.