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 jeffrobinson-com · Jun 06, 2016 at 06:02 PM · destroyrandomspawn

spawn random after death

I have this script which looks like this

using UnityEngine; using System.Collections;

public class HealthSystem_Network : MonoBehaviour {

 public float curHealth;
 public float maxHealth;
 public AudioClip deathClip;
 public GameObject iName;
 public GameObject cams;
 public GameObject iBarHealth;
 public string myname;
 
 [HideInInspector] public float lostHealth;
 [HideInInspector] public float minHealth;
 [HideInInspector] public bool recovery;
 [HideInInspector] public float recoveySpeed;
 [HideInInspector] public float counter;
 [HideInInspector] public float waitTime;
 [HideInInspector] public float timer;
 [HideInInspector] public float resetAfterDeathTime;
 [HideInInspector] public bool dead;
 [HideInInspector] public float timerfS;
 [HideInInspector] public float timerS;
 [HideInInspector] public bool fS;
 
 void Awake () {
     lostHealth = 0;
     minHealth = 1;
     recovery = false;
     recoveySpeed = 15;
     waitTime = 7.0f;
     counter = waitTime;
     resetAfterDeathTime = 3.0f;
     cams = GameObject.Find("Main Camera");
     timerfS = 0.25f;
     timerS = timerfS;
     
     if(curHealth < 1)
         curHealth = 100;
     if(maxHealth < 1)
         maxHealth = 100;
 }
 
 public void uName (string me){
     GetComponent<NetworkView>().RPC("userName", RPCMode.AllBuffered, me);
 }
 
 [RPC]
 public void userName (string nameOfUser){
     myname = nameOfUser;
 }
 
 public void Update () {
     if(curHealth < maxHealth && curHealth >= minHealth && !recovery) counter -= Time.deltaTime;
     if(counter <= 0 && !dead)recovery = true;
     if(counter > 0) recovery = false;
     if(recovery) curHealth += recoveySpeed * Time.deltaTime;
     if(curHealth >= maxHealth){
         counter = waitTime;
         curHealth = maxHealth;
     }
     if(curHealth < minHealth) Die();
     if(dead)timer += Time.deltaTime;
     else timer = 0;
     if(timer >= resetAfterDeathTime) LevelReset();
     lostHealth = maxHealth - curHealth;
     if(timerfS > 0) fS = true;
     else fS = false;
     if(dead) timerfS -= Time.deltaTime;
     else timerfS = timerS;
     if(iName && iBarHealth && cams) Info_Enemy();
 }
 
 public void Recived (int fireDamage){
     GetComponent<NetworkView>().RPC("Damage", RPCMode.All, fireDamage);
     counter = waitTime;
 }
 
 [RPC]
 public void Damage(int damage){
     if(curHealth >= minHealth && !dead) curHealth -= damage;
 }
 
 void Die(){
     if(curHealth < minHealth && curHealth != -300){
         dead = true;
         curHealth = -300;
         GetComponent<GTA_Controller_Network>().enabled = false;
         GetComponent<ToRagdollTest>().SendMessage("Dead");
         if(deathClip) AudioSource.PlayClipAtPoint(deathClip, transform.position);
         this.GetComponent<CapsuleCollider>().enabled = false;
         this.GetComponent<Rigidbody>().isKinematic = true;
     }
 }
 
 void LevelReset (){
     dead = false;
     curHealth = maxHealth;
     GetComponent<ToRagdollTest>().SendMessage("Dead");
     GetComponent<GTA_Controller_Network>().enabled = true;
     this.GetComponent<CapsuleCollider>().enabled = true;
     this.GetComponent<Rigidbody>().isKinematic = false;
 }
 
