Search Unity

Create a simple projectile?

Discussion in 'Scripting' started by 3, Nov 11, 2012.

  1. 3

    3

    Joined:
    Sep 16, 2012
    Posts:
    387
    Alright, I've tried everything. Many times when I fire, it spawns the bullet but just falls right to the floor. This is the only time it doesn't pass through the walls. All the other times it fires in some random direction, passing right through box colliders. So its a bit frustrating, no matter what I look up, it doesn't work, so I'm just going to ask for someone to help me out. May I have a script that works with raycasting? So all it does is fire a ray from an gameobject's position, and instantiate the bullet so It looks like bullet shells flying through the air.

    Here is my best script so far, though it still was a complete failure.

    Code (csharp):
    1.  
    2. var projectile : Transform;
    3. var bulletSpeed : float = 20;
    4. var shootForce : float = 20;
    5. function Update () {
    6.     if (Input.GetButtonDown("Fire1")) {
    7.     var clone : Transform;
    8.     clone = Instantiate(projectile, transform.position, transform.rotation);
    9. projectile.rigidbody.AddForce(new Vector3(0,20000,0));
    10.     }
    11. }
    12.  
    So there doesn't even need to be a bullet flying, also, how would I make it like an automatic weapon?

    Sorry for the demands, I've just gotten so frustrated with this I feel like punching a wall, but basic reasoning and logic tell me not to.

    Most of all, how do I make a decal? I don't really get how.

    Thanks,
     
    Last edited: Nov 11, 2012
  2. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
  3. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    did you try this:
    Code (csharp):
    1. var projectile : Rigidbody;
    2.  
    3. function Update () {
    4.  
    5.     if (Input.GetButtonDown("Fire1")) {
    6.  
    7.     var clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
    8.  
    9.    clone.AddForce(0,20,0);
    10.  
    11.     }
    12.  
    13. }
     
  4. woodygoody

    woodygoody

    Joined:
    Aug 23, 2011
    Posts:
    164
    Great .. But I hope someone tell me what is the reason of using keyword as in previous line before Rigidbody ?
     
    Last edited: Nov 12, 2012
  5. 3

    3

    Joined:
    Sep 16, 2012
    Posts:
    387
    It stills goes about a foot in front of the weapon, then falls. I put mass at .1 and nothing changes, why?
     
  6. Democre

    Democre

    Joined:
    Mar 31, 2010
    Posts:
    345
    The projectile variable is typed as a Rigidbody. The Instantiate method only returns Object typed objects which you must then cast yourself. Since we know that the type of "projectile" is Rigidbody, we can safely assume that whatever is instantiated would also be able to be cast to Rigidbody. The "as Rigidbody" merely tells the compiler that it can treat the result object as a Rigidbody.
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    you could have kept your original code, the fix actually came from the second line...


    you had..
    Code (csharp):
    1.  
    2.  
    3. clone = Instantiate(projectile, transform.position, transform.rotation);
    4. projectile.rigidbody.AddForce(new Vector3(0,20000,0));
    5.  
    6.  
    The problem with this code, is you are adding force to your prefab, not to the new bullet you just created...


    Code (csharp):
    1.  
    2. clone = Instantiate(projectile, transform.position, transform.rotation);
    3. clone.rigidbody.AddForce(new Vector3(0,20000,0));
    4.  
    The above change means it will apply the force to your new bullet, not the one you are making copies of.
     
  8. Scryptic

    Scryptic

    Joined:
    Jan 22, 2019
    Posts:
    1
    Just in case you get this: In Unity you can use Raycasts which start from one point and travel in one direction until it collides with something it's not ignoring. Then you just have to check if the raycast hit an enemy, and if so, subtract from it's health.
     
  9. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    This.

    Also, the force is added on the Y axis(up), i think you probably want the Z axis(forward).
    And make AddForce into AddRelativeForce so it wont be in relation to global space but in local space.

    Unless this is what you want, but for a gun thats what I think.

    Finally, unless your firing projectiles in the likes of missiles, mortar bombs, etc. you probably wont even see the object, maybe for a frame or two, real bullet are tiny and fast af.
    Unless you can actually see the bullets just use a line renderer as a trace for the path