Search Unity

Tank script, wont shoot forward and "die"...

Discussion in 'Scripting' started by wrm186, Aug 18, 2011.

  1. wrm186

    wrm186

    Joined:
    Apr 4, 2010
    Posts:
    661
    The code below is from someone else, I tried figuring it out but, I'm only a modeler. I'm a scripter learning in progress. Anyways, this makes my missile shoot and everything, but it wont go forward, it will only stay in one spot when shot. Then it also won't "die" so it takes up my hierarchy making it laggy.

    For the missile maybe I should and a rigid-body to it so it will move?

    Code (csharp):
    1. var Shot : AudioClip;
    2. var timer = 50;
    3. var bulletspeed = 10;
    4. var bullet : Transform;
    5.     function Update () {
    6.     if(timer == 50){
    7.     if(Input.GetKey(KeyCode.Space)){
    8.     audio.PlayOneShot(Shot);
    9.     var bulletprefab : Transform;
    10.     bulletprefab = Instantiate(bullet,
    11.     transform.position, transform.rotation);
    12.     timer = 0;
    13.     }
    14.     }
    15.     else{
    16.     timer += 1;
    17.     }
    18.     }
    Thanks,
    Wade
     
  2. Overunity3d

    Overunity3d

    Joined:
    Sep 5, 2010
    Posts:
    14
    You gotta do 'AddForce();'
     
  3. wrm186

    wrm186

    Joined:
    Apr 4, 2010
    Posts:
    661
    Alright I'll try :) Thanks. Hopefully I can get it on the first try!
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    2 things.

    1. Please edit your post if the code doesn't show up neatly.

    2. That code will only let you fire on 1 frame

    Heres a version that will let you fire (it also added the movement and death)
    Code (csharp):
    1.  
    2. var Shot : AudioClip;
    3.  
    4. var timeToFire : float=1.0f;
    5. var bulletspeed = 10;
    6. var bullet : Transform;
    7. var bulletLifetime : float=10.0f;
    8.  
    9. private var timer : float;
    10.  
    11. function Start () {
    12.     timer = timeToFire;
    13. }
    14.  
    15. function Update () {
    16.     if(timer >= timeToFire){
    17.         if(Input.GetKey(KeyCode.Space)){
    18.             audio.PlayOneShot(Shot);
    19.             var bulletprefab : GameObject = Instantiate(bullet, transform.position, transform.rotation) as GameObject;
    20.            
    21.             bulletPrefab.rigidbody.AddForce (transform.forward*bulletSpeed);
    22.            
    23.             Destroy (bulletPrefab, bulletLifetime);
    24.            
    25.             timer = 0;
    26.         }
    27.     }
    28.     else{
    29.         timer += Time.deltaTime;
    30.     }
    31. }
    32.  
    For movement, you need a rigidbody, and you add force in the forward direction (using transform.forward)
    To destroy it, you call Destroy (obj, lifeTime)
     
  5. wrm186

    wrm186

    Joined:
    Apr 4, 2010
    Posts:
    661
    Alright thank you Dman. But there is a problem here it says:

    bulletPrefab.rigidbody.AddForce (transform.forward*bulletSpeed);


    "Unknown Identifier: bulletPrefab".


    EDIT: Never mind fixed it, just had to lowercase the S in speed and the P in prefab :)

    Thanks again.
     
  6. wrm186

    wrm186

    Joined:
    Apr 4, 2010
    Posts:
    661
    Now it falls down when space has been pressed, but it wont shoot forward.

    Maybe add a constant force?