Search Unity

Instantiate + force on click

Discussion in 'Scripting' started by Broseidon91, Dec 7, 2010.

  1. Broseidon91

    Broseidon91

    Joined:
    Dec 6, 2010
    Posts:
    4
    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):
    1. unction Update () {
    2.     var click : float = Input.GetAxis("Fire1");
    3.     var power : float = 200;
    4.    
    5.         rigidbody.AddForce(Vector3(0,0,click * power));
    6.    
    7.        
    8.     }
     
  2. Vicenti

    Vicenti

    Joined:
    Feb 10, 2010
    Posts:
    664
    Does this help? Attach it to your camera / whatever fires arrows, set the arrow and power vars in the inspector.

    Code (csharp):
    1. var arrow : GameObject;
    2. var power : float;
    3.  
    4. function Update() {
    5.   if ( Input.GetKeyDown( KeyCode.Mouse0 ) ) {
    6.     var anArrow = Instantiate( arrow, transform.position, transform.rotation );
    7.     anArrow.rigidbody.AddForce( transform.forward * power );
    8.   }
    9. }
     
  3. Broseidon91

    Broseidon91

    Joined:
    Dec 6, 2010
    Posts:
    4
    var arrow would be where i stick the arrow?
     
  4. hallamasch

    hallamasch

    Joined:
    Nov 29, 2010
    Posts:
    153
    I am typing this in the Quick Reply, so just pseudo code this time.


    Put this on your Player or his Bow
    Code (csharp):
    1.  
    2. //outside update
    3. gameobject Arrow; // put the Arrow Prefab here
    4. gameobject currentArrow = null;
    5.  
    6.  
    7. // inside update
    8. if(Input.GetButtonDown("Fire1"))
    9. {
    10. // Basicly when the FireButton is Clicked Down, instantiate an Arrow Object at the Bow.
    11. currentArrow = Instantiate(Arrow, transform.position, transform.rotation);
    12. }
    13. else if ( Input.GetButtonUp("Fire1"))
    14. {
    15. // Check if the User has released the fire Button, and set the flying property on the arrow
    16. // This would also be the place where you can calculate the Power, based on how long the Fire1   // Button was held down
    17.  
    18. currentArrow.flying = true;
    19. currentArrow = null; // We don't need a reference anymore.
    20. }
    21.  
    22.  
    23.  
    24.  
    Put this on the Arrow
    Code (csharp):
    1.  
    2. // Outside Update
    3.  
    4. var power : float = 200;
    5. bool flying = false;
    6.  
    7. // inside of Update
    8. if ( flying )
    9. {
    10. // Do your acceleration Stuff here
    11. rigidbody.AddForce(Vector3(0,0,power));
    12.  
    13. }
    14.  
    15.  

    Hope that helps.
     
    Last edited: Dec 7, 2010
  5. Broseidon91

    Broseidon91

    Joined:
    Dec 6, 2010
    Posts:
    4
    Yes that worked perfectly, thanks a bunch!


    i am still applying force to the arrows though
     
    Last edited: Dec 7, 2010
  6. Broseidon91

    Broseidon91

    Joined:
    Dec 6, 2010
    Posts:
    4
    Does the currentArrow bit stop the force from being applied later?
     
  7. hallamasch

    hallamasch

    Joined:
    Nov 29, 2010
    Posts:
    153
    I see what you mean now.
    More Pseudo Code:

    Put this on your Bow, almost the same code
    Code (csharp):
    1.  
    2. //outside update
    3. gameobject Arrow; // put the Arrow Prefab here
    4. gameobject currentArrow = null;
    5.  
    6.  
    7. // inside update
    8. if(Input.GetButtonDown("Fire1"))
    9. {
    10. // Basicly when the FireButton is Clicked Down, instantiate an Arrow Object at the Bow.
    11. currentArrow = Instantiate(Arrow, transform.position, transform.rotation);
    12. }
    13. else if ( Input.GetButtonUp("Fire1"))
    14. {
    15. // Check if the User has released the fire Button, and call the Fire Function on the Arrow
    16. // This would also be the place where you can calculate the Power, based on how long the Fire1   // Button was held down
    17. // do your Power Calculation here
    18. float power = 200;
    19.  
    20. // Afterwards call the Fire Function on the Arrow, normally you would also pass a direction Vector but I leave that up to you
    21. currentArrow.Fire(power);
    22. currentArrow = null; // We don't need a reference anymore.
    23. }
    24.  
    25.  

    // Put this on your Arrow
    Code (csharp):
    1.  
    2. // Instead of using Update, we are going to define a fire function on the Arrow
    3. // Remove the Update Function from before
    4.  
    5. // This will only be called once
    6. function Fire( power: float )
    7. {
    8.        // The force will only be added once
    9.        rigidbody.AddForce(Vector3(0,0,power));
    10. }
    11.  
    12.