Hello everybody,
im a young student with the dream to create a little Multiplayer-Game.
At the moment there are Player that can connect to the Server and flight around etc. and thats working perfectely fine.
Now i want that the Player are able to Shoot Bullets to destroy Objects in my Scene. For testing stuff i just took a cube.
For Shooting i just wrote a little Javascript so if i press the Button “1” it creates an Prefab which i called “theBullet” (I´m very inventive).
Should be easy to understand i added that it plays an ShootingSound when i fire and an Ammo number that i reload when my magazine is empty which needs an certain time and an GUI which shows me the current Ammo.
#pragma strict
var theBullet : Rigidbody;
var Speed = 20;
var Ammo = 3;
var MaxAmmo = 3;
var Shoot = true;
var ReloadTime = 1;
var Reloading = false;
@script RequireComponent(AudioSource)
var front : AudioClip;
var back: AudioClip;
function Update () {
if(Ammo == 0) {
Shoot = false;
Reloading = true;
Reload();
//animation.Play("Reload");
Ammo = MaxAmmo;
}
if(Shoot == true){
if (Input.GetMouseButtonDown(1))
{
var clone = Instantiate(theBullet, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection(Vector3(0, 0, Speed));
audio.PlayOneShot(front, 1.0);
Ammo --;
Destroy (clone.gameObject, 1);
}}
}
function OnGUI() {
if(Reloading == false)
{ GUI.Label (Rect (10, 10, 100, 20), "Ammo: " + Ammo); }
if(Reloading == true)
{ GUI.Label (Rect (10, 10, 100, 20), "Reloading"); }
}
function Reload() {
yield WaitForSeconds(ReloadTime);
Shoot = true;
Reloading = false;
}
My Prefab, the Bullet, has another script where i can change the Damage and if it collides to my Object with an Health Script it takes Damage.
#pragma strict
var Dammage = 100;
function OnCollisionEnter (info : Collision)
{
info.transform.SendMessage("ApplyDammage", Dammage, SendMessageOptions.DontRequireReceiver);
}
So if my Bullet hits an Target with an Health Script where it checks the Damage dealt by the Bullet and when the Health is 0 it gets Destroyed.
#pragma strict
var Health = 200;
function ApplyDammage (TheDammage : int)
{
Health -= TheDammage;
if(Health <= 0)
{
Dead();
}
}
function Dead()
{
Destroy (gameObject);
}
And now i stuck in errors…
First is that if i shoot bullets on Player1, Player 2 on another PC cant see the bullets which i shoot. I added the PhotonView Script but i think its not that easy to do and yep, its not working.
Second is that not every Player gets the current Health of the Enemy with an the HealthScript. If i shoot on the Enemy with Player 1, it should reduce its maximum health which is 200 by the damage of the bullet which is 100. We have 100 left. Now i want that the Player 2 can “see” the current health of the Enemy and if he shoots another Bullet into the Enemy, which deals another 100 Damage, it gets -100Health from the current Health and not from the maximum Health.
Is there any easy way to fix these Problems? Or is it very complexity and takes many lines of codes and knowledge about Networking? Im just a young Boy and dont have the knowledge about that but i would love to learn some stuff about it so maybe you can recommend me any Page where i can find the Answers to my Questions?
Thanks for reading my novel and forgive me if i made some Mistakes with Grammar,
Daniel