Shooting with the same bullets.

Options
Freel4tte
edited January 2012 in DotNet
So im almost done with my own multiplayer game. I've stripped the Angry Bots Photon demo down and the only thing left is the player Prefab with additional scripts (Autofire etc.). I've made a weapon scroll function so that the player can switch weapons. The bullets for every weapon are working so the player can shoot with 5 different guns.

Now the problem is that when 2 players are in the game, each player shoots with the bullets chosen by the scene of each player. So for example when player 1 chooses the Assault Rifle, and player 2 chooses the Shotgun, in the scene of player 1 the 2nd player shoots with the assault rifle and in the scene of player 2 the 1st player shoots with the shotgun.

In other words, the game checks when someone is shooting, but instatiates the bullets according to the chosen weapon in a scene. I'm trying to get this fixed but without any succes. Maybe some of you can post their methods or idea's to get this fixed. Or explain where the problem can be found.

This is my Autofire script:
#pragma strict
#pragma downcast
@script RequireComponent (PerFrameRaycast)

class AutoFire extends Photon.MonoBehaviour {

var bulletPrefab : GameObject;
var assaultSound : AudioSource;

var shotGunPrefab : GameObject;
var shotgunSound : AudioSource;

var rocketPrefab : GameObject;

var electricPrefab : GameObject;
var electricSound : AudioSource;

var sniperPrefab : GameObject;
var sniperSound : AudioSource;

var spawnPoint : Transform;
var frequency : float = 10;
var addUp : int = 0;
var coneAngle : float = 1.5;
var firing : boolean = false;
var damagePerSecond : float = 20.0;
var forcePerSecond : float = 20.0;
var hitSoundVolume : float = 0.5;
var muzzleFlashFront : GameObject;

static var lastFireTime : float = -1;
private var raycast : PerFrameRaycast;

function Awake () {
	muzzleFlashFront.active = false;
	
	raycast = GetComponent.<PerFrameRaycast> ();
	if (spawnPoint == null)
		spawnPoint = transform;
}

function Update () {

	if(weaponWheel.weaponNumber == 1)
		addUp = 1;
	
	if(weaponWheel.weaponNumber == 2)
		addUp = 10;
	
	if(weaponWheel.weaponNumber == 3)
		addUp = 20;
		
	if(weaponWheel.weaponNumber == 4)
		addUp = 1;
	
	if(weaponWheel.weaponNumber == 5)
		addUp = 30;
	
	if (firing) {
		
			if (Time.time > lastFireTime + addUp / frequency) {
				// Spawn visual bullet
				if(weaponWheel.weaponNumber == 1){
					var coneRandomRotation = Quaternion.Euler (Random.Range (-coneAngle, coneAngle), Random.Range (-coneAngle, coneAngle), 0);
					var go : GameObject = Spawner.Spawn (bulletPrefab, spawnPoint.position, spawnPoint.rotation * coneRandomRotation) as GameObject;
					var bullet : SimpleBullet = go.GetComponent.<SimpleBullet> ();
					bulletPrefab.name = photonView.owner.name + "'s bullet";
					assaultSound.Play();
				}
				
				if(weaponWheel.weaponNumber == 2){
					var coneRandomRotationShot = Quaternion.Euler (Random.Range (-10, 10), Random.Range (-10, 10), 0);
					var goShot : GameObject = Spawner.Spawn (shotGunPrefab, spawnPoint.position, spawnPoint.rotation * coneRandomRotationShot) as GameObject;
					var bulletShot : shotGunBullet = goShot.GetComponent.<shotGunBullet>();
					
					var coneRandomRotationShot2 = Quaternion.Euler (Random.Range (-10, 10), Random.Range (-10, 10), 0);
					var coneRandomRotationShot3 = Quaternion.Euler (Random.Range (-10, 10), Random.Range (-10, 10), 0);
					var coneRandomRotationShot4 = Quaternion.Euler (Random.Range (-10, 10), Random.Range (-10, 10), 0);
					var coneRandomRotationShot5 = Quaternion.Euler (Random.Range (-10, 10), Random.Range (-10, 10), 0);
					var coneRandomRotationShot6 = Quaternion.Euler (Random.Range (-10, 10), Random.Range (-10, 10), 0);
					
					var goShot2 : GameObject = Spawner.Spawn (shotGunPrefab, spawnPoint.position, spawnPoint.rotation * coneRandomRotationShot2) as GameObject;
					var goShot3 : GameObject = Spawner.Spawn (shotGunPrefab, spawnPoint.position, spawnPoint.rotation * coneRandomRotationShot3) as GameObject;
					var goShot4 : GameObject = Spawner.Spawn (shotGunPrefab, spawnPoint.position, spawnPoint.rotation * coneRandomRotationShot4) as GameObject;
					var goShot5 : GameObject = Spawner.Spawn (shotGunPrefab, spawnPoint.position, spawnPoint.rotation * coneRandomRotationShot5) as GameObject;
					var goShot6 : GameObject = Spawner.Spawn (shotGunPrefab, spawnPoint.position, spawnPoint.rotation * coneRandomRotationShot6) as GameObject;
					
					var bulletShot2 : shotGunBullet = goShot2.GetComponent.<shotGunBullet> ();
					var bulletShot3 : shotGunBullet = goShot3.GetComponent.<shotGunBullet> ();
					var bulletShot4 : shotGunBullet = goShot4.GetComponent.<shotGunBullet> ();
					var bulletShot5 : shotGunBullet = goShot5.GetComponent.<shotGunBullet> ();
					var bulletShot6 : shotGunBullet = goShot6.GetComponent.<shotGunBullet> ();
					shotGunPrefab.name = photonView.owner.name + "'s bullet";
					shotgunSound.Play();
				}
				
				if(weaponWheel.weaponNumber == 3){
					var coneRandomRotationRock = Quaternion.Euler (0, 0, 0);
					var goRock : GameObject = Spawner.Spawn (rocketPrefab, spawnPoint.position, spawnPoint.rotation * coneRandomRotationRock) as GameObject;
					var bulletRock : shotGunBullet = goRock.GetComponent.<shotGunBullet> ();
					rocketPrefab.name = photonView.owner.name + "'s bullet";
				}
				
				if(weaponWheel.weaponNumber == 4){
					var coneRandomRotationElc = Quaternion.Euler (0, 0, 0);
					var goElc : GameObject = Spawner.Spawn (electricPrefab, spawnPoint.position, spawnPoint.rotation * coneRandomRotationElc) as GameObject;
					var bulletElc : electroBullet = goElc.GetComponent.<electroBullet> ();
				}
				
				if(weaponWheel.weaponNumber == 5){
					var coneRandomRotationSnip = Quaternion.Euler (0, 0, 0);
					var goSnip : GameObject = Spawner.Spawn (sniperPrefab, spawnPoint.position, spawnPoint.rotation * coneRandomRotationSnip) as GameObject;
					var bulletSnip : sniperShot = goSnip.GetComponent.<sniperShot> ();
					sniperSound.Play();
					sniperPrefab.name = photonView.owner.name + "'s bullet";
				}
				
				lastFireTime = Time.time;
				
				// Find the object hit by the raycast
				/*var hitInfo : RaycastHit = raycast.GetHitInfo ();
				if (hitInfo.transform) {
					// Get the health component of the target if any
					var targetHealth : Health = hitInfo.transform.GetComponent.<Health> ();
					if (targetHealth) {
						// Apply damage
						targetHealth.OnDamage (damagePerSecond / frequency, -spawnPoint.forward);
					}
					
					// Get the rigidbody if any
					if (hitInfo.rigidbody) {
						// Apply force to the target object at the position of the hit point
						var force : Vector3 = transform.forward * (forcePerSecond / frequency);
						hitInfo.rigidbody.AddForceAtPosition (force, hitInfo.point, ForceMode.Impulse);
					}
					
					// Ricochet sound
					//var sound : AudioClip = MaterialImpactManager.GetBulletHitSound (hitInfo.collider.sharedMaterial);
					//AudioSource.PlayClipAtPoint (sound, hitInfo.point, hitSoundVolume);
					
					bullet.dist = hitInfo.distance;
				}
				else {
					bullet.dist = 1000;
				}*/
			}
	}
}

function OnStartFire () {
	firing = true;
	
	muzzleFlashFront.active = true;
	
	if (audio)
		audio.Play ();
}

function OnStopFire () {
	firing = false;
	
	muzzleFlashFront.active = false;
	
	if (audio)
		audio.Stop ();
}


function OnPhotonSerializeView ( stream : PhotonStream,  info : PhotonMessageInfo)    {
    if(stream.isWriting){
    	stream.SendNext(firing);
		
		
    }else{
		
    	var val : boolean = stream.ReceiveNext();
    	if(val){
    	OnStartFire();
		
    	}else{
    	OnStopFire();}
    }
  }
 }

If you don't get the question, or if I have to post more scripts, let me know. I will help you the best i can!

Thanks in advance!

Comments

  • Leepo
    Options
    Where/what is "weaponWheel.weaponNumber"?

    Clearly in your weapon selection code you should only run selection changes if the player is the owner of the attached PhotonView:

    if(photonView.isMine){
    //Weapon -change-input
    }

    Also, send an RPC to all other players after changing a weapon locally to network this.