- Home /
How to change between scripts on trigger?
I'm pretty new to C# so sorry if this is an easy script to figure out.
So I have a box collider on a cube, that when I shoot it with a projectile I want it to activate a power-up, which is another script. The original script is a click-and-shoot script, and the power-up script allows the player to click-and-hold to rapid fire. I know it has something to do with tags to trigger the script switch when a projectile hits it. I also wanted the power-up script to only last for a set amount of time before reverting back to the click-and-shoot script.
Any help would be much appreciated. :)
Answer by GreenSerpent · Nov 26, 2018 at 11:28 PM
You would want to add a tag called "Projectile" to your projectile. Then, you would want to add this in a script on the cube:
public PowerupManager powerupScript;
void OnTriggerEnter (Collider other) {
if (other.collider.gameObject.tag == "Projectile") {
powerupScript.powerupTime = 0;
}
}
Then, make a script called "PowerupManager" with this in it:
public float powerupLength;
public float powerupTime;
public RapidFireScript rapidFire;
public SingleShotScript singleShot;
void Update () {
powerupTime += Time.deltaTime;
if (powerupTime > powerupLength) {
powerupTime = powerupLength;
rapidFire.enabled = false;
singleShot.enabled = true;
} else {
rapidFire.enabled = true;
singleShot.enabled = false;
}
}
Make sure you change "RapidFireScript" and "SingleShotScript" to the actual names of your power-up scripts. Then, add that script to your player and set the variables in the inspector. Set 'powerupScript' on the cube to be the script you just added to the player. 'powerupLength' is how long the power-up will last in seconds. 'rapidFire' and 'singleShot' are your power-up scripts that you wanted to enable and disable. Though it is needed, changing 'powerupTime' in the inspector will do nothing.
Make sure your projectile has a collider and either your cube or your projectile has a rigidbody. If you don't want it to move around, you can always set the rigidbody to kinematic. Hopefully this works!
Thanks so much for replying, I appreciate it. So it is kind of working, except when I run the game I immediately have the power-up activated, it lasts for as long as I set the power up limit is set for, then it switches to the click-and-shoot script. I don't think my collider situation is right, cause when I shoot my power-up targets, nothing happens. I have an explosion prefab on the power-ups already, so when I hit it with a projectile it disappears. Would I have to add a second box collider and set it as a trigger or something? Extra info: - my two shooting scripts have variables that can be set, they are in my main camera (where the shots come from) -the scripts are the same, except that I had to change the getComponent collider part because I'm using a slightly older version of Unity I think. - The powerupCollision script is the script for the power-up cube.
Answer by TheGameLearner · Nov 27, 2018 at 04:27 AM
In @GreenSerpent 's script, add a bool to check whether we want to activate the powerUp. Update function should add 'powerupTime' with Time.deltaTime and activate deactivate other scripts only when the bool is true. you can set 'powerupTime' to zero whenever the trigger is fired and then start from there. Keep the bool as False at starting to avoid adding to time, and then if bool is false, activate single click script.
Your answer
Follow this Question
Related Questions
Does anyone know how to activate different scripts OnTrigger? 1 Answer
How to switch between scripts on trigger? 1 Answer
My player's camera displays jerky objects 2 Answers
When I enable certain scripts on my player, the transform falls through the map. 0 Answers
Best way to make this grid 0 Answers