Spawning with trigger
Hello I'm making a fps game, my plan is to make a "flag" which would let a player trigger it, and when he triggers it, it spawns something 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..This is last thing that I need in my game..can some1 pls help
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, which you then set in the inspector. Also add a public GameObject variable to be your prefab, which 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;
}
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($$anonymous$$eshRenderer).enabled = false;
flagBlue.GetComponent($$anonymous$$eshRenderer).enabled = false;
flagNeutral.GetComponent($$anonymous$$eshRenderer).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($$anonymous$$eshRenderer).enabled = true;
flagBlue.GetComponent($$anonymous$$eshRenderer).enabled = false;
flagNeutral.GetComponent($$anonymous$$eshRenderer).enabled = false;
mistRed.GetComponent(Renderer).enabled = true;
mistBlue.GetComponent(Renderer).enabled = false;
mistNeutral.GetComponent(Renderer).enabled = false;
}
if(bluecapturePerc >= 100)
{
bluecapturePerc = 100;
flagRed.GetComponent($$anonymous$$eshRenderer).enabled = false;
flagBlue.GetComponent($$anonymous$$eshRenderer).enabled = true;
flagNeutral.GetComponent($$anonymous$$eshRenderer).enabled = false;
mistRed.GetComponent(Renderer).enabled = false;
mistBlue.GetComponent(Renderer).enabled = true;
mistNeutral.GetComponent(Renderer).enabled = false;
}
if(redcapturePerc <= 50 && bluecapturePerc <= 51)
{
flagRed.GetComponent($$anonymous$$eshRenderer).enabled = false;
flagBlue.GetComponent($$anonymous$$eshRenderer).enabled = false;
flagNeutral.GetComponent($$anonymous$$eshRenderer).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"));
}
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;
}
}
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.
I have no idea what you mean by " when cap reaches".
"how to destroy it when other $$anonymous$$m collides it"
Add a Trigger Collider on the flag and OnTriggerEnter, if it's the other $$anonymous$$m...
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,
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.
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;
}
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 $$anonymous$$ms base, and if red in red $$anonymous$$m base. I made a flag with trigger function but I dont know how to make spawn when triggered t