• 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 /
  • Help Room /
avatar image
Question by pero123123 · Oct 23, 2015 at 02:32 PM · collidertriggerfpsspawn

Spawning with trigger

Hello I'm making a fps game, my plan is to make a "flag" w$$anonymous$$ch would let a player trigger it, and when he triggers it, it spawns somet$$anonymous$$ng where empty gameObject is...but I would like to make it like if 1 team has triggered the flag other team cant trigger it till first team doesnt move from trigger..T$$anonymous$$s is last t$$anonymous$$ng that I need in my game..can some1 pls help

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Statement · Oct 23, 2015 at 11:17 PM

You could setup collision layers for each team and use trigger colliders on the flag.

On the flag script, add a public Transform variable to be your spawn location, w$$anonymous$$ch you then set in the inspector. Also add a public GameObject variable to be your prefab, w$$anonymous$$ch describes what to spawn. Also set up that variable in the inspector.

In OnTriggerEnter, Instantiate the prefab, and set its position to your spawns position.

 #pragma strict
 
 var spawn : Transform;
 var prefab : GameObject;
     
 // Assume layers have been set up!
 function OnTriggerEnter(other : Collider) 
 {
     Instantiate(prefab, spawn.position, spawn.rotation);
 }

And if you want to add a cooldown timer to only trigger every x seconds etc...

 #pragma strict
 
 var spawn : Transform;
 var prefab : GameObject;
 var cooldown = 5f; // 5 second trigger cooldown by default
     
 private var cooldownExpiry = 0f;
     
 // Assume layers have been set up!
 function OnTriggerEnter(other : Collider) 
 {
     if (Time.time < cooldownExpiry)
         return;
             
     Instantiate(prefab, spawn.position, spawn.rotation);
     cooldownExpiry = Time.time + cooldown;
 }
Comment

People who like this

0 Show 8 · 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 pero123123 · Oct 24, 2015 at 11:31 AM 0
Share

This could work, but i work in java script could u help me put it in.. This is my Capture controller script #pragma strict

 var blueTeam : boolean = false;
 var redTeam : boolean = false;
 
 private var redcapturePerc : float = 0;
 private var bluecapturePerc : float = 0;
 
 //FLAGS
 var flagRed : GameObject;
 var flagBlue : GameObject;
 var flagNeutral : GameObject;
 
 //Particles
 var mistRed : GameObject;
 var mistBlue : GameObject;
 var mistNeutral : GameObject;
 
 function Start()
 {
     flagRed.GetComponent(MeshRenderer).enabled = false;
     flagBlue.GetComponent(MeshRenderer).enabled = false;
     flagNeutral.GetComponent(MeshRenderer).enabled = true;
     
     mistRed.GetComponent(Renderer).enabled = false;
     mistBlue.GetComponent(Renderer).enabled = false;
     mistNeutral.GetComponent(Renderer).enabled = true;
 }
 
 function Update()
 {
     if(blueTeam == true)
     {
         bluecapturePerc += Time.deltaTime * 20;
         redcapturePerc -= Time.deltaTime * 20;
     }
     
     if(redTeam == true)
     {
         redcapturePerc += Time.deltaTime * 20;
         bluecapturePerc -= Time.deltaTime * 20;
     }
     
     if(redTeam == true && blueTeam == true)
     {
         redcapturePerc = redcapturePerc;
         bluecapturePerc = bluecapturePerc;
     }
     
     if(redcapturePerc >= 100)
     {
         redcapturePerc = 100;
         flagRed.GetComponent(MeshRenderer).enabled = true;
         flagBlue.GetComponent(MeshRenderer).enabled = false;
         flagNeutral.GetComponent(MeshRenderer).enabled = false;
         
         mistRed.GetComponent(Renderer).enabled = true;
         mistBlue.GetComponent(Renderer).enabled = false;
         mistNeutral.GetComponent(Renderer).enabled = false;
     }
     
     if(bluecapturePerc >= 100)
     {
         bluecapturePerc = 100;
         flagRed.GetComponent(MeshRenderer).enabled = false;
         flagBlue.GetComponent(MeshRenderer).enabled = true;
         flagNeutral.GetComponent(MeshRenderer).enabled = false;
         
         mistRed.GetComponent(Renderer).enabled = false;
         mistBlue.GetComponent(Renderer).enabled = true;
         mistNeutral.GetComponent(Renderer).enabled = false;
     }
     
     if(redcapturePerc <= 50 && bluecapturePerc <= 51)
     {
         flagRed.GetComponent(MeshRenderer).enabled = false;
         flagBlue.GetComponent(MeshRenderer).enabled = false;
         flagNeutral.GetComponent(MeshRenderer).enabled = true;
         
         mistRed.GetComponent(Renderer).enabled = false;
         mistBlue.GetComponent(Renderer).enabled = false;
         mistNeutral.GetComponent(Renderer).enabled = true;
     }
     
     if(redcapturePerc <= 0)
     {
         redcapturePerc = 0;
     }
     
     if(bluecapturePerc <= 0)
     {
         bluecapturePerc = 0;
     }
 }
 
 function OnGUI()
 {
     GUI.Box(Rect(10, 10, 300, 25),"Red Cap" + " " + redcapturePerc.ToString("0") + " " + "Blue Cap" + " " + bluecapturePerc.ToString("0"));
 }