 void Info_Enemy(){
     int heightLettter = iName.GetComponent<TextMesh>().fontSize;
     float enemyHealth_var_x = (curHealth * (heightLettter/30))/150  + curHealth /150;
     float enemyHealth_var_y = (heightLettter/60) + 0.7f;
     iName.transform.rotation = Quaternion.Euler(0, cams.transform.rotation.eulerAngles.y, 0);
     iBarHealth.transform.rotation = Quaternion.Euler(0, cams.transform.rotation.eulerAngles.y, 0);
     if(!dead) iBarHealth.transform.localScale = new Vector3 (Mathf.Lerp(iBarHealth.transform.localScale.x, enemyHealth_var_x, Time.deltaTime * 5), Mathf.Lerp(iBarHealth.transform.localScale.y,enemyHealth_var_y/10, Time.deltaTime * 5), iBarHealth.transform.localScale.z);
     else iBarHealth.transform.localScale = new Vector3 (Mathf.Lerp(iBarHealth.transform.localScale.x, 0, Time.deltaTime * 5), iBarHealth.transform.localScale.y, iBarHealth.transform.localScale.z);
     
     if(this.gameObject.name == "Remote_Player"){
         Vector3 distance = this.transform.position - GameObject.Find("Local_Player").transform.position;
         float limitOfVision = distance.magnitude;
         iName.GetComponent<TextMesh>().text = this.myname;
         iBarHealth.GetComponent<MeshRenderer>().enabled = true;
         if(limitOfVision <= 60 && !dead){
             iName.GetComponent<MeshRenderer>().enabled = true;
             if(limitOfVision >= 5) iName.GetComponent<TextMesh>().fontSize = (int)limitOfVision * 5;
             else iName.GetComponent<TextMesh>().fontSize = 25;
             float LimitVision = (limitOfVision * 0.01f) + 0.2f;

             switch (iName.GetComponent<OrientationName>().Sequence.currentSlot){
             case Setup.XYZ_Sequence.Width_Height_Depth_OR_Depth_Height_Width:
                 iName.transform.localPosition = new Vector3 
                     (iName.transform.localPosition.x, 
                      Mathf.Lerp(iName.transform.localPosition.y, LimitVision, Time.deltaTime * 5), 
                      iName.transform.localPosition.z);
                 break;
             case Setup.XYZ_Sequence.Width_Depth_Height_OR_Depth_Width_Height:
                 iName.transform.localPosition = new Vector3 
                     (iName.transform.localPosition.x, 
                      iName.transform.localPosition.y,
                      Mathf.Lerp(iName.transform.localPosition.z, LimitVision, Time.deltaTime * 5));
                 break;
             case Setup.XYZ_Sequence.Height_Width_Depth_OR_Height_Depth_Width:
                 iName.transform.localPosition = new Vector3 
                     (Mathf.Lerp(iName.transform.localPosition.x, LimitVision, Time.deltaTime * 5), 
                      iName.transform.localPosition.y, 
                      iName.transform.localPosition.z);
                 break;
             case Setup.XYZ_Sequence.Width_negativeHeight_Depth_OR_Depth_negativeHeight_Width:
                 iName.transform.localPosition = new Vector3 
                     (iName.transform.localPosition.x, 
                      Mathf.Lerp(iName.transform.localPosition.y, -LimitVision, Time.deltaTime * 5), 
                      iName.transform.localPosition.z);
                 break;
             case Setup.XYZ_Sequence.Width_Depth_negativeHeight_OR_Depth_Width_negativeHeight:
                 iName.transform.localPosition = new Vector3 
                     (iName.transform.localPosition.x, 
                      iName.transform.localPosition.y,
                      Mathf.Lerp(iName.transform.localPosition.z, -LimitVision, Time.deltaTime * 5));
                 break;
             case Setup.XYZ_Sequence.negativeHeight_Width_Depth_OR_negativeHeight_Depth_Width:
                 iName.transform.localPosition = new Vector3 
                     (Mathf.Lerp(iName.transform.localPosition.x, -LimitVision, Time.deltaTime * 5), 
                      iName.transform.localPosition.y, 
                      iName.transform.localPosition.z);
                 break;
             }
         }
         else{
             iName.GetComponent<MeshRenderer>().enabled = false;
             iBarHealth.GetComponent<MeshRenderer>().enabled = false;
         }
     }
     else{
         iName.GetComponent<TextMesh>().text = null;
         iBarHealth.GetComponent<MeshRenderer>().enabled = false;
     }
 }

}

How would I make it so after death i dpawnn at random objects tagged Spawnpoint?

Comment
Add comment · Show 2
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 Raresh · Jun 06, 2016 at 06:10 PM 0
Share

So you basically want to spawn at a random GameObject from the current scene with the tag "Spawnpoint"?

avatar image jeffrobinson-com Raresh · Jun 06, 2016 at 06:41 PM 0
Share

yes i do plese

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by imaxus · Jun 06, 2016 at 06:47 PM

Make game object with tag "spawnPoint", place them in points u want to spawn your character. Then in function Die() or Levelreset() do sth like this:

 var spawnPoints = GameObject.FindGameObjectsWithTag("spawnPoint");
 int rand = (int)Random.Range(0, spawnPoints.Count);
 transform.position = spawnPoints(rand).transform.position;




Comment
Add comment · Show 5 · 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 Raresh · Jun 06, 2016 at 06:54 PM 1
Share

Heh, beat me to it :p. Some slight adjustments to your code, hope you don't $$anonymous$$d ^_^

 public Transform GetRandomSpawnpoint()
 {
      GameObject[] spawnPoints = GameObject.FindGameObjectsWithTag("Spawnpoint");
      int rand = Random.Range(0, spawnPoints.Length);
      return spawnPoints[rand].transform;
 }


avatar image jeffrobinson-com Raresh · Jun 06, 2016 at 07:03 PM 0
Share

raresh, I got this error

Error CS1061 'GameObject[]' does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type 'GameObject[]' could be found (are you missing a using directive or an assembly reference?)

avatar image imaxus jeffrobinson-com · Jun 06, 2016 at 07:30 PM 0
Share

Count give u size of list. @Raresh code contains array not list like i want to do.

avatar image imaxus Raresh · Jun 06, 2016 at 07:28 PM 0
Share

Ofc i dont $$anonymous$$d, i wrote this fast so i made many mistakes :) But i think var spawnPoints is ok since it should be marked as list by compiler.

avatar image Raresh imaxus · Jun 06, 2016 at 07:32 PM 0
Share

Whoops sorry my bad, corrected it to .length, also made it a method :p

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I detect if there's an object at the same position and destroy ONLY ONE of them? 1 Answer

Randomly Place Cubes In Viewport Without Overlap 1 Answer

Generate random number and set a GameObject active. 1 Answer

Problem with random spawning and coroutine 1 Answer

2D runner, need a help 0 Answers


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