Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bullet Holes staying in air [when object is moving]

Discussion in 'Scripting' started by i3artyy2222, May 12, 2014.

  1. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Hello,
    As in title i have a problem because if i shot at moving target, the bullet holes stays in that place where i hit the target and they are just floating in air, how can i make it so will be attach to the object :( ?
    here is my script
     
    Last edited: May 13, 2014
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Parent the instantiated object's transform to the target's transform.
     
  3. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    hmm im not that pro what should i write ?
    ''parentInstantiate'' instead of Instantiate :D ?
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You're not "pro" enough to write "transform.parent = someOtherTransform" ?

    Check out the documentation for Instantiate to see what it returns to you.
    Check out the documentation for Transform and notice that there is a "parent" member.
     
    Last edited: May 12, 2014
  5. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Still dont get it...
     
    Last edited: May 12, 2014
  6. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Instantiate returns clone object thats all what i found -_-
    cant you just tell me what to put where... in some simply way other than this

    ''Parent the instantiated object's transform to the target's transform.''

    All i understand from that is that i need to parent the instantiated object which is bulletholes transform to the targets transform, target.. what target you mean like 'Dirt' ,'Untagged' ... thats all what i understand from there but where to put what line of code what to change i dont know ...
     
  7. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    easy there....

    Object.Instantiate returns an object. Since you are instantiating an object that is Typable. (no its not written on the site, but it is an implied thing) Worse, in the Javascript part, they don't even specify, you do.

    This is from your code...
    Code (csharp):
    1.  
    2. Instantiate(ConcreteParticle, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    3.  
    If you look at the documentation for Instantiate: http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

    You will see that they use something like this:

    Code (csharp):
    1.  
    2. var clone : Rigidbody;
    3. clone = Instantiate(projectile, transform.position, transform.rotation);
    4.  
    They only specify what it is by the "type" of Rigidbody and the "variable" known as clone.

    you can actually do something like this in Javascript:
    Code (csharp):
    1.  
    2. var projectile: GameObject;
    3.  
    4. function Update(){
    5. var clone : Rigidbody;
    6. clone = Instantiate(projectile, transform.position, transform.rotation);
    7. }
    8.  
    Which simply tells Unity that clone is the Rigidbody from the projectile, even though projectile is a GameObject.

    Now, what KelsoMRK is trying to say is that you "Type" your instantiated object as a Transform. This giving you all of the features that the Transform object has:
    http://docs.unity3d.com/Documentation/ScriptReference/Transform.html

    So the Transform specifically has Parent. Parent tells Unity that when the parent moves, so do all of it's children.

    Thus, when you shoot a plane, the bullet hole goes with the plane.

    (i.e.)
    Code (csharp):
    1.  
    2. var clone : Transform = Instantiate(ConcreteParticle, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    3. clone.parent = hit.collider.transform;
    4.  
    Note: you have to specify a transform when setting the parent, so I pulled the collider's transform that we hit.

    http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit-collider.html
     
    murp35 and A_Marraff like this.
  8. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    This is what kind of help is good explaining everything i learner a lot from there understand
    Thank you !
    it works for particle, but when i want to do for [main] for bullet holes

    Code (csharp):
    1.         if (MetalHole  hit.transform.tag == "Metal"){
    2.             Instantiate(MetalParticle, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    3.             var clone : Transform = Instantiate(MetalHole[Random.Range(0,3)], hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    4.             clone.parent = hit.collider.transform;
    5.             }
    I am getting this Assets/Scripts/BulletScript.js(40,60): BCE0022: Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Transform'.

    and when i do this
    Code (csharp):
    1.         if (MetalHole  hit.transform.tag == "Metal"){
    2.             var clone : Transform = Instantiate(MetalParticle, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    3.             clone.parent = hit.collider.transform;
    4.             var clone : GameObject = Instantiate(MetalHole[Random.Range(0,3)], hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    5.             clone.parent = hit.collider.transform;
    6.             }
    Im getting Assets/Scripts/BulletScript.js(41,29): BCE0067: There is already a local variable with the name 'clone'.
     
    Last edited: May 12, 2014
  9. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    this may sound stupid.. as I dont write in JS, split the lines like they have in the documentation:
    Code (csharp):
    1.  
    2. var clone : Transform;
    3. clone = Instantiate(MetalHole[Random.Range(0,3)], hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    4. clone.parent = hit.collider.transform;
    5.  
    or... worse. it forces the type through the Instantiate. in which case, you simply pull the transform through.

    Code (csharp):
    1.  
    2. var clone : Transform = Instantiate(MetalHole[Random.Range(0,3)], hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal)).transform;
    3. clone.parent = hit.collider.transform;
    4.  
    Which is also just as silly. one should work.
     
  10. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    By the by...

    The error says it's returning a GameObject.. (that is a type) and GameObject, like just about every other object in Unity has a reference to transform.

    Overall, Unity is a compilation of different object types. An object is a GameObject, or core, and a Transform which tells where it is in space.

    You add things to it then, like modules to get Rigidbodies or Colliders, but the core (GameObject and Transform) are always there.
     
  11. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Ok the second one worked, i tested the bullets stay on the moving object but im keep getting a lot of these
    ''Actor::updateMassFromShapes: Compute mesh inertia tensor failed for one of the actor's mesh shapes! Please change mesh geometry or supply a tensor manually!
    UnityEngine.Transform:set_parent(Transform)
    BulletScript:Update() (at Assets/Scripts/BulletScript.js:41)
    ''
     
  12. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    what is line 41?
     
  13. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    clone.parent = hit.collider.transform;
     
  14. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
  15. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Ok i fix it i made very thin box and it works now thank you for helping me !
    If you know how raycast [shooting] works you could help me also with this :( sometimes the hits when i shoot go through some objects and i dont know why ive try looking on every topic about raycast and i found that most of raycast shooting scripts doesnt have this line of code
    Code (csharp):
    1.     var fwd = transform.TransformDirection(Vector3.forward);
    http://forum.unity3d.com/threads/24...etimes-it-doesnt-detect?p=1617841#post1617841
     
  16. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    The basic concept of a raycast bullet is roughly the same. If they dont have the one you have, they will have one like this:
    Code (csharp):
    1.  
    2. var fwd = transform.forward;
    3.  
    Which is much simpler.

    If all of your objects have colliders then none of your objects are subject to rays going through them. There are three instances where it would.

    1: If your raycast was using a layer mask, and the objects they are not going through are omited by the layer mask.
    2: If your collider does not line up with your geometry. (i.e. a cube being used to roughly shape a wall with 3d elements.
    3: the collider or game object is inactive.

    The concept of a raycast bullet is very simple. Every frame, you get the distance and direction that the bullet would travel. (including gravity if that is part of your simulation) Cast a physics Ray to see if anything is between the current point, and the next point. If so, do something with it and destroy the bullet. If not, simply advance the bullet to the next position.