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

Help With Bullet Wind Mechanics.

Discussion in 'Scripting' started by Aidenjl, Jul 4, 2014.

  1. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    Hello, I have been working on some bullet mechanics for a sniper game and I decided that it needed wind mechanics aswell. So what I did was copy the script from this site: http://armedunity.com/topic/1384-wind-physics-effects-anything-you-want-it-to/ (I know it's never a good idea to copy scripts that I don't understand, but I did it anyway). I have fully integrated the scripts with mine but my bullets are not being effected by the wind (I tested it on a sphere and that worked perfectly fine) here are my scripts:

    Code (JavaScript):
    1.  
    2. #pragma strict
    3. #pragma implicit
    4. #pragma downcast
    5.  
    6. //BULLET Particles
    7. var Untagged : GameObject;                   // default bullet hole
    8. var Concrete : GameObject;                    // concrete bullet hole
    9. var Wood : GameObject;                        // wood bullet hole
    10. var Metal : GameObject;                        // metal bullet hole
    11. var Dirt : GameObject;                        // dirt/sand bullet hole
    12. var Blood : GameObject;                        // blood bullet hole
    13. var Glass : GameObject;                        // glass bullet hole
    14. var Snow : GameObject;                        // Snow bullet hole
    15. var Water : GameObject;                        // water bullet hole
    16.  
    17. var lifeTime : float = 2.0;                        // bullet life Time
    18. var mask : LayerMask = 1;
    19. var Gravity : float = 2;
    20.  
    21. var debugDamage : boolean = false;
    22.  
    23. private var hitCount : int = 0;                    // hit counter for counting bullet impacts for bullet penetration
    24. private var damage : float;                        // damage bullet applies to a target
    25. private var force : float;
    26. private var maxHits : float;                    // number of collisions before bullet gets destroyed  
    27. private var impactForce : float;                // force applied to a rigid body object
    28. private var maxInaccuracyHIP : float;            // maximum amount of inaccuracy
    29. private var spread : float;                        // used in machineguns to decrease accuracy if maintaining fire
    30. private var speed : float;                        // bullet speed
    31. private var hit : RaycastHit;
    32. private var hit2 : RaycastHit;
    33. private var player : GameObject;
    34. var Rotation : Transform;
    35.  
    36. private var velocity : Vector3 = Vector3.zero;  // bullet velocity
    37.  
    38. private var hasHit : boolean = false;            // has the bullet hit something?
    39. private var ignore : Collider = null;            // ignore this collider
    40. private var direction : Vector3;                // direction bullet is travelling
    41. private var direction2 : Vector3;
    42. private var newPos  : Vector3;
    43. private var oldPos : Vector3;
    44.  
    45. private var isTracer : boolean = true;
    46. private var bulletInfo : float[] = new float[2];
    47.  
    48. var windScript : WindManager;
    49. var Weight : float = .5;
    50.  
    51. function SetUp(bulletInfo : float[] ){            // information sent from gun to bullet to change bullet properties
    52.     damage = bulletInfo[0];                      // bullet damage
    53.     force = bulletInfo[1];                         // force applied to rigid bodies
    54.     maxHits = bulletInfo[2];                     // max number of bullet impacts before bullet is destroyed
    55.     maxInaccuracyHIP = bulletInfo[3];           // max inaccuracy of the bullet
    56.     spread = bulletInfo[4];                      // current inaccuracy... mostly for machine guns that lose accuracy over time
    57.     speed = bulletInfo[5];                       // bullet speed
    58.    
    59.     newPos = transform.position;                   // bullet's new position
    60.     oldPos = newPos;                               // bullet's old position
    61.    
    62.    
    63.     velocity = speed * transform.forward;         // bullet's velocity determined by direction and bullet speed
    64.    
    65.     // drection bullet is traveling
    66.     direction = transform.TransformDirection(Random.Range(-maxInaccuracyHIP, maxInaccuracyHIP) * spread, Random.Range(-maxInaccuracyHIP, maxInaccuracyHIP) * spread, 1);
    67.    
    68.     Destroy(gameObject, lifeTime);
    69. }
    70.  
    71. function Start() {
    72.  
    73. windScript = gameObject.GetComponent("WindManager");
    74.  
    75. }
    76.  
    77. function Update (){
    78.  
    79. var windInfluence = windScript.windSpeed / Weight;
    80. rigidbody.AddForce(windScript.windDirection * windInfluence);
    81.        
    82.     if (hasHit)
    83.     return;     // if bullet has already hit its max hits... exit
    84.    
    85.     // assume we move all the way
    86.  
    87.     newPos += (velocity + direction) * Time.deltaTime;
    88.  
    89.    
    90.     // Check if we hit anything on the way
    91.     var dir : Vector3 = newPos - oldPos;
    92.     var dist : float = dir.magnitude;
    93.  
    94.     if (dist > 0) {
    95.    
    96.         // normalize
    97.          dir /= dist;
    98.        
    99.     if(Physics.Raycast(oldPos, dir, hit, dist, mask.value)){
    100.        
    101.             newPos = hit.point;
    102.                
    103.                // notify hit
    104.             OnHit(hit);
    105.                //Debug.Log("if " + hitCount + " > " + maxHits + " then destroy bullet...");
    106.                  
    107.             if (hitCount >= maxHits) {
    108.                 hasHit = true;
    109.                    Destroy(gameObject);
    110.           }
    111.     }
    112.    
    113.     if (Physics.Raycast(newPos, -dir, hit2, dist, mask.value)) {    // send a ray behind the bullet to check for exit impact  
    114.    
    115.             // testing to see if the bullet passed through something
    116.             //RaycastHit hit2 = hits2[j];
    117.               if ((!hasHit))    // && (hit2.transform != owner.transform))
    118.                {
    119.                 OnBackHit(hit2); // send rear impact and check what to do with it
    120.             }
    121.            
    122.         }
    123.        
    124.         oldPos = transform.position;  // set old position to current position
    125.         //rigidbody.AddForce(Vector3.left * 5, ForceMode.VelocityChange);
    126.         transform.position = newPos;  // set current position to the new position  
    127.     }
    128.    
    129.    
    130.     velocity.y -= Gravity * Time.deltaTime;
    131.     Rotation.transform.rotation = Quaternion.LookRotation(dir);
    132.  
    133.  
    134. }
    135.  
    136. function OnHit(hit : RaycastHit){
    137.  
    138.     hitCount++; // add another hit to counter
    139.     var contact : Vector3 = hit.point;     // point where bullet hit
    140.     var rotation : Quaternion = Quaternion.FromToRotation(Vector3.up, hit.normal); // rotation of bullet impact
    141.     var direction2 = gameObject.transform.TransformDirection(Vector3(Random.Range(-0.01, 0.01) * spread, Random.Range(-0.01, 0.01) * spread, 1));
    142.    
    143.     var hitHeight : float = 0.008;
    144.    
    145.     if (hit.rigidbody)
    146.         hit.rigidbody.AddForceAtPosition(force * direction2, hit.point);
    147.        
    148.     //Physics.IgnoreCollision(hit.collider, player.transform.root.collider);
    149.        
    150.     if (hit.transform.tag == "Untagged" && Untagged != null) {
    151.         var untaggedHole : GameObject = Instantiate(Untagged, hit.point, rotation) as GameObject;
    152.         untaggedHole.transform.localPosition += hitHeight * hit.normal;
    153.         untaggedHole.transform.parent = hit.transform;
    154.     }
    155.    
    156.     if (hit.transform.tag == "Concrete" && Concrete != null) {
    157.         var concreteHole : GameObject = Instantiate (Concrete, hit.point, rotation) as GameObject;
    158.         concreteHole.transform.localPosition += hitHeight * hit.normal;
    159.         concreteHole.transform.parent = hit.transform;
    160.     }
    161.    
    162.     if (hit.transform.tag == "Wood" && Wood != null) {
    163.         var woodHole = Instantiate (Wood, hit.point, rotation) as GameObject;
    164.         woodHole.transform.localPosition += hitHeight * hit.normal;
    165.         woodHole.transform.parent = hit.transform;
    166.     }
    167.    
    168.     if (hit.transform.tag == "Metal" && Metal != null) {
    169.         var metalHole = Instantiate (Metal, hit.point, rotation) as GameObject;
    170.         metalHole.transform.localPosition += hitHeight * hit.normal;
    171.         metalHole.transform.parent = hit.transform;
    172.     }
    173.        
    174.     if (hit.transform.tag == "Enemy" && Blood != null) {
    175.         var bloodHole = Instantiate (Blood, hit.point, rotation) as GameObject;
    176.         bloodHole.transform.localPosition += hitHeight * hit.normal;
    177.         bloodHole.transform.parent = hit.transform;
    178.     }
    179.    
    180.     if (hit.transform.tag == "Player" && Blood != null) {
    181.         var playerHole = Instantiate (Blood, hit.point, rotation) as GameObject;
    182.         playerHole.transform.localPosition += hitHeight * hit.normal;
    183.         playerHole.transform.parent = hit.transform;
    184.     }
    185.    
    186.     if (hit.transform.tag == "Snow" && Snow != null) {
    187.         var snowHole = Instantiate (Snow, hit.point, rotation) as GameObject;
    188.         snowHole.transform.localPosition += hitHeight * hit.normal;
    189.         snowHole.transform.parent = hit.transform;
    190.     }
    191.    
    192.     if (hit.transform.tag == "Dirt" || hit.transform.tag == "Grass"|| hit.transform.tag == "Sand" && Dirt != null) {
    193.         var dirtHole = Instantiate (Dirt, hit.point, rotation) as GameObject;
    194.         dirtHole.transform.localPosition += hitHeight * hit.normal;
    195.         dirtHole.transform.parent = hit.transform;
    196.     }
    197.    
    198.     if (hit.transform.tag == "Water" && Water != null) {
    199.         var waterHole = Instantiate (Water, hit.point, rotation) as GameObject;
    200.         waterHole.transform.localPosition += hitHeight * hit.normal;
    201.         waterHole.transform.parent = hit.transform;
    202.     }
    203.    
    204.     if (hit.transform.tag == "Glass" && Glass != null) {
    205.         var glassHole = Instantiate (Glass, hit.point, rotation) as GameObject;
    206.         glassHole.transform.localPosition += hitHeight * hit.normal;
    207.         glassHole.transform.parent = hit.transform;
    208.     }
    209.    
    210.     hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
    211.     hit.collider.SendMessageUpwards("PlayerDamage", damage/2, SendMessageOptions.DontRequireReceiver);
    212.    
    213.     if(debugDamage){
    214.         Debug.Log("Current Damage is : " + damage);
    215.     }
    216. }
    217.  
    218. function OnBackHit(hit : RaycastHit){
    219.  
    220.     var contact : Vector3 = hit.point;     // point where bullet hit
    221.     var rotation : Quaternion = Quaternion.FromToRotation(Vector3.up, hit.normal); // rotation of bullet impact
    222.     var hitHeight : float = 0.008;
    223.    
    224.     if (hit.transform.tag == "Untagged" && Untagged != null) {
    225.         var untaggedHole : GameObject = Instantiate(Untagged, hit.point, rotation) as GameObject;
    226.         untaggedHole.transform.localPosition += hitHeight * hit.normal;
    227.         untaggedHole.transform.parent = hit.transform;
    228.     }
    229.    
    230.     if (hit.transform.tag == "Concrete" && Concrete != null) {
    231.         var concreteHole : GameObject = Instantiate (Concrete, hit.point, rotation) as GameObject;
    232.         concreteHole.transform.localPosition += hitHeight * hit.normal;
    233.         concreteHole.transform.parent = hit.transform;
    234.     }
    235.    
    236.     if (hit.transform.tag == "Wood" && Wood != null) {
    237.         var woodHole = Instantiate (Wood, hit.point, rotation) as GameObject;
    238.         woodHole.transform.localPosition += hitHeight * hit.normal;
    239.         woodHole.transform.parent = hit.transform;
    240.     }
    241.    
    242.     if (hit.transform.tag == "Metal" && Metal != null) {
    243.         var metalHole = Instantiate (Metal, hit.point, rotation) as GameObject;
    244.         metalHole.transform.localPosition += hitHeight * hit.normal;
    245.         metalHole.transform.parent = hit.transform;
    246.     }
    247.        
    248.     if (hit.transform.tag == "Enemy" && Blood != null) {
    249.         var bloodHole = Instantiate (Blood, hit.point, rotation) as GameObject;
    250.         bloodHole.transform.localPosition += hitHeight * hit.normal;
    251.         bloodHole.transform.parent = hit.transform;
    252.     }
    253.    
    254.     if (hit.transform.tag == "Dirt" || hit.transform.tag == "Grass" && Dirt != null) {
    255.         var dirtHole = Instantiate (Dirt, hit.point, rotation) as GameObject;
    256.         dirtHole.transform.localPosition += hitHeight * hit.normal;
    257.         dirtHole.transform.parent = hit.transform;
    258.     }
    259.    
    260.     if (hit.transform.tag == "Glass" && Glass != null) {
    261.         var glassHole = Instantiate (Glass, hit.point, rotation) as GameObject;
    262.         glassHole.transform.localPosition += hitHeight * hit.normal;
    263.         glassHole.transform.parent = hit.transform;
    264.     }
    265. }
    Code (JavaScript):
    1. var minDirectionWait : int = 5;
    2. var maxDirectionWait : int = 60;
    3. var minSpeed : int = 0;
    4. var maxSpeed : int = 15;
    5. var minSpeedWait : int = 5;
    6. var maxSpeedWait : int = 30;
    7. var minDirection : float = 0;
    8. var maxDirection : float = 1;
    9. var windDirection : Vector3;
    10. var windSpeed : float;
    11. private var directionTimer : float;
    12. private var speedTimer : float;
    13. private var directionAdjustX : float;
    14. private var directionAdjustZ : float;
    15. function Update()
    16. {
    17.         if(directionTimer <= 0)
    18.         {
    19.                 ChangeDirection();
    20.         }
    21.         if(speedTimer <= 0)
    22.         {
    23.                 ChangeSpeed();
    24.         }
    25.         directionTimer -= 1 * Time.deltaTime;
    26.         speedTimer -= 1 * Time.deltaTime;
    27. }
    28. function ChangeDirection()
    29. {
    30.         directionAdjustX = Random.Range(minDirection, maxDirection);
    31.         directionAdjustZ = Random.Range(minDirection, maxDirection);
    32.         if(directionAdjustX > 0.5)
    33.         {
    34.                 windDirection.x = 1;
    35.         }
    36.         else
    37.         {
    38.                 windDirection.x = 0;
    39.         }
    40.         if(directionAdjustZ > 0.5)
    41.         {
    42.                 windDirection.z = 1;
    43.         }
    44.         else
    45.         {
    46.                 windDirection.z = 0;
    47.         }
    48.         directionTimer += Random.Range(minDirectionWait, maxDirectionWait);
    49. }
    50. function ChangeSpeed()
    51. {
    52.         windSpeed = Random.Range(minSpeed, maxSpeed);
    53.         speedTimer += Random.Range(minSpeedWait, maxSpeedWait);
    54. }

    Any help would be much appreciated, thanks :)
     
  2. earrgames

    earrgames

    Joined:
    Jan 27, 2014
    Posts:
    168
    Do your bullet have the rigidbody component?
     
  3. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    Yes :/
     
  4. earrgames

    earrgames

    Joined:
    Jan 27, 2014
    Posts:
    168
    Ok, the script is very long and is hard to tell what the ploblem is, it can be something with your bullet object, something with the parameters, and somthing with the code.

    So your bullet and the sphere have the same rigidbody component atached with the same values? also the same scripts with the same values? Please give those a check, I can't believe the model of the object have something to do with it's behaviour.

    A last thing, your bullet is a custom model or a box?
     
  5. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    Yeah the parameters and the scripts are the same. And the bullet model is custom.
     
  6. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    Well The sphere doesn't use the bullet script it uses:

    Code (JavaScript):
    1. var windScript : WindManager;
    2. var Weight : float = .5;
    3. function Start()
    4. {
    5.         windScript = gameObject.GetComponent("WindManager");
    6. }
    7. function Update()
    8. {
    9.         var windInfluence = windScript.windSpeed / Weight;
    10.         rigidbody.AddForce(windScript.windDirection * windInfluence);
    11. }
     
  7. earrgames

    earrgames

    Joined:
    Jan 27, 2014
    Posts:
    168
    Uhm.... I can't really see the problem here, but maybe this can help you. Since using unity colliders is better(Better performance and stability), you can set the bullet model as a child of your sphere, then turn off the mesh renderer of the sphere, in that way you are using sphere's collider but rendering the bullet. Note that mesh collider doesn't work correctly with the physics engine usless you check the "convex" box in the mesh collider component.

    Hope this help!
     
  8. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    Thanks, I hadn't thought of that. I will give it a shot
     
  9. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    Well that didn't work. It's a problem with the first script (The wind is applied at lines 79 and 80 in case you were wondering) I believe it's a problem with the Rigidbody.Addforce not working on my bullet.
     
  10. earrgames

    earrgames

    Joined:
    Jan 27, 2014
    Posts:
    168
    Your rigidbody is checked in "Is kinematic"? it have to be false in order to the Addforce to work, besides that i don't see any reason for the Addforce not to be working, unless some of the vars involved in the wind turns to 0, also try lowering the mass of the bullet to a very little value, this way the minimum force aplied should affect the movement.
     
  11. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    Thanks but it still isn't working :(
     
  12. earrgames

    earrgames

    Joined:
    Jan 27, 2014
    Posts:
    168
    Uhm... What is the variable "Rotation" for?, I'm trying to implement that script but I don't know what objet to assign here.
     
  13. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    Just set the bullet to rotation.
     
  14. earrgames

    earrgames

    Joined:
    Jan 27, 2014
    Posts:
    168
    I tried the script and the bullet just falls... I think it has something to do with that "Rotation" var, I'll figure it out in a while. You know, it's better to code by yourself because you can know exactly what you're doing, that's the reason i prefer making simple scripts instead of trying to implement other guys code, I'm sure that remaking the code form scrath is not so hard, also that script have innesesary lines like the ones with materials.

    I'll reply you if got with the answer, in the mean time you can try to make a simplified version.

    Cheers!
     
  15. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    Thanks :)
     
  16. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    Also the actual first script is mine and the bullet is supposed to drop because of the bullet drop and the textures are for bullet holes :D
     
  17. earrgames

    earrgames

    Joined:
    Jan 27, 2014
    Posts:
    168
    In that case probably you'll find the answer yourself before anyone else, ;), but i'll figure out what the problem could be, or rework a piece of your code, I'll come up with something (I hope:p). See ya!
     
  18. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    Thanks it means a lot. I mean I did allready try my own code but that didn't work either :p