- Home /
how to differentiate clones?
Hi!
I want to have my character with the input of "fire1" to instantiate around 5 flying daggers gameobjects(prefab) around him and then give them a x velocity, but each at a different time.
so forgive me since it would make this 2 questions.
//Insantiate a rigidbody then set the velocity var projectile : Rigidbody; function Update () { // Ctrl was pressed, launch a projectile if (Input.GetButtonDown("Fire1")) { // Instantiate the projectile at the position and rotation of this transform var clone : Rigidbody; clone = Instantiate(projectile, transform.position, transform.rotation);
// Give the cloned object an initial velocity along the current // object's Z axis clone.velocity = transform.TransformDirection (Vector3.forward * 10); } }
This is the code I got from the script reference. The first question is: how can I loop the instantiate part of the code to create the 5 projectiles. The second question is: once the 5 daggers are created, how do I point to them individually?
Or would it be easier to have each of the projectiles their own script that sets their velocity?
Answer by 3dDude · Aug 02, 2010 at 06:25 PM
try this :
//Insantiate a rigidbody then set the velocity
var projectile : Rigidbody; var velocities : Vector3[];
function Update () {
// Ctrl was pressed, launch a projectile
if (Input.GetButtonDown("Fire1")) {
// Instantiate the projectile at the position and rotation of this transform
for(i=0;i<velocities.length;i++){
var clone : Rigidbody;
clone = Instantiate(projectile, transform.position, transform.rotation);
// Give the cloned object an initial velocity along the current
// object's Z axis
clone.velocity = transform.TransformDirection (velocities[i]);
}
}
}
so just fill out the array of velocity Vector3's and then it will clone as many as you say.
Firstly thank you for answering. There are a few things I don't understand though. Vector3 is supposed to be an array of 3 variables (x,y,z). what I don't understand is why you put vector3 in velocities and then call velocities.length (I most usuall used length to measure a string, not an array...)
And most probably this will then explain why when I put the 3 xyz in the [ ] at line 3 it give me an error: ; expected at end of line.
Also... as I see the script you've shown me it would create a projectile and then fire it in on loop pass. What I'm looking for is a loop that will create all, say, 5 projectiles, have them float in the air for a second and then fire them one at a time (like a magic spell). But at the moment, this way is fine... as long as I get to have a few nifty projectiles flying in the right direction. (oh and sorry for not mentioning that before, but I'm making(trying to) a 2d scroller castlevania clone)
Thank you for the time you put in helping me figure this out.
no it doesn't work... like I said in my first comment, it gives me an error at line 3 ; expected at end of line