- Home /
Projectile Selection through Array
Hi everyone,
I've made my vehicle able to select 2 types of rockets based on whether I press "1" or "2" (and space bar to shoot). Except I wish to add more types of projectiles later on and I know the code will definitely get messy without an array, which I am currently not using. How may I implement projectile selection through an array? I know how to make an array for the projectiles, but I can't manage to reference the projectile inside the array into the Instantiation script.
I appreciate your help- Hyperion
Answer by robertbu · Aug 05, 2013 at 01:30 AM
I'm not sure where you have the stumbling block, so I'll take a shot:
At the top of the file you can do:
var prefab : GameObject[];
You can click on the triangle to the left of the variable, set the size of the array, and then drag and drop your prefabs into the array.
To fire the projectile you can do something like:
function FireProjectile(index : int) {
var go = Instantiate(prefab[index], transform.position, transform.rotation);
go.rigidbody.AddForce(transform.forward * force_factor);
}
There are several ways to translate keys to specific values to pass into fireProjectile. One simple way would be to use a switch() statement.
PART 1 OF CO$$anonymous$$$$anonymous$$ENT: Well, that's not quite what I was going for... Here's my current attempt at the issue in code:
var projectiles:GameObject[];
function Start()
{
SelectProjectile(0);
}
function FixedUpdate() {
//Projectile Selection if (Input.Get$$anonymous$$eyDown("1")) SelectProjectile(0); if (Input.Get$$anonymous$$eyDown("2")) SelectProjectile(1); if (Input.GetButtonDown("Jump") && canShoot){
var projectile=Instantiate(projectiles[index], transform.FindChild("CannonPoint").transform.position, Quaternion.identity);
//tags projectile, gets speed of vehicle and multiplies it by 3000 for the RPG
if (projectiles[index]==RPG){
projectile.gameObject.tag="projectile";
projectile.rigidbody.AddForce(transform.forward*4000);
}
if (projectiles[index]==RPBR){
projectile.gameObject.tag="BouncyRocket";
projectile.rigidbody.AddForce(transform.forward*50000);
projectile.rigidbody.AddForce(transform.up*7000);
}
}
}
function SelectProjectile(index : int) {
for (var obj:GameObject in projectiles)obj.SetActive(false);
projectiles[index].SetActive(true);
}
Except it has a problem: projectiles index is unidentified in an error message.
PART 2 OF CO$$anonymous$$$$anonymous$$ENT: How about I just show you my code that works without an array? $$anonymous$$aybe you'll be able to understand what I meant specifically...
//the 2 projectile types I have so far
var RPG:Transform;
var RPBR:Transform;
//variable for storing current projectile being used/to be used
var myProjectile;
function Start()
{
myProjectile=RPG;
}
//Fixed Update because of State$$anonymous$$achine, not a problem
function FixedUpdate()
{
//Projectile Selection
if (Input.Get$$anonymous$$eyDown("1")) myProjectile=RPG;
if (Input.Get$$anonymous$$eyDown("2")) myProjectile=RPBR;
if (Input.GetButtonDown("Jump") && canShoot){
var projectile=Instantiate(myProjectile, transform.FindChild("CannonPoint").transform.position, Quaternion.identity);
if (myProjectile==RPG){
projectile.gameObject.tag="projectile";
projectile.rigidbody.AddForce(transform.forward*4000);
}
if (myProjectile==RPBR){
projectile.gameObject.tag="BouncyRocket";
projectile.rigidbody.AddForce(transform.forward*50000);
projectile.rigidbody.AddForce(transform.up*7000);
}
}
}
By the way, thank you for helping me with the code, I don't mean to be a nuisance but I'm bad with arrays.
The concept is the same. Here is a quick rewrite of your code to show you what I mean:
var projectiles : Transform[];
enum ProjType { RPG, RPBR }
var currProj = ProjType.RPG;
var myProjectile;
//Fixed Update because of State$$anonymous$$achine, not a problem
function FixedUpdate()
{
//Projectile Selection
if (Input.Get$$anonymous$$eyDown("1")) currProj=ProjType.RPG;
if (Input.Get$$anonymous$$eyDown("2")) currProj=ProjType.RPBR;
if (Input.GetButtonDown("Jump") && canShoot){
var projectile=Instantiate(projectiles[currProj], transform.FindChild("CannonPoint").transform.position, Quaternion.identity);
switch(currProj) {
case ProjType.RPG:
projectile.gameObject.tag="projectile";
projectile.rigidbody.AddForce(transform.forward*4000);
break;
case ProjType.RPBR:
projectile.gameObject.tag="BouncyRocket";
projectile.rigidbody.AddForce(transform.forward*50000);
projectile.rigidbody.AddForce(transform.up*7000);
break;
}
}
}
You will have to drag and drop your prefabs into the projectiles array in the correct order.
Your answer
Follow this Question
Related Questions
Cycle Through GameObjects in Array Issue. 1 Answer
How to cycle through targets? 2 Answers
IndexOutOfRangeException: Array index is out of range when using an Array and instantiating 2 Answers
Activating Bool Array in sequence. 2 Answers
Array with game object: object reference not set to an instance of an object 1 Answer