Search Unity

Snowgun shots everywere but not in the right direction.

Discussion in 'Scripting' started by Transformator, Dec 20, 2014.

  1. Transformator

    Transformator

    Joined:
    Oct 1, 2013
    Posts:
    11
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Why do you have so many scripts?

    As far as shooting a gun, at most you should have two. Gun_ Controller and Bullet_Unit.

    Gun_Controller will monitor input, on fire btn pressed, fire(), here you instantiate, or call from a pool, a Bullet_Unit. Set it the same rotation, position as barrel of gun. And then fire the bullet. Here is where the bullet takes over, propelling itself.
     
  3. Transformator

    Transformator

    Joined:
    Oct 1, 2013
    Posts:
    11
    Is it possible to create a reference to the bullet i've created, that i can say
    Code (csharp):
    1. bullet.transform.position.x = 0;
    or something like this?
     
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    If you are instantiating and destroying the bullet, I would suggest you have no need to reference it, however yes.

    Code (csharp):
    1.  
    2. GameObject bullet = Instantiate();
    3. ///now you could add this bullet to a List<> for example
    4.  
    If you are pooling your bullets, you should already have a reference for it.
     
  5. Transformator

    Transformator

    Joined:
    Oct 1, 2013
    Posts:
    11
    this:
    Code (csharp):
    1.  
    2. // This funktion fires the bullet and sets it's roation, scale and position.
    3. function fire()
    4. {
    5.     // creates a reference to bullet
    6.     var b = Instantiate(bullet);
    7.  
    8.     // sets position of bullet to spawnposition
    9.     b.transform.position.x = transform.position.x;
    10.     b.transform.position.y = transform.position.y;
    11.     b.transform.position.z = transform.position.z;
    12.  
    13.     // sets rotation of bullet to spawnposition
    14.     b.transform.rotation.x = transform.rotation.x;
    15.     b.transform.rotation.y = transform.rotation.y;
    16.     b.transform.rotation.z = transform.rotation.z;
    17.  
    18.     // adds bullet to bulletList
    19.     bulletList.Add(b);
    20. }
    21.  
    is now my fire funktion. but it don't work! No SyntaxError but it don't work. Now the bullet never shoots, not in the false direction.
    here is my full code:
    Gun_Controller.js - http://pastebin.com/N1w8d0Ks
    Bullet_Unit.js - http://pastebin.com/ijTpZgxk
    i've also added a few comments.

    Edit: no thats wrong. It shoots, but the bullet is nowere the position is false, but why?
     
  6. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    first, why write,
    Code (csharp):
    1.  
    2.    // sets position of bullet to spawnposition
    3.    b.transform.position.x = transform.position.x;
    4.    b.transform.position.y = transform.position.y;
    5.    b.transform.position.z = transform.position.z;
    6.  
    7.  
    8.   // sets rotation of bullet to spawnposition
    9.    b.transform.rotation.x = transform.rotation.x;
    10.     b.transform.rotation.y = transform.rotation.y;
    11.    b.transform.rotation.z = transform.rotation.z;
    12.  
    Just write.
    Code (csharp):
    1.  
    2. b.transform.position = transform.position;
    3. b.transform.rotation = transform.rotation;
    4.  
    Second, you dont do anything to your bullet. You set up no way for it to propel itself. You must use Translate(), MoveTowards() or some type of Physics Force to it.
     
  7. Transformator

    Transformator

    Joined:
    Oct 1, 2013
    Posts:
    11
    ok now i've changed my code like this:
    Code (csharp):
    1.  
    2. // This funktion fires the bullet and sets it's roation, scale and position.
    3. function fire()
    4. {
    5.     // creates a reference to bullet
    6.     var b = Instantiate(bullet);
    7.    
    8.     // sets position of bullet to spawnposition
    9.     b.transform.position = transform.position;
    10.    
    11.     // sets rotation of bullet to spawnposition
    12.     b.transform.rotation = transform.rotation;
    13.    
    14.     // adds bullet to bulletList
    15.     bulletList.Add(b);
    16. }
    17.  
    but it didn't work. so first i make the movement with Translate():
    Code (csharp):
    1.  
    2. function Update () {
    3.     // Let the Bullet move forward. (Here is a mistake)
    4.     transform.Translate(Vector3.forward * Time.deltaTime);
    5.     //transform.position = transform.position + transform.forward * distance * Time.deltaTime;
    6. ...
    7.  
    i am not sure if it's working, because i can't see the bullet.
    How to change the variables from "b"? how to reference it right?
     
  8. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Add in a factor to slow or speed the bullet up. Also, place a print in at start, just to see if it is created and in the correct position or watch the hierarchy to see if it is there.
     
  9. Transformator

    Transformator

    Joined:
    Oct 1, 2013
    Posts:
    11
    EDIT:
    Now I've changed the most part of the code.
    I've attached it.

    Gun_Controller.js - http://pastebin.com/SxbwktHg
    Bullet_Unit.js - http://pastebin.com/XFPUK1RU

    The Problem is now, that i can't set the position of the bullet in my Gun_Controller file. It still not work, I've tried much ways but none worked.
     
    Last edited: Dec 22, 2014