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

Glitches, Code Errors, FPS Related Problems

Discussion in 'Community Learning & Teaching' started by Trikstamachine, Mar 10, 2013.

  1. Trikstamachine

    Trikstamachine

    Joined:
    Mar 2, 2013
    Posts:
    9
    Code (csharp):
    1. var beingHeld : boolean = false;
    2. var outsideBox : GameObject;
    3. @HideInInspector
    4. var countToThrow : int = -1;
    5. @HideInInspector
    6. var playerTransform : Transform;
    7. @HideInInspector
    8. var playerMovementScript : PlayerMovementScript;
    9.  
    10. @HideInInspector
    11. var cameraObject : GameObject;
    12. @HideInInspector
    13. var targetXRotation : float;
    14. @HideInInspector
    15. var targetYRotation : float;
    16. @HideInInspector
    17. var targetXRotationV : float;
    18. @HideInInspector
    19. var targetYRotationV : float;
    20.  
    21. var rotateSpeed : float = 0.3;
    22.  
    23. var holdHeight : float = -0.5;
    24. var holdSide : float = 0.5;
    25. var racioHipHold : float = 1;
    26. var hipToAimSpeed : float = 0.1;
    27. @HideInInspector
    28. var racioHipHoldV : float;
    29.  
    30. var aimRacio : float = 0.4;
    31.  
    32. var zoomAngle : float = 30;
    33.  
    34. var fireSpeed : float = 15;
    35. @HideInInspector
    36. var waitTilNextFire : float = 0;
    37. var bullet : GameObject;
    38. var bulletSpawn : GameObject;
    39.  
    40. var shootAngleRandomizationAiming : float = 5;
    41. var shootAngleRandomizationNotAiming : float = 15;
    42.  
    43. var recoilAmount : float = 0.5;
    44. var recoilRecoverTime : float = 0.2;
    45. @HideInInspector
    46. var currentRecoilZPos : float;
    47. @HideInInspector
    48. var currentRecoilZPosV : float;
    49.  
    50. var bulletSound : GameObject;
    51. var muzzelFlash : GameObject;
    52.  
    53. var gunbobAmountX : float = 0.5;
    54. var gunbobAmountY : float = 0.5;
    55. var currentGunbobX : float;
    56. var currentGunbobY : float;
    57.  
    58. function Awake ()
    59. {
    60.     countToThrow = -1;
    61.     playerTransform = GameObject.FindWithTag("Player").transform;
    62.     playerMovementScript = GameObject.FindWithTag("Player").GetComponent(PlayerMovementScript);
    63.     cameraObject = GameObject.FindWithTag("MainCamera");
    64. }
    65.  
    66. function LateUpdate ()
    67. {
    68. if (beingHeld)
    69. {
    70.     rigidbody.useGravity = false;
    71.     outsideBox.GetComponent(Collider).enabled = false;
    72.  
    73.     currentGunbobX = Mathf.Sin(cameraObject.GetComponent(MouseLookScript).headbobStepCounter) * gunbobAmountX * racioHipHold;
    74.     currentGunbobY = Mathf.Cos(cameraObject.GetComponent(MouseLookScript).headbobStepCounter * 2) * gunbobAmountY * -1 * racioHipHold;
    75.  
    76.     var holdMuzzelFlash : GameObject;
    77.     var holdSound : GameObject;
    78.     if (Input.GetButton("Fire1"))
    79.     {
    80.         if (waitTilNextFire <= 0)
    81.         {
    82.             if (bullet)
    83.                 Instantiate(bullet,bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    84.             if (bulletSound)
    85.                 holdSound = Instantiate(bulletSound, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    86.             if (muzzelFlash)
    87.                 holdMuzzelFlash = Instantiate(muzzelFlash, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    88.             targetXRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, racioHipHold);
    89.             targetYRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, racioHipHold);
    90.             currentRecoilZPos -= recoilAmount;
    91.             waitTilNextFire = 1;
    92.         }
    93.     }
    94.     waitTilNextFire -= Time.deltaTime * fireSpeed;
    95.  
    96.     if (holdSound)
    97.         holdSound.transform.parent = transform;
    98.     if (holdMuzzelFlash)
    99.         holdMuzzelFlash.transform.parent = transform;
    100.  
    101.     currentRecoilZPos = Mathf.SmoothDamp( currentRecoilZPos, 0, currentRecoilZPosV, recoilRecoverTime);
    102.  
    103.     cameraObject.GetComponent(MouseLookScript).currentTargetCameraAngle = zoomAngle;
    104.  
    105.     if (Input.GetButton("Fire2")){
    106.         cameraObject.GetComponent(MouseLookScript).currentAimRacio = aimRacio;
    107.         racioHipHold = Mathf.SmoothDamp(racioHipHold, 0, racioHipHoldV, hipToAimSpeed);}
    108.     if (Input.GetButton("Fire2") == false){
    109.         cameraObject.GetComponent(MouseLookScript).currentAimRacio = 1;
    110.         racioHipHold = Mathf.SmoothDamp(racioHipHold, 1, racioHipHoldV, hipToAimSpeed);}
    111.  
    112.     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));
    113.    
    114.     targetXRotation = Mathf.SmoothDamp( targetXRotation, cameraObject.GetComponent(MouseLookScript).xRotation, targetXRotationV, rotateSpeed);
    115.     targetYRotation = Mathf.SmoothDamp( targetYRotation, cameraObject.GetComponent(MouseLookScript).yRotation, targetYRotationV, rotateSpeed);
    116.    
    117.     transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0);
    118. }
    119. if (!beingHeld)
    120. {
    121.     rigidbody.useGravity = true;
    122.     outsideBox.GetComponent(Collider).enabled = true;
    123.    
    124.     countToThrow -= 1;
    125.     if (countToThrow == 0)
    126.         rigidbody.AddRelativeForce(0, playerMovementScript.throwGunUpForce, playerMovementScript.throwGunFowardForce);
    127.        
    128.     if (Vector3.Distance(transform.position, playerTransform.position) < playerMovementScript.distToPickUpGun  Input.GetButtonDown("Use Key")  playerMovementScript.waitFrameForSwitchGuns <= 0)
    129.     {
    130.         playerMovementScript.currentGun.GetComponent(GunScript).beingHeld = false;
    131.         playerMovementScript.currentGun.GetComponent(GunScript).countToThrow = 2;
    132.         playerMovementScript.currentGun = gameObject;
    133.         beingHeld = true;
    134.         targetYRotation = cameraObject.GetComponent(MouseLookScript).yRotation - 180;
    135.         playerMovementScript.waitForFrameToSwitchGuns = 2;
    136.     }
    137. }
    138. }
    139.  
    140.  


    Code (csharp):
    1. var currentGun : GameObject;
    2. var distToPickUpGun : float = 6;
    3. var throwGunUpForce : float = 100;
    4. var throwGunFowardForce : float = 300;
    5. var waitFrameForSwitchGuns : int = -1;
    6.  
    7. var walkAcceleration : float = 5;
    8. var walkAccelAirRacio : float = 0.1;
    9. var walkDeacceleration : float = 5;
    10. @HideInInspector
    11. var walkDeaccelerationVolx : float;
    12. @HideInInspector
    13. var walkDeaccelerationVolz : float;
    14.  
    15. var cameraObject : GameObject;
    16. var maxWalkSpeed : float = 20;
    17. @HideInInspector
    18. var horizontalMovement : Vector2;
    19.  
    20. var jumpVelocity : float = 20;
    21. @HideInInspector
    22. var grounded : boolean = false;
    23. var maxSlope : float = 60;
    24.  
    25. var crouchRacio : float = 0.3;
    26. var transitionToCrouchSec : float = 0.2;
    27. var crouchingVelocity : float;
    28. var currentCrouchRacio : float = 1;
    29. var originalLocalScaleY : float;
    30. var crouchLocalScaleY : float;
    31. var collisionDetectionSphere : GameObject;
    32.  
    33. function Awake ()
    34. {
    35.     currentCrouchRacio = 1;
    36.     originalLocalScaleY = transform.localScale.y;
    37.     crouchLocalScaleY = transform.localScale.y * crouchRacio;
    38. }
    39.  
    40. function LateUpdate ()
    41. {
    42.     waitFrameForSwitchGuns -= 1;
    43.  
    44.     transform.localScale.y = Mathf.Lerp(crouchLocalScaleY, originalLocalScaleY, currentCrouchRacio);
    45.     if (Input.GetButton("Crouch"))
    46.         currentCrouchRacio = Mathf.SmoothDamp(currentCrouchRacio, 0, crouchingVelocity, transitionToCrouchSec);
    47.     if (Input.GetButton("Crouch") == false  collisionDetectionSphere.GetComponent(CollisionDetectionSphereScript).collisionDetected == false)
    48.         currentCrouchRacio = Mathf.SmoothDamp(currentCrouchRacio, 1, crouchingVelocity, transitionToCrouchSec);
    49.  
    50.     horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
    51.     if (horizontalMovement.magnitude > maxWalkSpeed)
    52.     {
    53.         horizontalMovement = horizontalMovement.normalized;
    54.         horizontalMovement *= maxWalkSpeed;    
    55.     }
    56.     rigidbody.velocity.x = horizontalMovement.x;
    57.     rigidbody.velocity.z = horizontalMovement.y;
    58.    
    59.     if (grounded){
    60.         rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelerationVolx, walkDeacceleration);
    61.         rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelerationVolz, walkDeacceleration);}
    62.    
    63.     transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
    64.    
    65.     if (grounded)
    66.         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
    67.     else
    68.         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRacio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRacio * Time.deltaTime);
    69.            
    70.     if (Input.GetButtonDown("Jump")  grounded)
    71.         rigidbody.AddForce(0,jumpVelocity,0);
    72. }
    73.  
    74. function OnCollisionStay (collision : Collision)
    75. {
    76.     for (var contact : ContactPoint in collision.contacts)
    77.     {
    78.         if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
    79.             grounded = true;
    80.     }
    81. }
    82.  
    83. function OnCollisionExit ()
    84. {
    85.     grounded = false;
    86. }
    I cannot pickup a different weapon..


    Code (csharp):
    1. var floatAbove : float = 0.1;
    2. var playerCapsuleTransform : Transform;
    3. @HideInInspector
    4. var collisionDetected : boolean = false;
    5.  
    6. function Update ()
    7. {
    8.     transform.position.x = playerCapsuleTransform.position.x;
    9.     transform.position.z = playerCapsuleTransform.position.z;
    10.         transform.position.y = playerCapsuleTransform.position.y + ((playerCapsuleTransform.GetComponent(Collider.height * playerCapsuleTransform.localScale.y) / 2) + (transform.localScale.y / 2) + floatAbove);
    11. }
    12.  
    13. function OnCollisionStay (collision : Collision)
    14. {
    15.     if (collision.transform.tag == "Level Parts");
    16.         collisionDetected = true;
    17. }
    18.  
    19. function OnCollisionExit ()
    20. {
    21.     collisionDetected = false;
    22. }
    And I am getting a 'Null reference error' from my CollisionDetectionSphere.