Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by YEngine226 · Mar 16, 2020 at 04:05 PM · scripting problemmultiplayerphoton

Problem with my Enemy Versus Script..

Hello everyone!

I've decided to make a multiplayer horror game which is like Dead by Daylight, where the host is the monster and he need to kill all the player which are alive. But Dead by Daylight has random chooser, well I don't really need random killer chooser in my game.

So... if there are 2 survivor players in the game, and when I want kill the 1st player, then the game destroys the 2nd player.

So that's the problem for me.. Could someone help to fix this one little problem?

Here's the Enemy Script:

using System.Collections; using System.Collections.Generic; using UnityEngine; using Photon.Pun; using UnityEngine.SceneManagement;

public class EnemyScript_Versus : MonoBehaviourPunCallbacks {

 public PhotonView PV;

 public GameObject[] players;

 public int currentHealth;

 public int maxHealth = 100;

 private int minHealth = 0;

 private int damageAmount = 50;

 [SerializeField]
 private Animator Anim;


 public bool onTrig;

 [SerializeField]
 private float distanceToSurvivor = 1f;

 void Start () {
     currentHealth = maxHealth;
 }

 void Update() {

     if (PauseMenu_Multiplayer.onPauseMenuOpen)
     {
         return;
     }

     players = GameObject.FindGameObjectsWithTag("Player");

     if (currentHealth > maxHealth)
     {
         currentHealth = maxHealth;
     }
     if(currentHealth < minHealth)
     {
         currentHealth = minHealth;
     }

     foreach (GameObject playerGO in players)
     {
         float distance = Vector3.Distance(transform.position, playerGO.transform.position);

         if (photonView.IsMine)
         {
             if (distance < distanceToSurvivor)
             {
                 RaycastHit hit;
                 Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
                 if (Physics.Raycast(ray, out hit, distanceToSurvivor))
                 {
                     foreach (GameObject go in players)
                     {
                         if (hit.transform.position == go.transform.localPosition)
                         {
                             Debug.Log(go.transform.localPosition - transform.localPosition);
                             if (Input.GetMouseButtonDown(0))
                             {
                                 Attacking();
                             }
                         }
                     }
                 }
             }
         }
     }
 }

 public void Damaging()
 {
     PlayerHealth[] PH = GameObject.FindWithTag("Player").GetComponents<PlayerHealth>();
     PhotonView[] PV = GameObject.FindWithTag("Player").GetComponents<PhotonView>();

     foreach(PlayerHealth pH in PH)
     {
         if(pH != null)
         {
             foreach (PhotonView PVm in PV)
             {
                 if (photonView == null)
                 {
                     Debug.LogError("PhotonView hasn't been attached.. Please make sure to attach your Photon View to this script.");
                 }
                 else
                 {
                     PVm.RPC("Death", RpcTarget.AllBuffered);
                 }
             }
         }

     }
 }

 public void Death()
 {
     foreach(GameObject player in players)
     {
         PhotonNetwork.Destroy(player);
         Debug.Log(player.gameObject + " has been killed by the werewolf!");
     }

}

 public void Attacking()
 {
     Anim.SetBool("isIdle", false);
     Anim.SetBool("isWalking", false);
     Anim.SetBool("isRunning", false);
     Anim.SetBool("isAttacking", true);
     StartCoroutine(Timer3());
     StartCoroutine(Timer1());
 }

 IEnumerator Timer3()
 {
     yield return new WaitForSeconds(1.15f);
     Anim.SetBool("isAttacking", false);
     Anim.SetBool("isIdle", true);
     Anim.SetBool("isWalking", false);
     Anim.SetBool("isRunning", false);

 }

 IEnumerator Timer1()
 {
     yield return new WaitForSeconds(0.9f);
     Damaging();
 }

}

Also there's the Survivor Health Script:

using System.Collections; using System.Collections.Generic; using UnityEngine; using Photon.Pun; using System;

public class PlayerHealth : MonoBehaviourPunCallbacks {

 public int maxHealth = 100;

 private EnemyScript_Versus _enemyScript;

 public int currentHealth;

 void Start () {
     currentHealth = maxHealth;
 }

 /**[PunRPC]
 public void TakeDamage(int amt)
 {
     if (photonView.IsMine)
     {
         currentHealth -= amt;

         if (currentHealth <= 0)
         {
             Death();
         }
     }
 }*/

 [PunRPC]
 void Death()
 {
     if (photonView.IsMine == true && PhotonNetwork.IsConnected == true)
     {
         PhotonNetwork.Destroy(this.gameObject);
         Debug.Log(gameObject + " has killed by the werewolf!");
         _enemyScript = GameObject.Find("Enemy_Multiplayer(Clone)").GetComponent<EnemyScript_Versus>();
     }
 }

 public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
 {
     if (stream.IsWriting)
     {
         stream.SendNext(currentHealth);
     }
     if(stream.IsReading)
     {
         stream.ReceiveNext();
     }
 }

}

I'm still practicing myself on creating multiplayer scripts. Anyways, thank you in advance! ^^

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by metalted · Mar 16, 2020 at 04:27 PM

So its not easy to understand everything you are trying to do in your game, but what i did notice is that you use this piece of code:

 public void Death()
  {
      foreach(GameObject player in players)
      {
          PhotonNetwork.Destroy(player);
          Debug.Log(player.gameObject + " has been killed by the werewolf!");
      }
 }


This would mean that you use an array or a list of some kind to store your players. When the death function is called, it will loop through all the players and Destroy() them. You would need to add an extra check to see if the player is actually dead, by using a bool in the player script or something. Something like this:

 public void Death()
  {
      foreach(GameObject player in players)
      {
          if(!player.dead){continue;}
 
          PhotonNetwork.Destroy(player);
          Debug.Log(player.gameObject + " has been killed by the werewolf!");
      }
 }


Hope this helps you. If not, please provide some extra information about the problem. Also try to single out the source of the problem instead of posting all the scripts. This will make it easier for people to help you with your problem.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image YEngine226 · Mar 16, 2020 at 05:24 PM 0
Share

And should I need to create that boolean inside the PlayerHealth script to check this inside my Versus script?

avatar image metalted YEngine226 · Mar 16, 2020 at 09:30 PM 0
Share

There is a script somewhere that you use to subtract health from the total health when you get hit right ? This will probably be the PlayerHealth script. In here you subtract from the health, until it has reached 0 or lower. When that happends you can set a boolean in that script. This boolean can than be checked to see if the player is actually dead or not.

avatar image YEngine226 metalted · Mar 25, 2020 at 05:03 PM 0
Share

I've tried for a lot of time in different ways, but the boolean isn't working, if I put the boolean above the PhotonNetwork.Destroy() function, it will not gonna work the Destroy() either.

Have you got other different ways to fix that?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

292 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Photon Multiplayer - nick above model head, I have no idea how to repair it 1 Answer

Problem with multiplayer unity. 1 Answer

Networking: Keep room alive without active players 0 Answers

Oculus Quest Photon Network bugs connecting players 2 Answers

How many current players will I get daily if I have 10k installs 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges