Search Unity

ETeeskiTutorials AI Path Finding Tutorial Scripts FPS1.41

Discussion in 'Community Learning & Teaching' started by eteeski, May 16, 2012.

  1. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    Click here to see the first person shooter tutorial, FPS1.41
    Here are all the scripts used in FPS1.41 to affect the behavior of the enemies. Also, there is a zip file of the project.

    FPS1.41 zip file:
    View attachment $FPS1.41.zip

    AIpathCellScript:
    Code (csharp):
    1.  
    2. var doors = new Array();
    3.  
    4. function OnTriggerEnter (other : Collider)
    5. {
    6.     if (other.tag == "AIpathDoor")
    7.         doors.Add(other.gameObject);
    8. }
    9.  
    AIpathDoorScirpt;
    Code (csharp):
    1.  
    2. var cells = new Array();
    3. var doorsToCells = new Array();
    4. var imediateCells = new Array();
    5. var testForCells : boolean = true;
    6. var waitToTestCells : float = 2;
    7. var stage : int = 1;
    8.  
    9. var animatedDoor : GameObject;
    10. @HideInInspector
    11. var doorOpen : boolean = true;
    12.  
    13. function Awake ()
    14. {
    15.     doorOpen = true;
    16.     cells = GameObject.FindGameObjectsWithTag("AIpathCell");
    17.     doorsToCells.length = cells.length;
    18.     testForCells = true;
    19.     waitToTestCells = 2;
    20.     stage = 1;
    21. }
    22.  
    23. function Update ()
    24. {
    25.     if (animatedDoor)
    26.         doorOpen = animatedDoor.GetComponent(DoorScript).open;
    27.  
    28.     if (testForCells  waitToTestCells <= 0)
    29.     {
    30.         for (var imediateCell : GameObject in imediateCells)
    31.         {
    32.             for (var i : int = 0; i <= cells.length - 1; i++)
    33.             {
    34.                 if (cells[i] == imediateCell)
    35.                     doorsToCells[i] = 1;
    36.             }
    37.         }
    38.        
    39.         for (stage = 2; stage <= cells.length; stage++)
    40.         {
    41.             for (i = 0; i <= cells.length - 1; i++)
    42.             {
    43.                 if (doorsToCells[i] == stage - 1)
    44.                     for (var checkDoor : GameObject in cells[i].GetComponent(AIpathCellScript).doors)
    45.                     {
    46.                         if (checkDoor != gameObject)
    47.                         {
    48.                             for (var checkCell : GameObject in checkDoor.GetComponent(AIpathDoorScript).imediateCells)
    49.                             {
    50.                                 for (var j : int = 0; j <= cells.length - 1; j++)
    51.                                 {
    52.                                     if (cells[j] == checkCell  doorsToCells[j] == null)
    53.                                         doorsToCells[j] = stage;
    54.                                 }
    55.                             }
    56.                         }
    57.                     }
    58.             }
    59.         }
    60.        
    61.         testForCells = false;
    62.         Debug.Log(doorsToCells);
    63.     }
    64.     waitToTestCells -= 1;
    65. }
    66.  
    67. function OnTriggerEnter (other : Collider)
    68. {
    69.     if (other.tag == "AIpathCell")
    70.         imediateCells.Add(other.gameObject);
    71. }
    72.  
    BulletScript:
    Code (csharp):
    1.  
    2. var maxDist : float = 1000000000;
    3. var decalHitWall : GameObject;
    4. var floatInFrontOfWall : float = 0.00001;
    5. var damage : float = 1;
    6. var bulletForce : float = 1000;
    7.  
    8. function Update ()
    9. {
    10.     var hit : RaycastHit;
    11.     if (Physics.Raycast(transform.position, transform.forward, hit))
    12.     {
    13.         if (decalHitWall  hit.transform.tag == "Level Parts")
    14.             Instantiate(decalHitWall, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    15.         if (hit.transform.tag == "EnemyBodyPart")
    16.         {
    17.             if (hit.transform.GetComponent(EnemyBodyPartScript).enemyBody.GetComponent(EnemyBodyScript).enemyHealth > damage * hit.transform.GetComponent(EnemyBodyPartScript).damageMultiplyer)
    18.                 hit.transform.GetComponent(EnemyBodyPartScript).enemyBody.GetComponent(EnemyBodyScript).enemyHealth -= damage * hit.transform.GetComponent(EnemyBodyPartScript).damageMultiplyer;
    19.             else
    20.             {
    21.                 hit.transform.GetComponent(EnemyBodyPartScript).enemyBody.GetComponent(EnemyBodyScript).enemyHealth -= damage * hit.transform.GetComponent(EnemyBodyPartScript).damageMultiplyer;
    22.                 hit.transform.GetComponent(EnemyBodyPartScript).addForceVector = transform.forward * bulletForce;
    23.             }
    24.         }
    25.     }
    26.     Destroy(gameObject);
    27. }
    28.  
    EnemyBodyPartScript;
    Code (csharp):
    1.  
    2. var enemyBody : GameObject;
    3. var addForceVector : Vector3;
    4. var damageMultiplyer : float = 1;
    5.  
    6. function Update ()
    7. {
    8.     if (addForceVector != Vector3.zero  !enemyBody.GetComponent(EnemyBodyScript).enabled)
    9.     {
    10.         if (rigidbody)
    11.             rigidbody.AddForce(addForceVector);
    12.         else
    13.             transform.parent.rigidbody.AddForce(addForceVector);
    14.         addForceVector = Vector3.zero;
    15.     }
    16. }
    17.  
    EnemyBodyScript:
    Code (csharp):
    1.  
    2. var moveDamp : float = 0.1;
    3. var parentToFollow : Transform;
    4. var parentLastPos : Vector3;
    5. var targetRotation : Quaternion;
    6. var turnSpeed : float = 10;
    7.  
    8. var velocityX : float;
    9. var velocityZ : float;
    10.  
    11. var parentScript : EnemyMovementScript;
    12. var walkAnimationString : String;
    13. var runAnimationString : String;
    14.  
    15. var enemyHealth : float = 1;
    16. var rigidbodies : Rigidbody[];
    17.  
    18. function Awake ()
    19. {
    20.     parentScript = transform.parent.GetComponent(EnemyMovementScript);
    21.     animation.wrapMode = WrapMode.Loop;
    22.     parentToFollow = transform.parent;
    23.     transform.parent = null;
    24. }
    25.  
    26. function Update ()
    27. {
    28.     transform.position.x = Mathf.SmoothDamp(transform.position.x, parentToFollow.position.x, velocityX, moveDamp);
    29.     transform.position.z = Mathf.SmoothDamp(transform.position.z, parentToFollow.position.z, velocityZ, moveDamp);
    30.    
    31.     var tempParentCurrentPos : Vector3 = parentToFollow.position;
    32.     var tempParentLastPos : Vector3 = parentLastPos;
    33.     tempParentCurrentPos.y = 0;
    34.     tempParentLastPos.y = 0;
    35.    
    36.     targetRotation = Quaternion.LookRotation(tempParentCurrentPos - tempParentLastPos);
    37.     parentLastPos = parentToFollow.position;
    38.    
    39.     transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
    40.    
    41.     var hits : RaycastHit[];
    42.     hits = Physics.RaycastAll(Ray(transform.position + Vector3(0,100,0), Vector3.up * -1));
    43.     for (var i : int = 0; i < hits.length; i++)
    44.     {
    45.         var hit : RaycastHit = hits[i];
    46.         if (hit.transform.gameObject == parentToFollow.gameObject.GetComponent(EnemyMovementScript).currentCell)
    47.             transform.position.y = hit.point.y;
    48.     }
    49.    
    50.     if (!animation.IsPlaying(walkAnimationString)  !parentScript.aware)
    51.         animation.Play(walkAnimationString);
    52.     if (!animation.IsPlaying(runAnimationString)  parentScript.aware)
    53.         animation.Play(runAnimationString);
    54.        
    55.     if (enemyHealth <= 0)
    56.     {
    57.         for (var rigidbodyFor in rigidbodies)
    58.             rigidbodyFor.isKinematic = false;
    59.         animation.enabled = false;
    60.         parentScript.enabled = false;
    61.         enabled = false;
    62.     }
    63. }
    64.  
    EnemyMovementScript:
    Code (csharp):
    1.  
    2. var currentCell : GameObject;
    3. var playerMovementScript : PlayerMovementScript;
    4. var playerTransform : Transform;
    5. var playerCell : GameObject;
    6. var goalDoor : GameObject;
    7. var shortestPathSoFar : float;
    8. @HideInInspector
    9. var waitToStart : int = 5;
    10. var maxMoveSpeed : float = 5;
    11. var minMoveSpeed : float = 1;
    12. var speedDamage : float = 0.5;
    13. var speedRecover : float = 1;
    14. var currentMoveSpeed : float = 5;
    15.  
    16. var randomizedCourse : boolean = false;
    17. var randomizeCourseVector : Vector3;
    18. var calculatedNewRandomizeCourseVector : boolean;
    19.  
    20. var lastPos : Vector3;
    21.  
    22. var viewAngle : float = 60;
    23. var aware : boolean = false;
    24. var unawareSpeed : float = 1;
    25.  
    26.  
    27.  
    28. function Awake ()
    29. {
    30.     shortestPathSoFar = Mathf.Infinity;
    31.     playerMovementScript = GameObject.FindWithTag("Player").GetComponent(PlayerMovementScript);
    32.     playerTransform = GameObject.FindWithTag("Player").transform;
    33.     waitToStart = 5;
    34.     randomizeCourseVector = transform.position;
    35.     lastPos = transform.position;
    36.     aware = false;
    37. }
    38.  
    39. function Update ()
    40. {
    41.     if (waitToStart <= 0)
    42.     {
    43.         playerCell = playerMovementScript.currentCell;
    44.         for (var doorCheckingNow : GameObject in currentCell.GetComponent(AIpathCellScript).doors)
    45.         {
    46.             for (var i : int = 0; i <= doorCheckingNow.GetComponent(AIpathDoorScript).cells.length - 1; i++)
    47.             {
    48.                 if (doorCheckingNow.GetComponent(AIpathDoorScript).cells[i] == playerCell)
    49.                 if (doorCheckingNow.GetComponent(AIpathDoorScript).doorsToCells[i] < shortestPathSoFar)
    50.                 {
    51.                     goalDoor = doorCheckingNow;
    52.                     shortestPathSoFar = doorCheckingNow.GetComponent(AIpathDoorScript).doorsToCells[i];
    53.                 }
    54.             }
    55.         }
    56.         shortestPathSoFar = Mathf.Infinity;
    57.     }
    58.     waitToStart -= 1;
    59.    
    60.     var hits : RaycastHit[];
    61.     var anyHit : boolean = false;
    62.     if (Vector3.Angle(transform.forward, playerTransform.position - transform.position) < viewAngle / 2  !aware)
    63.     {
    64.         hits = Physics.SphereCastAll(transform.position, transform.localScale.x / 3, playerTransform.position - transform.position, Vector3.Distance(transform.position, playerTransform.position));
    65.         for (var hit : RaycastHit in hits)
    66.         {
    67.             if (hit.transform.tag == "Level Parts")
    68.                 anyHit = true;
    69.         }
    70.         if (!anyHit)
    71.             aware = true;
    72.     }
    73.    
    74.     if (!aware)
    75.         goalDoor = null;
    76.    
    77.     if (!calculatedNewRandomizeCourseVector)
    78.     {
    79.         randomizeCourseVector = FindRandomSpotInCurrentCell();
    80.         calculatedNewRandomizeCourseVector = true;
    81.     }
    82.    
    83.     if (goalDoor)
    84.     if (!goalDoor.GetComponent(AIpathDoorScript).doorOpen)
    85.         goalDoor = null;
    86.    
    87.     if (currentCell != playerCell || playerCell == null || !aware)
    88.     {
    89.         if (randomizedCourse  goalDoor)
    90.             transform.position += (goalDoor.transform.position - transform.position).normalized * currentMoveSpeed * Time.deltaTime;
    91.         if (!randomizedCourse)
    92.         {
    93.             transform.position += (randomizeCourseVector - transform.position).normalized * currentMoveSpeed * Time.deltaTime;
    94.             if (Vector3.Distance(transform.position, randomizeCourseVector) < transform.localScale.x)
    95.             {
    96.                 if (goalDoor)
    97.                     randomizedCourse = true;
    98.                 if (goalDoor == null)
    99.                     calculatedNewRandomizeCourseVector = false;
    100.             }
    101.         }
    102.     }
    103.        
    104.     if (playerCell == currentCell  aware)
    105.         transform.position += (playerTransform.position - transform.position).normalized * currentMoveSpeed * Time.deltaTime;
    106.    
    107.     if (currentMoveSpeed < maxMoveSpeed  aware)
    108.         currentMoveSpeed += speedRecover * Time.deltaTime;
    109.     if (currentMoveSpeed > maxMoveSpeed  aware)
    110.         currentMoveSpeed = maxMoveSpeed;
    111.  
    112.     if (currentMoveSpeed < unawareSpeed  !aware)
    113.         currentMoveSpeed += speedRecover * Time.deltaTime;
    114.     if (currentMoveSpeed > unawareSpeed  !aware)
    115.         currentMoveSpeed = unawareSpeed;
    116.        
    117.     transform.rotation = Quaternion.LookRotation(transform.position - lastPos);
    118.     lastPos = transform.position;
    119. }
    120.  
    121. function OnTriggerEnter (hitTrigger : Collider)
    122. {
    123.     if (hitTrigger.tag == "AIpathCell")
    124.     {
    125.         currentCell = hitTrigger.gameObject;
    126.         randomizedCourse = false;
    127.         calculatedNewRandomizeCourseVector = false;
    128.     }
    129. }
    130.  
    131. function OnTriggerStay (hitTrigger : Collider)
    132. {
    133.     if (hitTrigger.tag == "Enemy"  hitTrigger.gameObject != gameObject)
    134.     {
    135.         if (currentMoveSpeed > minMoveSpeed)
    136.             currentMoveSpeed -= speedDamage;
    137.         transform.position += (transform.position - hitTrigger.transform.position).normalized * 0.1;
    138.     }
    139. }
    140.  
    141. function FindRandomSpotInCurrentCell ()
    142. {
    143.     return currentCell.transform.position + (currentCell.transform.rotation * Vector3(Random.Range(currentCell.transform.localScale.x * -0.5,currentCell.transform.localScale.x * 0.5),0,Random.Range(currentCell.transform.localScale.z * -0.5,currentCell.transform.localScale.z * 0.5)));
    144. }
    145.  
    PlayerMovementScript:
    Code (csharp):
    1.  
    2. var currentGun : GameObject;
    3. var distToPickUpGun : float = 6;
    4. var throwGunUpForce : float = 100;
    5. var throwGunForwardForce : float = 300;
    6. var waitFrameForSwitchGuns : int = -1;
    7.  
    8. var walkAcceleration : float = 5;
    9. var walkAccelAirRacio : float = 0.1;
    10. var walkDeacceleration : float = 5;
    11. @HideInInspector
    12. var walkDeaccelerationVolx : float;
    13. @HideInInspector
    14. var walkDeaccelerationVolz : float;
    15.  
    16. var cameraObject : GameObject;
    17. var maxWalkSpeed : float = 20;
    18. @HideInInspector
    19. var horizontalMovement : Vector2;
    20.  
    21. var jumpVelocity : float = 20;
    22. @HideInInspector
    23. var grounded : boolean = false;
    24. var maxSlope : float = 60;
    25.  
    26. var crouchRacio : float = 0.3;
    27. var transitionToCrouchSec : float = 0.2;
    28. var crouchingVelocity : float;
    29. var currentCrouchRacio : float = 1;
    30. var originalLocalScaleY : float;
    31. var crouchLocalScaleY : float;
    32. var collisionDetectionSphere : GameObject;
    33.  
    34. @HideInInspector
    35. var waitToPickupAmmo : float = 0;
    36.  
    37. var currentCell : GameObject;
    38.  
    39. function Awake ()
    40. {
    41.     currentCrouchRacio = 1;
    42.     originalLocalScaleY = transform.localScale.y;
    43.     crouchLocalScaleY = transform.localScale.y * crouchRacio;
    44. }
    45.  
    46. function LateUpdate ()
    47. {
    48.     waitFrameForSwitchGuns -= 1;
    49.     waitToPickupAmmo -= Time.deltaTime;
    50.    
    51.     transform.localScale.y = Mathf.Lerp(crouchLocalScaleY, originalLocalScaleY, currentCrouchRacio);
    52.     if (Input.GetButton("Crouch"))
    53.         currentCrouchRacio = Mathf.SmoothDamp(currentCrouchRacio, 0, crouchingVelocity, transitionToCrouchSec);
    54.     if (Input.GetButton("Crouch") == false  collisionDetectionSphere.GetComponent(CollsionDetectionSphereScript).collisionDetected == false)
    55.         currentCrouchRacio = Mathf.SmoothDamp(currentCrouchRacio, 1, crouchingVelocity, transitionToCrouchSec);
    56.    
    57.     horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
    58.     if (horizontalMovement.magnitude > maxWalkSpeed)
    59.     {
    60.         horizontalMovement = horizontalMovement.normalized;
    61.         horizontalMovement *= maxWalkSpeed;    
    62.     }
    63.    
    64.     rigidbody.velocity.x = horizontalMovement.x;
    65.     rigidbody.velocity.z = horizontalMovement.y;
    66.    
    67.     if (grounded){
    68.         rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelerationVolx, walkDeacceleration);
    69.         rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelerationVolz, walkDeacceleration);}
    70.    
    71.     transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
    72.    
    73.     /*if (grounded)
    74.         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
    75.     else
    76.         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRacio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRacio * Time.deltaTime);
    77.             */
    78.     if (Input.GetButtonDown("Jump")  grounded)
    79.         rigidbody.AddForce(0,jumpVelocity,0);
    80. }
    81.  
    82. function FixedUpdate ()
    83. {
    84.     if (grounded)
    85.         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration, 0, Input.GetAxis("Vertical") * walkAcceleration);
    86.     else
    87.         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRacio, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRacio);
    88. }
    89.  
    90. function OnCollisionStay (collision : Collision)
    91. {
    92.     for (var contact : ContactPoint in collision.contacts)
    93.     {
    94.         if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
    95.             grounded = true;
    96.     }
    97. }
    98.  
    99. function OnCollisionExit ()
    100. {
    101.     grounded = false;
    102. }
    103.  
    104. function OnTriggerExit ()
    105. {
    106.     currentCell = null;
    107. }
    108.  
    109. function OnTriggerStay (hitTrigger : Collider)
    110. {
    111.     if (hitTrigger.tag == "AIpathCell")
    112.         currentCell = hitTrigger.gameObject;
    113.  
    114.     if (hitTrigger.transform.tag == "StairGoingUp")
    115.         if (!Input.GetButton("Jump")  Vector3.Angle(rigidbody.velocity, hitTrigger.transform.forward) < 90)
    116.             if (rigidbody.velocity.y > 0)
    117.                 rigidbody.velocity.y = 0;
    118.     if (hitTrigger.transform.tag == "StairGoingDown")
    119.         if (!Input.GetButton("Jump")  Vector3.Angle(rigidbody.velocity, hitTrigger.transform.forward) < 90)
    120.             rigidbody.AddForce(0,-100,0);
    121.  
    122.  
    123.     var current : GunScript = null;
    124.     if (currentGun)
    125.         current = currentGun.GetComponent(GunScript);
    126.     var ammo : AmmoPickupScript = null;
    127.     var gun : GunScript = null;
    128.     if (hitTrigger.tag == "AmmoPickup"  waitToPickupAmmo <= 0)
    129.     {
    130.         ammo = hitTrigger.gameObject.GetComponent(AmmoPickupScript);
    131.         if (current.currentExtraAmmo < current.maxExtraAmmo)
    132.         {
    133.             if (ammo.fromGun)
    134.             {
    135.                 gun = ammo.gun.GetComponent(GunScript);
    136.                 if (gun.currentExtraAmmo > 0  gun.ammoType == current.ammoType  ammo.gun != currentGun)
    137.                 {
    138.                     if (gun.currentExtraAmmo >= current.maxExtraAmmo - current.currentExtraAmmo)
    139.                     {
    140.                         gun.currentExtraAmmo -= current.maxExtraAmmo - current.currentExtraAmmo;
    141.                         current.currentExtraAmmo = current.maxExtraAmmo;
    142.                     }
    143.                     if (gun.currentExtraAmmo < current.maxExtraAmmo - current.currentExtraAmmo)
    144.                     {
    145.                         current.currentExtraAmmo += gun.currentExtraAmmo;
    146.                         gun.currentExtraAmmo = 0;
    147.                     }
    148.                     if (ammo.pickupSound)
    149.                         ammo.gameObject.GetComponent(AudioSource).Play();
    150.                 }
    151.             }
    152.             if (!ammo.fromGun)
    153.             {
    154.                 if (current.ammoType == ammo.ammoType || ammo.ammoType == -1)
    155.                 {
    156.                     if (ammo.ammoAmount > 0  !ammo.unlimitedAmmo)
    157.                     {
    158.                         if (ammo.ammoAmount >= current.maxExtraAmmo - current.currentExtraAmmo)
    159.                         {
    160.                             ammo.ammoAmount -= current.maxExtraAmmo - current.currentExtraAmmo;
    161.                             current.currentExtraAmmo = current.maxExtraAmmo;
    162.                         }
    163.                         if (ammo.ammoAmount < current.maxExtraAmmo - current.currentExtraAmmo)
    164.                         {
    165.                             current.currentExtraAmmo += gun.currentExtraAmmo;
    166.                             ammo.ammoAmount = 0;
    167.                         }
    168.                         if (ammo.pickupSound)
    169.                             ammo.gameObject.GetComponent(AudioSource).Play();
    170.                     }
    171.                     if (ammo.unlimitedAmmo)
    172.                     {
    173.                         current.currentExtraAmmo = current.maxExtraAmmo;
    174.                         if (ammo.pickupSound)
    175.                             ammo.gameObject.GetComponent(AudioSource).Play();
    176.                     }
    177.                 }
    178.             }
    179.         }
    180.     }
    181. }
    182.  
     
  2. jonathan909

    jonathan909

    Joined:
    Oct 12, 2012
    Posts:
    1
    hey ETeeskiTutorials got a problem with that .addForceVector
    my gameobject is just a cube with no body parts so its not in a array..

    unity say its:
    MissingFieldException: Field 'target.addForceVector' not found.
    Boo.Lang.Runtime.DynamicDispatching.PropertyDispatcherFactory.FindExtension (IEnumerable`1 candidates)
    Boo.Lang.Runtime.DynamicDispatching.PropertyDispatcherFactory.Create (SetOrGet gos)
    Boo.Lang.Runtime.DynamicDispatching.PropertyDispatcherFactory.CreateSetter ()
    Boo.Lang.Runtime.RuntimeServices.DoCreatePropSetDispatcher (System.Object target, System.Type type, System.String name, System.Object value)
    Boo.Lang.Runtime.RuntimeServices.CreatePropSetDispatcher (System.Object target, System.String name, System.Object value)
    Boo.Lang.Runtime.RuntimeServices+<SetProperty>c__AnonStorey16.<>m__C ()



    the code i used is this:
    var maxDist : float = 1000000000;
    var decalHitWall : GameObject;
    var floatInFrontOfWall : float = 0.00001;
    var damage : float = 20;
    var bulletForce : float = 600;

    function Update ()
    {
    var hit : RaycastHit;
    if (Physics.Raycast(transform.position, transform.forward, hit, maxDist))
    {
    if (decalHitWall hit.transform.tag == "Level Parts")
    Instantiate(decalHitWall, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    if(hit.transform.tag == "targets"){
    if(hit.transform.GetComponent(target).targetHealth > damage)
    hit.transform.GetComponent(target).targetHealth -= damage;
    else
    {
    hit.transform.GetComponent(target).targetHealth -= damage;
    hit.transform.GetComponent(target).addForceVector = transform.forward * bulletForce;
    }
    }
    }
    Destroy(gameObject);
    }

    theres not much of a difference but what am i missing here.. =<