avatar image pero123123 pero123123 · Oct 24, 2015 at 11:35 AM 0
Share

and i have this one, this is Flag script

 #pragma strict
 
 private var capController : CaptureController;
 
 function Start()
 {
 
 capController = GameObject.Find("CaptureCollider").GetComponent(CaptureController);
 
 }
 
 function OnTriggerEnter(Col : Collider)
 {
     if(Col.tag == "BlueTeam")
     {
         capController.blueTeam = true;
     }
     
     if(Col.tag == "RedTeam")
     {
         capController.redTeam = true;
     }
 }
 
 function OnTriggerExit(Col : Collider)
 {
     if(Col.tag == "BlueTeam")
     {
         capController.blueTeam = false;
     }
     
     if(Col.tag == "RedTeam")
     {
         capController.redTeam = false;
     }
 }
avatar image Statement pero123123 · Oct 24, 2015 at 11:41 AM 1
Share

I converted the example to javascript. Please try to solve it yourself. Create an empty game object which will be where you spawn your "something". Create a prefab that represents your "something". Put the script I made on your flag which has a trigger collider. Then set it up in the inspector

Spawn: the location where the "something" will spawn.
Prefab: the asset for "something" which will be spawned.

avatar image pero123123 Statement · Oct 24, 2015 at 11:49 AM 0
Share

Thanks man :D

Show more comments
avatar image Statement · Oct 25, 2015 at 03:30 PM 0
Share

I have no idea what you mean by " when cap reaches".

"how to destroy it when other team collides it"

Add a Trigger Collider on the flag and OnTriggerEnter, if it's the other team...

Use Destroy(gameObject);

avatar image pero123123 Statement · Oct 25, 2015 at 03:56 PM 0
Share

Sry for my english... When player(blue) stands close to flag it goes from 0-100 (timer) and when it reaches 100 flag becomes blue, now i want that when its blue flag it spawns gameobject in player blue base and when it reaches 0 on timer game object in player blue base is destroyed, same for red player... I already posted my capture controller script in the begining. if u want to look I hope that you understanded me,

avatar image Statement pero123123 · Oct 25, 2015 at 04:19 PM 0
Share

If you need to solve a different problem than your original question, then make a new question. Don't keep dragging the discussion into "the next step" to make your game complete.

avatar image

Answer by springwater · Oct 23, 2015 at 04:19 PM

 var redFlag:Transform;
 var scoringBlPlyr:Transform;//scoring blue player
 var bluDis= Vector3.Distance(scoringBlPlyr.position, redFlag.position); 
 var blueTeamScored=false;
 var xAmount= 5.0;//distance from flag where blue scoring player must remain
 var redCanTrigger=true;
 function Update(){
 if (blueTeamScored==true)
 if (BluDis<xAmount)
 redCanTrigger=false;
 }

Comment

People who like this

0 Show 1 · 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 pero123123 · Oct 23, 2015 at 04:33 PM 0
Share

Thanks for trying to help, but thats not what I ment. I dont need scoring I need spawning. like if flag is triggered by blue, then some items spawn in blue teams base, and if red in red team base. I made a flag with trigger function but I dont know how to make spawn when triggered t

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Flag capture with spawning and destroying gameObject 0 Answers

OnTriggerEnter2D() function not working 1 Answer

My code is instantiating many prefabs, i only want one. 1 Answer

How do I differentiate between the triggers? 1 Answer

How to identify which collider generated OnTriggerEnter2D 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