So ive got this game where i shoot an "arrow" at an object. Right now, its just a long cube attached to the camera. I use a rigid body (no gravity) that is in the center of the camera. When you click, its forced foward, and using some code i found here, sticks to the object and detach itself from the parent. The problem is that i can only shoot it once. previous attempts have caused too many cubes to instantiate. So im wondering how i could Instantiate an "arrow" from an empty game object and force it forward on click, and also stop any new arrows from reacting to the click so far ive got this attached to my arrow. Code (csharp): unction Update () { var click : float = Input.GetAxis("Fire1"); var power : float = 200; rigidbody.AddForce(Vector3(0,0,click * power)); }
Does this help? Attach it to your camera / whatever fires arrows, set the arrow and power vars in the inspector. Code (csharp): var arrow : GameObject; var power : float; function Update() { if ( Input.GetKeyDown( KeyCode.Mouse0 ) ) { var anArrow = Instantiate( arrow, transform.position, transform.rotation ); anArrow.rigidbody.AddForce( transform.forward * power ); } }
I am typing this in the Quick Reply, so just pseudo code this time. Put this on your Player or his Bow Code (csharp): //outside update gameobject Arrow; // put the Arrow Prefab here gameobject currentArrow = null; // inside update if(Input.GetButtonDown("Fire1")) { // Basicly when the FireButton is Clicked Down, instantiate an Arrow Object at the Bow. currentArrow = Instantiate(Arrow, transform.position, transform.rotation); } else if ( Input.GetButtonUp("Fire1")) { // Check if the User has released the fire Button, and set the flying property on the arrow // This would also be the place where you can calculate the Power, based on how long the Fire1 // Button was held down currentArrow.flying = true; currentArrow = null; // We don't need a reference anymore. } Put this on the Arrow Code (csharp): // Outside Update var power : float = 200; bool flying = false; // inside of Update if ( flying ) { // Do your acceleration Stuff here rigidbody.AddForce(Vector3(0,0,power)); } Hope that helps.
I see what you mean now. More Pseudo Code: Put this on your Bow, almost the same code Code (csharp): //outside update gameobject Arrow; // put the Arrow Prefab here gameobject currentArrow = null; // inside update if(Input.GetButtonDown("Fire1")) { // Basicly when the FireButton is Clicked Down, instantiate an Arrow Object at the Bow. currentArrow = Instantiate(Arrow, transform.position, transform.rotation); } else if ( Input.GetButtonUp("Fire1")) { // Check if the User has released the fire Button, and call the Fire Function on the Arrow // This would also be the place where you can calculate the Power, based on how long the Fire1 // Button was held down // do your Power Calculation here float power = 200; // Afterwards call the Fire Function on the Arrow, normally you would also pass a direction Vector but I leave that up to you currentArrow.Fire(power); currentArrow = null; // We don't need a reference anymore. } // Put this on your Arrow Code (csharp): // Instead of using Update, we are going to define a fire function on the Arrow // Remove the Update Function from before // This will only be called once function Fire( power: float ) { // The force will only be added once rigidbody.AddForce(Vector3(0,0,power)); }