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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

ETeeskiTutorials AI Path Finding Tutorial Scripts FPS1.33

Discussion in 'Community Learning & Teaching' started by eteeski, Mar 12, 2012.

  1. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    Here are the scripts from FPS1.33 going over AI Path Finding, followed by the rest of the scripts from that game, and a zip file of the project.

    FPS1.33 Zip File:
    View attachment $FPS1.33.zip

    AIPathCellScript:
    Code (csharp):
    1.  
    2. var doors = new Array();
    3.  
    4. function OnTriggerEnter (other : Collider)
    5. {
    6.     if (other.tag == "AIpathDoor")
    7.         doors.Push(other.gameObject);
    8. }
    9.  
    AIPathDoorScript:
    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. @HideInInspector
    10. var doorOpen : boolean = true;
    11.  
    12. function Awake ()
    13. {
    14.     doorOpen = true;
    15.     cells = GameObject.FindGameObjectsWithTag("AIpathCell");
    16.     doorsToCells.length = cells.length;
    17.     testForCells = true;
    18.     waitToTestCells = 2;
    19.     stage = 1;
    20. }
    21.  
    22. function Update ()
    23. {
    24.     if (testForCells  waitToTestCells <= 0)
    25.     {
    26.         for (var imediateCell : GameObject in imediateCells)
    27.         {
    28.             for (var i : int = 0; i <= cells.length - 1; i++)
    29.             {
    30.                 if (cells[i] == imediateCell)
    31.                     doorsToCells[i] = 1;
    32.             }
    33.         }  
    34.        
    35.         for (stage = 2; stage <= cells.length; stage++)
    36.         {
    37.             for (i = 0; i <= cells.length - 1; i++)
    38.             {
    39.                 if (doorsToCells[i] == stage - 1)
    40.                     for (var checkDoor : GameObject in cells[i].GetComponent(AIpathCell).doors)
    41.                     {
    42.                         if (checkDoor != gameObject)
    43.                         {
    44.                             for (var checkCell : GameObject in checkDoor.GetComponent(AIpathDoor).imediateCells)
    45.                             {
    46.                                 for (var j : int = 0; j <= cells.length - 1; j++)
    47.                                 {
    48.                                     if (cells[j] == checkCell  doorsToCells[j] == null)
    49.                                         doorsToCells[j] = stage;
    50.                                 }
    51.                             }
    52.                         }
    53.                     }
    54.             }
    55.         }
    56.            
    57.        
    58.         testForCells = false;
    59.     }
    60.     waitToTestCells -= 1;
    61. }
    62.  
    63. function OnTriggerEnter (other : Collider)
    64. {
    65.     if (other.tag == "AIpathCell")
    66.         imediateCells.Add(other.gameObject);
    67. }
    68.  
    69.  
    BulletScript:
    Code (csharp):
    1.  
    2. var maxDist : float = 1000000000;
    3. var decalHitWall : GameObject;
    4. var floatInFrontOfWall : float = 0.00001;
    5.  
    6. function Update ()
    7. {
    8.     var hit : RaycastHit;
    9.     if (Physics.Raycast(transform.position, transform.forward, hit))
    10.     {
    11.         if (decalHitWall  hit.transform.tag == "Level Parts")
    12.             Instantiate(decalHitWall, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    13.     }
    14.     Destroy(gameObject);
    15. }
    16.  
    CollisionDetectionShereScript:
    Code (csharp):
    1.  
    2. var floatAbove : float = 0.1;
    3. var playerCapsuleTransform : Transform;
    4. @HideInInspector
    5. var collisionDetected : boolean = false;
    6.  
    7. function Update ()
    8. {
    9.     transform.position.x = playerCapsuleTransform.position.x;
    10.     transform.position.z = playerCapsuleTransform.position.z;
    11.     transform.position.y = playerCapsuleTransform.position.y + ((playerCapsuleTransform.GetComponent(Collider).height * playerCapsuleTransform.localScale.y) / 2) + (transform.localScale.y / 2) + floatAbove;
    12. }
    13.  
    14. function OnCollisionStay (collision : Collision)
    15. {
    16.     if (collision.transform.tag == "Level Parts")
    17.         collisionDetected = true;
    18. }
    19.  
    20. function OnCollisionExit ()
    21. {
    22.     collisionDetected = false;
    23. }
    24.  
    DestroyAfterTimeScript;
    Code (csharp):
    1.  
    2. var destroyAfterTime : float = 30;
    3. var destroyAfterTimeRandomization : float = 0;
    4. @HideInInspector
    5. var countToTime : float;
    6.  
    7. function Awake ()
    8. {
    9.     destroyAfterTime += Random.value * destroyAfterTimeRandomization;
    10. }
    11.  
    12. function Update ()
    13. {
    14.     countToTime += Time.deltaTime;
    15.     if (countToTime >= destroyAfterTime)
    16.         Destroy(gameObject);
    17. }
    18.  
    DoorScript:
    Code (csharp):
    1.  
    2. var open : boolean = false;
    3.  
    4. var openAnimationString : String;
    5. var closeAnimationString : String;
    6.  
    7. var buttonTransform : Transform;
    8. var distToOpen : float = 4;
    9.  
    10. @HideInInspector
    11. var playerTransform : Transform;
    12. @HideInInspector
    13. var cameraTransform : Transform;
    14.  
    15. var openSound : AudioClip;
    16. var closeSound : AudioClip;
    17.  
    18. function Awake ()
    19. {
    20.     playerTransform = GameObject.FindWithTag("Player").transform;
    21.     cameraTransform = GameObject.FindWithTag("MainCamera").transform;
    22.     if (open)
    23.         animation.Play(openAnimationString);
    24. }
    25.  
    26. function Update ()
    27. {
    28.     var alreadyChecked : boolean = false;
    29.     var angle : float = Vector3.Angle(buttonTransform.position - cameraTransform.position, buttonTransform.position + (cameraTransform.right * buttonTransform.localScale.magnitude) - cameraTransform.position);
    30.     if (Vector3.Distance(playerTransform.position,buttonTransform.position) <= distToOpen)
    31.     if (Vector3.Angle(buttonTransform.position - cameraTransform.position, cameraTransform.forward) <= angle)
    32.     if (Input.GetButtonDown("Use Key")  !animation.isPlaying)
    33.     {
    34.         if (open)
    35.         {
    36.             animation.Play(closeAnimationString);
    37.             open = false;
    38.             alreadyChecked = true;
    39.             if (closeSound)
    40.                 audio.PlayOneShot(closeSound);
    41.         }
    42.         if (!open  !alreadyChecked)
    43.         {
    44.             animation.Play(openAnimationString);
    45.             open = true;
    46.             if (openSound)
    47.                 audio.PlayOneShot(openSound);
    48.         }
    49.     }
    50. }
    51.  
    GunCameraScript:
    Code (csharp):
    1.  
    2. var cameraObject : GameObject;
    3.  
    4. function Awake ()
    5. {
    6.     cameraObject = GameObject.FindWithTag("MainCamera");
    7. }
    8.  
    9. function LateUpdate ()
    10. {
    11.     camera.fieldOfView = cameraObject.camera.fieldOfView;
    12. }
    13.  
    GunScript:
    Code (csharp):
    1.  
    2. var beingHeld : boolean = false;
    3. var outsideBox : GameObject;
    4. @HideInInspector
    5. var countToThrow : int = -1;
    6. @HideInInspector
    7. var playerTransform : Transform;
    8. @HideInInspector
    9. var playerMovementScript : PlayerMovementScript;
    10.  
    11. @HideInInspector
    12. var cameraObject : GameObject;
    13. @HideInInspector
    14. var cameraTransform : Transform;
    15. @HideInInspector
    16. var targetXRotation : float;
    17. @HideInInspector
    18. var targetYRotation : float;
    19. @HideInInspector
    20. var targetXRotationV : float;
    21. @HideInInspector
    22. var targetYRotationV : float;
    23.  
    24. var rotateSpeed : float = 0.3;
    25.  
    26. var holdHeight : float = -0.5;
    27. var holdSide : float = 0.5;
    28. var racioHipHold : float = 1;
    29. var hipToAimSpeed : float = 0.1;
    30. @HideInInspector
    31. var racioHipHoldV : float;
    32.  
    33. var aimRacio : float = 0.4;
    34.  
    35. var zoomAngle : float = 30;
    36.  
    37. var fireSpeed : float = 15;
    38. @HideInInspector
    39. var waitTilNextFire : float = 0;
    40. var bullet : GameObject;
    41. var bulletSpawn : GameObject;
    42.  
    43. var shootAngleRandomizationAiming : float = 5;
    44. var shootAngleRandomizationNotAiming : float = 15;
    45.  
    46. var recoilAmount : float = 0.5;
    47. var recoilRecoverTime : float = 0.2;
    48. @HideInInspector
    49. var currentRecoilZPos : float;
    50. @HideInInspector
    51. var currentRecoilZPosV : float;
    52.  
    53. var bulletSound : GameObject;
    54. var muzzelFlash : GameObject;
    55.  
    56. var gunbobAmountX : float = 0.5;
    57. var gunbobAmountY : float = 0.5;
    58. var currentGunbobX : float;
    59. var currentGunbobY : float;
    60.  
    61. var gunModelObjects : GameObject[];
    62.  
    63. var reloading : boolean = false;
    64. var reloadAnimation : Animation;
    65. var reloadAnimationString : String;
    66. var reloadSound : AudioSource;
    67.  
    68. var clipSize : int = 25;
    69. var currentClip : int = 25;
    70. var maxExtraAmmo : int = 100;
    71. var currentExtraAmmo : int = 100;
    72. var ammoType : int = 0;
    73.  
    74. var bulletHudTexture : Texture;
    75. var ammoCountRect : Rect = Rect(25,25,50,25);
    76. var ammoStartX : int = 100;
    77. var ammoY : int = 25;
    78. var ammoSize : Vector2 = Vector2(10,25);
    79. var ammoSpacing : int = 4;
    80. var ammoDecorationHudRect : Rect = Rect(25,50,50,25);
    81. var ammoDecorationTexture : Texture;
    82.  
    83. function Awake ()
    84. {
    85.     cameraTransform = GameObject.FindWithTag("MainCamera").transform;
    86.     countToThrow = -1;
    87.     playerTransform = GameObject.FindWithTag("Player").transform;
    88.     playerMovementScript = GameObject.FindWithTag("Player").GetComponent(PlayerMovementScript);
    89.     cameraObject = GameObject.FindWithTag("MainCamera");
    90. }
    91.  
    92. function LateUpdate ()
    93. {
    94. if (currentClip > clipSize)
    95.     currentClip = clipSize;
    96. if (currentExtraAmmo > maxExtraAmmo)
    97.     currentExtraAmmo = maxExtraAmmo;
    98. if (currentClip < 0)
    99.     currentClip = 0;
    100. if (currentExtraAmmo < 0)
    101.     currentExtraAmmo = 0;
    102.  
    103. if (beingHeld)
    104. {
    105.     if (!reloading  Input.GetButtonDown("Reload")  currentClip < clipSize  currentExtraAmmo > 0)
    106.     {
    107.         reloading = true;
    108.         reloadAnimation.Play(reloadAnimationString);
    109.         reloadSound.Play();
    110.     }
    111.     if (!reloading  Input.GetButtonDown("Fire1")  currentClip == 0  currentExtraAmmo > 0)
    112.     {
    113.         reloading = true;
    114.         reloadAnimation.Play(reloadAnimationString);
    115.         reloadSound.Play();
    116.     }
    117.     if (reloading  !reloadAnimation.IsPlaying(reloadAnimationString))
    118.     {
    119.         if (currentExtraAmmo >= clipSize - currentClip)
    120.         {
    121.             currentExtraAmmo -= clipSize - currentClip;
    122.             currentClip = clipSize;
    123.         }
    124.         if (currentExtraAmmo < clipSize - currentClip)
    125.         {
    126.             currentClip += currentExtraAmmo;
    127.             currentExtraAmmo = 0;
    128.         }
    129.         reloading = false;
    130.     }
    131.  
    132.     for(var modelObject : GameObject in gunModelObjects)
    133.     {
    134.         modelObject.layer = 8;
    135.     }
    136.  
    137.     rigidbody.useGravity = false;
    138.     outsideBox.GetComponent(Collider).enabled = false;
    139.    
    140.     currentGunbobX = Mathf.Sin(cameraObject.GetComponent(MouseLookScript).headbobStepCounter) * gunbobAmountX * racioHipHold;
    141.     currentGunbobY = Mathf.Cos(cameraObject.GetComponent(MouseLookScript).headbobStepCounter * 2) * gunbobAmountY * -1 * racioHipHold;
    142.    
    143.     var holdMuzzelFlash : GameObject;
    144.     var holdSound : GameObject;
    145.     if (Input.GetButton("Fire1")  currentClip > 0  !reloading)
    146.     {
    147.         if (waitTilNextFire <= 0)
    148.         {
    149.             currentClip -= 1;
    150.             if (bullet)
    151.                 Instantiate(bullet,bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    152.             if (bulletSound)
    153.                 holdSound = Instantiate(bulletSound, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    154.             if (muzzelFlash)
    155.                 holdMuzzelFlash = Instantiate(muzzelFlash, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    156.             targetXRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, racioHipHold);
    157.             targetYRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, racioHipHold);
    158.             currentRecoilZPos -= recoilAmount;
    159.             waitTilNextFire = 1;
    160.         }
    161.     }
    162.     waitTilNextFire -= Time.deltaTime * fireSpeed;
    163.  
    164.     if (holdSound)
    165.         holdSound.transform.parent = transform;
    166.     if (holdMuzzelFlash)
    167.         holdMuzzelFlash.transform.parent = transform;
    168.  
    169.     currentRecoilZPos = Mathf.SmoothDamp( currentRecoilZPos, 0, currentRecoilZPosV, recoilRecoverTime);
    170.  
    171.     cameraObject.GetComponent(MouseLookScript).currentTargetCameraAngle = zoomAngle;
    172.  
    173.     if (Input.GetButton("Fire2")  !reloading){
    174.         cameraObject.GetComponent(MouseLookScript).currentAimRacio = aimRacio;
    175.         racioHipHold = Mathf.SmoothDamp(racioHipHold, 0, racioHipHoldV, hipToAimSpeed);}
    176.     if (Input.GetButton("Fire2") == false || reloading){
    177.         cameraObject.GetComponent(MouseLookScript).currentAimRacio = 1;
    178.         racioHipHold = Mathf.SmoothDamp(racioHipHold, 1, racioHipHoldV, hipToAimSpeed);}
    179.  
    180.     transform.position = cameraObject.transform.position + (Quaternion.Euler(0,targetYRotation,0) * Vector3(holdSide * racioHipHold + currentGunbobX, holdHeight * racioHipHold + currentGunbobY, 0) + Quaternion.Euler(targetXRotation, targetYRotation, 0) * Vector3(0,0,currentRecoilZPos));
    181.    
    182.     targetXRotation = Mathf.SmoothDamp( targetXRotation, cameraObject.GetComponent(MouseLookScript).xRotation, targetXRotationV, rotateSpeed);
    183.     targetYRotation = Mathf.SmoothDamp( targetYRotation, cameraObject.GetComponent(MouseLookScript).yRotation, targetYRotationV, rotateSpeed);
    184.    
    185.     transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0);
    186. }
    187. if (!beingHeld)
    188. {
    189.     for(var modelObject : GameObject in gunModelObjects)
    190.     {
    191.         modelObject.layer = 0;
    192.     }
    193.  
    194.     rigidbody.useGravity = true;
    195.     outsideBox.GetComponent(Collider).enabled = true;
    196.    
    197.     countToThrow -= 1;
    198.     if (countToThrow == 0)
    199.         rigidbody.AddRelativeForce(0, playerMovementScript.throwGunUpForce, playerMovementScript.throwGunForwardForce);
    200.    
    201.     var angle : float = Vector3.Angle(outsideBox.transform.position - cameraTransform.position, outsideBox.transform.position + (cameraTransform.right * outsideBox.transform.localScale.magnitude) - cameraTransform.position);
    202.     if (Vector3.Angle(outsideBox.transform.position - cameraTransform.position, cameraTransform.forward) <= angle)
    203.     if (Vector3.Distance(transform.position, playerTransform.position) < playerMovementScript.distToPickUpGun  Input.GetButtonDown("Use Key")  playerMovementScript.waitFrameForSwitchGuns <= 0)
    204.     {
    205.         playerMovementScript.waitToPickupAmmo = 2;
    206.         playerMovementScript.currentGun.GetComponent(GunScript).beingHeld = false;
    207.         playerMovementScript.currentGun.GetComponent(GunScript).countToThrow = 2;
    208.         playerMovementScript.currentGun = gameObject;
    209.         beingHeld = true;
    210.         targetYRotation = cameraObject.GetComponent(MouseLookScript).yRotation - 180;
    211.         playerMovementScript.waitFrameForSwitchGuns = 2;
    212.     }
    213. }
    214. }
    215.  
    216. function OnGUI ()
    217. {
    218.     if (beingHeld)
    219.     {
    220.         for (var i : int = 1; i <= currentClip; i++)
    221.         {
    222.             GUI.DrawTexture(Rect(ammoStartX + ((i - 1) * (ammoSize.x + ammoSpacing)), ammoY, ammoSize.x, ammoSize.y), bulletHudTexture);
    223.         }
    224.         GUI.Label(ammoCountRect, currentExtraAmmo.ToString());
    225.         if (ammoDecorationTexture)
    226.             GUI.DrawTexture( ammoDecorationHudRect, ammoDecorationTexture);
    227.     }
    228. }
    229.  
    MouseLookScript:
    Code (csharp):
    1.  
    2. var defaultCameraAngle : float = 60;
    3. @HideInInspector
    4. var currentTargetCameraAngle : float = 60;
    5. @HideInInspector
    6. var racioZoom : float = 1;
    7. @HideInInspector
    8. var racioZoomV : float;
    9.  
    10. var racioZoomSpeed : float = 0.2;
    11.  
    12. var lookSensitivity : float = 5;
    13. @HideInInspector
    14. var yRotation : float;
    15. @HideInInspector
    16. var xRotation : float;
    17. @HideInInspector
    18. var currentYRotation : float;
    19. @HideInInspector
    20. var currentXRotation : float;
    21. @HideInInspector
    22. var yRotationV : float;
    23. @HideInInspector
    24. var xRotationV : float;
    25. var lookSmoothDamp : float = 0.1;
    26. @HideInInspector
    27. var currentAimRacio : float = 1;
    28.  
    29. var headbobSpeed : float = 1;
    30. @HideInInspector
    31. var headbobStepCounter : float;
    32. var headbobAmountX : float = 1;
    33. var headbobAmountY : float = 1;
    34. @HideInInspector
    35. var parentLastPos : Vector3;
    36. var eyeHeightRacio : float = 0.9;
    37.  
    38. function Awake ()
    39. {
    40.     parentLastPos = transform.parent.position;
    41. }
    42.  
    43. function Update ()
    44. {
    45.     if (transform.parent.GetComponent(PlayerMovementScript).grounded)
    46.         headbobStepCounter += Vector3.Distance(parentLastPos, transform.parent.position) * headbobSpeed;
    47.     transform.localPosition.x = Mathf.Sin(headbobStepCounter) * headbobAmountX * currentAimRacio;
    48.     transform.localPosition.y = (Mathf.Cos(headbobStepCounter * 2) * headbobAmountY * currentAimRacio) + (transform.parent.localScale.y * eyeHeightRacio) - (transform.parent.localScale.y / 2);
    49.    
    50.     parentLastPos = transform.parent.position;
    51.  
    52.     if (currentAimRacio == 1)
    53.         racioZoom = Mathf.SmoothDamp(racioZoom, 1, racioZoomV, racioZoomSpeed);
    54.     else
    55.         racioZoom = Mathf.SmoothDamp(racioZoom, 0, racioZoomV, racioZoomSpeed);
    56.        
    57.     camera.fieldOfView = Mathf.Lerp(currentTargetCameraAngle, defaultCameraAngle, racioZoom);
    58.  
    59.     yRotation += Input.GetAxis("Mouse X") * lookSensitivity * currentAimRacio;
    60.     xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity * currentAimRacio;
    61.    
    62.     xRotation = Mathf.Clamp(xRotation, -90, 90);
    63.    
    64.     currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, xRotationV, lookSmoothDamp);
    65.     currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, yRotationV, lookSmoothDamp);
    66.    
    67.     transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
    68. }
    69.  
    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. function Awake ()
    38. {
    39.     currentCrouchRacio = 1;
    40.     originalLocalScaleY = transform.localScale.y;
    41.     crouchLocalScaleY = transform.localScale.y * crouchRacio;
    42. }
    43.  
    44. function LateUpdate ()
    45. {
    46.     waitFrameForSwitchGuns -= 1;
    47.     waitToPickupAmmo -= Time.deltaTime;
    48.    
    49.     transform.localScale.y = Mathf.Lerp(crouchLocalScaleY, originalLocalScaleY, currentCrouchRacio);
    50.     if (Input.GetButton("Crouch"))
    51.         currentCrouchRacio = Mathf.SmoothDamp(currentCrouchRacio, 0, crouchingVelocity, transitionToCrouchSec);
    52.     if (Input.GetButton("Crouch") == false  collisionDetectionSphere.GetComponent(CollsionDetectionSphereScript).collisionDetected == false)
    53.         currentCrouchRacio = Mathf.SmoothDamp(currentCrouchRacio, 1, crouchingVelocity, transitionToCrouchSec);
    54.    
    55.     horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
    56.     if (horizontalMovement.magnitude > maxWalkSpeed)
    57.     {
    58.         horizontalMovement = horizontalMovement.normalized;
    59.         horizontalMovement *= maxWalkSpeed;    
    60.     }
    61.    
    62.     rigidbody.velocity.x = horizontalMovement.x;
    63.     rigidbody.velocity.z = horizontalMovement.y;
    64.    
    65.     if (grounded){
    66.         rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelerationVolx, walkDeacceleration);
    67.         rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelerationVolz, walkDeacceleration);}
    68.    
    69.     transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
    70.    
    71.     /*if (grounded)
    72.         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
    73.     else
    74.         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRacio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRacio * Time.deltaTime);
    75.             */
    76.     if (Input.GetButtonDown("Jump")  grounded)
    77.         rigidbody.AddForce(0,jumpVelocity,0);
    78. }
    79.  
    80. function FixedUpdate ()
    81. {
    82.     if (grounded)
    83.         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration, 0, Input.GetAxis("Vertical") * walkAcceleration);
    84.     else
    85.         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRacio, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRacio);
    86. }
    87.  
    88. function OnCollisionStay (collision : Collision)
    89. {
    90.     for (var contact : ContactPoint in collision.contacts)
    91.     {
    92.         if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
    93.             grounded = true;
    94.     }
    95. }
    96.  
    97. function OnCollisionExit ()
    98. {
    99.     grounded = false;
    100. }
    101.  
    102. function OnTriggerStay (hitTrigger : Collider)
    103. {
    104.     if (hitTrigger.transform.tag == "StairGoingUp")
    105.         if (!Input.GetButton("Jump")  Vector3.Angle(rigidbody.velocity, hitTrigger.transform.forward) < 90)
    106.             if (rigidbody.velocity.y > 0)
    107.                 rigidbody.velocity.y = 0;
    108.     if (hitTrigger.transform.tag == "StairGoingDown")
    109.         if (!Input.GetButton("Jump")  Vector3.Angle(rigidbody.velocity, hitTrigger.transform.forward) < 90)
    110.             rigidbody.AddForce(0,-100,0);
    111.  
    112.  
    113.     var current : GunScript = null;
    114.     if (currentGun)
    115.         current = currentGun.GetComponent(GunScript);
    116.     var ammo : AmmoPickupScript = null;
    117.     var gun : GunScript = null;
    118.     if (hitTrigger.tag == "AmmoPickup"  waitToPickupAmmo <= 0)
    119.     {
    120.         ammo = hitTrigger.gameObject.GetComponent(AmmoPickupScript);
    121.         if (current.currentExtraAmmo < current.maxExtraAmmo)
    122.         {
    123.             if (ammo.fromGun)
    124.             {
    125.                 gun = ammo.gun.GetComponent(GunScript);
    126.                 if (gun.currentExtraAmmo > 0  gun.ammoType == current.ammoType  ammo.gun != currentGun)
    127.                 {
    128.                     if (gun.currentExtraAmmo >= current.maxExtraAmmo - current.currentExtraAmmo)
    129.                     {
    130.                         gun.currentExtraAmmo -= current.maxExtraAmmo - current.currentExtraAmmo;
    131.                         current.currentExtraAmmo = current.maxExtraAmmo;
    132.                     }
    133.                     if (gun.currentExtraAmmo < current.maxExtraAmmo - current.currentExtraAmmo)
    134.                     {
    135.                         current.currentExtraAmmo += gun.currentExtraAmmo;
    136.                         gun.currentExtraAmmo = 0;
    137.                     }
    138.                     if (ammo.pickupSound)
    139.                         ammo.gameObject.GetComponent(AudioSource).Play();
    140.                 }
    141.             }
    142.             if (!ammo.fromGun)
    143.             {
    144.                 if (current.ammoType == ammo.ammoType || ammo.ammoType == -1)
    145.                 {
    146.                     if (ammo.ammoAmount > 0  !ammo.unlimitedAmmo)
    147.                     {
    148.                         if (ammo.ammoAmount >= current.maxExtraAmmo - current.currentExtraAmmo)
    149.                         {
    150.                             ammo.ammoAmount -= current.maxExtraAmmo - current.currentExtraAmmo;
    151.                             current.currentExtraAmmo = current.maxExtraAmmo;
    152.                         }
    153.                         if (ammo.ammoAmount < current.maxExtraAmmo - current.currentExtraAmmo)
    154.                         {
    155.                             current.currentExtraAmmo += gun.currentExtraAmmo;
    156.                             ammo.ammoAmount = 0;
    157.                         }
    158.                         if (ammo.pickupSound)
    159.                             ammo.gameObject.GetComponent(AudioSource).Play();
    160.                     }
    161.                     if (ammo.unlimitedAmmo)
    162.                     {
    163.                         current.currentExtraAmmo = current.maxExtraAmmo;
    164.                         if (ammo.pickupSound)
    165.                             ammo.gameObject.GetComponent(AudioSource).Play();
    166.                     }
    167.                 }
    168.             }
    169.         }
    170.     }
    171. }
    172.  
    AmmoPickupScript:
    Code (csharp):
    1.  
    2. var fromGun : boolean = false;
    3. var gun : GameObject;
    4. var ammoType : int = 0;
    5. var ammoAmount : int = 25;
    6. var unlimitedAmmo : boolean = false;
    7. var pickupSound : AudioClip;
    8.  
    FPS1.33 Zip File:
    View attachment $FPS1.33.zip

    PlayerMovementScript after FPS1.34:
    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 OnTriggerStay (hitTrigger : Collider)
    105. {
    106.     if (hitTrigger.tag == "AIpathCell")
    107.         currentCell = hitTrigger.gameObject;
    108.  
    109.     if (hitTrigger.transform.tag == "StairGoingUp")
    110.         if (!Input.GetButton("Jump")  Vector3.Angle(rigidbody.velocity, hitTrigger.transform.forward) < 90)
    111.             if (rigidbody.velocity.y > 0)
    112.                 rigidbody.velocity.y = 0;
    113.     if (hitTrigger.transform.tag == "StairGoingDown")
    114.         if (!Input.GetButton("Jump")  Vector3.Angle(rigidbody.velocity, hitTrigger.transform.forward) < 90)
    115.             rigidbody.AddForce(0,-100,0);
    116.  
    117.  
    118.     var current : GunScript = null;
    119.     if (currentGun)
    120.         current = currentGun.GetComponent(GunScript);
    121.     var ammo : AmmoPickupScript = null;
    122.     var gun : GunScript = null;
    123.     if (hitTrigger.tag == "AmmoPickup"  waitToPickupAmmo <= 0)
    124.     {
    125.         ammo = hitTrigger.gameObject.GetComponent(AmmoPickupScript);
    126.         if (current.currentExtraAmmo < current.maxExtraAmmo)
    127.         {
    128.             if (ammo.fromGun)
    129.             {
    130.                 gun = ammo.gun.GetComponent(GunScript);
    131.                 if (gun.currentExtraAmmo > 0  gun.ammoType == current.ammoType  ammo.gun != currentGun)
    132.                 {
    133.                     if (gun.currentExtraAmmo >= current.maxExtraAmmo - current.currentExtraAmmo)
    134.                     {
    135.                         gun.currentExtraAmmo -= current.maxExtraAmmo - current.currentExtraAmmo;
    136.                         current.currentExtraAmmo = current.maxExtraAmmo;
    137.                     }
    138.                     if (gun.currentExtraAmmo < current.maxExtraAmmo - current.currentExtraAmmo)
    139.                     {
    140.                         current.currentExtraAmmo += gun.currentExtraAmmo;
    141.                         gun.currentExtraAmmo = 0;
    142.                     }
    143.                     if (ammo.pickupSound)
    144.                         ammo.gameObject.GetComponent(AudioSource).Play();
    145.                 }
    146.             }
    147.             if (!ammo.fromGun)
    148.             {
    149.                 if (current.ammoType == ammo.ammoType || ammo.ammoType == -1)
    150.                 {
    151.                     if (ammo.ammoAmount > 0  !ammo.unlimitedAmmo)
    152.                     {
    153.                         if (ammo.ammoAmount >= current.maxExtraAmmo - current.currentExtraAmmo)
    154.                         {
    155.                             ammo.ammoAmount -= current.maxExtraAmmo - current.currentExtraAmmo;
    156.                             current.currentExtraAmmo = current.maxExtraAmmo;
    157.                         }
    158.                         if (ammo.ammoAmount < current.maxExtraAmmo - current.currentExtraAmmo)
    159.                         {
    160.                             current.currentExtraAmmo += gun.currentExtraAmmo;
    161.                             ammo.ammoAmount = 0;
    162.                         }
    163.                         if (ammo.pickupSound)
    164.                             ammo.gameObject.GetComponent(AudioSource).Play();
    165.                     }
    166.                     if (ammo.unlimitedAmmo)
    167.                     {
    168.                         current.currentExtraAmmo = current.maxExtraAmmo;
    169.                         if (ammo.pickupSound)
    170.                             ammo.gameObject.GetComponent(AudioSource).Play();
    171.                     }
    172.                 }
    173.             }
    174.         }
    175.     }
    176. }
    177.  
    EnemyMovementScript:
    Code (csharp):
    1.  
    2. var currentCell : GameObject;
    3. var playerMovementScript : PlayerMovementScript;
    4. var playerCell : GameObject;
    5. var goalDoor : GameObject;
    6. var shortestPathSoFar : float;
    7. @HideInInspector
    8. var waitToStart : int = 5;
    9.  
    10. function Awake ()
    11. {
    12.     shortestPathSoFar = Mathf.Infinity;
    13.     playerMovementScript = GameObject.FindWithTag("Player").GetComponent(PlayerMovementScript);
    14.     waitToStart = 5;
    15. }
    16.  
    17. function Update ()
    18. {
    19.     if (waitToStart <= 0)
    20.     {
    21.         playerCell = playerMovementScript.currentCell;
    22.         for (var doorCheckingNow : GameObject in currentCell.GetComponent(AIpathCellScript).doors)
    23.         {
    24.             for (var i : int = 0; i <= doorCheckingNow.GetComponent(AIpathDoorScript).cells.length - 1; i++)
    25.             {
    26.                 if (doorCheckingNow.GetComponent(AIpathDoorScript).cells[i] == playerCell)
    27.                 if (doorCheckingNow.GetComponent(AIpathDoorScript).doorsToCells[i] < shortestPathSoFar)
    28.                 {
    29.                     goalDoor = doorCheckingNow;
    30.                     shortestPathSoFar = doorCheckingNow.GetComponent(AIpathDoorScript).doorsToCells[i];
    31.                 }
    32.             }
    33.         }
    34.         shortestPathSoFar = Mathf.Infinity;
    35.     }
    36.     waitToStart -= 1;
    37. }
    38.  
    39. function OnTriggerEnter (hitTrigger : Collider)
    40. {
    41.     if (hitTrigger.tag == "AIpathCell")
    42.         currentCell = hitTrigger.gameObject;
    43. }
    44.  
     
    Last edited: Mar 27, 2012
    matthewaranzanso likes this.
  2. love

    love

    Joined:
    Dec 14, 2011
    Posts:
    16
    hi Eteeski how make Bullet Holes parent with the object When the moving object moving with him the Holes
     
  3. omarzonex

    omarzonex

    Joined:
    Jan 16, 2012
    Posts:
    158
    wow very nice tutorial New 1.34

    thank you very very much

    :)
     
  4. ABNV09

    ABNV09

    Joined:
    Sep 3, 2012
    Posts:
    3
    I am not able to download the .zip file :(
     
  5. Rheeenz!

    Rheeenz!

    Joined:
    Nov 23, 2012
    Posts:
    7
    Hello There ETeeski, I have a question. Why my door doesn't open even if my door have an animation for closing and opening. I'm pretty sure that I have the right code because I already copy it to your thread. Thank you..
     
  6. FirePlantGames

    FirePlantGames

    Joined:
    Dec 11, 2012
    Posts:
    49
    use this script that me and squared 55 made :
    Code (csharp):
    1.  
    2. var maxDist : float = 1000000000;
    3. var decalHitWall : GameObject;
    4. var floatInFrontOfWall : float = 0.00001;
    5.  
    6.  
    7.  
    8. function Update ()
    9. {
    10. var hit : RaycastHit;
    11. if (Physics.Raycast(transform.position,transform.forward,hit,maxDist))
    12. {
    13. if (decalHitWall  hit.transform.tag == "Level Parts")
    14. {
    15. var bulletHole = Instantiate(decalHitWall, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    16. bulletHole.transform.parent = hit.transform;
    17. }
    18. }
    19. Destroy(gameObject);
    20. }
    21.  
     
  7. LordSire

    LordSire

    Joined:
    Nov 9, 2012
    Posts:
    1
    Eteeski

    I have just finished the AI path finding 1.33
    when i use this line
    Debug.Log(doorsToCells);
    the compiler returns to me
    ,,,,,,,,,,,,,,,,
    it does not show any numbers
    i tried deleting some cells and door to make it smaller but i got the same result.
    i have also gone through my script multiple times comparing it to yours and it is written just like yours
    Can you help me?
     
  8. onelove

    onelove

    Joined:
    Jan 31, 2013
    Posts:
    3
    hey ETeeski, great tutorials, have been learning a lot from your tutorials, but somehow i get a glitch with the ai path, every time the enemy chases me, it gets stuck in some doors spinning out of control, is there a simple way to solve this,
     
  9. AfricanMoney

    AfricanMoney

    Joined:
    Apr 20, 2013
    Posts:
    1
    Hey ETeeski I have a error in this line of code:
    for (var checkDoor : GameObject in cells.GetComponent(AIpathCellScript).doors)

    it says the 'GetComponent' is not a member of 'Object'
    What can I do to change fix this??
     
  10. walkirie

    walkirie

    Joined:
    Oct 3, 2016
    Posts:
    2
    GunScript in C#Sharp please?