Search Unity

[FPS] Problem: glitchy walking

Discussion in 'Scripting' started by DrSega, Mar 6, 2012.

  1. DrSega

    DrSega

    Joined:
    Mar 6, 2012
    Posts:
    157
    Hello, im following ETeeskiTutorials and im stuck on FPS tutorials 1.23

    My problem is when i walk my screen shakes like crazy and ill post scripts so u can see.

    MouseLookScript
    Code (csharp):
    1. var defaultCameraAngle : float = 60;
    2.  
    3. @HideInInspector
    4.  
    5. var currentTargetCameraAngle : float = 60;
    6.  
    7. @HideInInspector
    8.  
    9. var racioZoom : float = 1;
    10.  
    11. @HideInInspector
    12.  
    13. var racioZoomV : float;
    14.  
    15.  
    16.  
    17. var racioZoomSpeed : float = 0.2;
    18.  
    19.  
    20.  
    21. var lookSensitivity : float = 5;
    22.  
    23. @HideInInspector
    24.  
    25. var yRotation : float;
    26.  
    27. @HideInInspector
    28.  
    29. var xRotation : float;
    30.  
    31. @HideInInspector
    32.  
    33. var currentYRotation : float;
    34.  
    35. @HideInInspector
    36.  
    37. var currentXRotation : float;
    38.  
    39. @HideInInspector
    40.  
    41. var yRotationV : float;
    42.  
    43. @HideInInspector
    44.  
    45. var xRotationV : float;
    46.  
    47. var lookSmoothDamp : float = 0.1;
    48.  
    49. @HideInInspector
    50.  
    51. var currentAimRacio : float = 1;
    52.  
    53.  
    54.  
    55. var headbobSpeed : float = 1;
    56.  
    57. @HideInInspector
    58.  
    59. var headbobStepCounter : float;
    60.  
    61. var headbobAmountX : float = 1;
    62.  
    63. var headbobAmountY : float = 1;
    64.  
    65. @HideInInspector
    66.  
    67. var parentLastPos : Vector3;
    68.  
    69. var eyeHeightRacio : float = 0.9;
    70.  
    71.  
    72.  
    73.  
    74.  
    75. function Awake ()
    76.  
    77. {
    78.  
    79.     parentLastPos = transform.parent.position;
    80.  
    81. }
    82.  
    83.  
    84.  
    85. function Update ()
    86.  
    87. {
    88.  
    89.     if (transform.parent.GetComponent(PlayerMovementScript).grounded)
    90.  
    91.         headbobStepCounter += Vector3.Distance(parentLastPos, transform.parent.position) * headbobSpeed;
    92.  
    93.     transform.localPosition.x = Mathf.Sin(headbobStepCounter) * headbobAmountX * currentAimRacio;
    94.  
    95.     transform.localPosition.y = (Mathf.Cos(headbobStepCounter * 2) * headbobAmountY * -1 * currentAimRacio) + (transform.parent.localScale.y * eyeHeightRacio) - (transform.parent.localScale.y / 2);
    96.  
    97.    
    98.  
    99.     parentLastPos = transform.parent.position;
    100.  
    101.    
    102.  
    103.     if (currentAimRacio == 1)
    104.  
    105.         racioZoom = Mathf.SmoothDamp(racioZoom, 1, racioZoomV, racioZoomSpeed);
    106.  
    107.     else
    108.  
    109.         racioZoom = Mathf.SmoothDamp(racioZoom, 0, racioZoomV, racioZoomSpeed);
    110.  
    111.        
    112.  
    113.     camera.fieldOfView = Mathf.Lerp(currentTargetCameraAngle, defaultCameraAngle, racioZoom);
    114.  
    115.  
    116.  
    117.     yRotation += Input.GetAxis("Mouse X") * lookSensitivity * currentAimRacio;
    118.  
    119.     xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity * currentAimRacio;
    120.  
    121.    
    122.  
    123.     xRotation = Mathf.Clamp(xRotation, -90, 90);
    124.  
    125.    
    126.  
    127.     currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, xRotationV, lookSmoothDamp);
    128.  
    129.     currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, yRotationV, lookSmoothDamp);
    130.  
    131.    
    132.  
    133.     transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
    134.  
    135. }
    PlayerMovementScript
    Code (csharp):
    1. var currentGun : GameObject;
    2.  
    3. var distToPickUpGun : float = 6;
    4.  
    5. var throwGunUpForce : float = 100;
    6.  
    7. var throwGunForwardForce : float = 300;
    8.  
    9. var waitFrameForSwitchGuns : int = -1;
    10.  
    11.  
    12.  
    13. var walkAcceleration : float = 5;
    14.  
    15. var walkAccelAirRacio : float = 0.1;
    16.  
    17. var walkDeacceleration : float = 5;
    18.  
    19. @HideInInspector
    20.  
    21. var walkDeaccelerationVolx : float;
    22.  
    23. @HideInInspector
    24.  
    25. var walkDeaccelerationVolz : float;
    26.  
    27.  
    28.  
    29. var cameraObject : GameObject;
    30.  
    31. var maxWalkSpeed : float = 20;
    32.  
    33. @HideInInspector
    34.  
    35. var horizontalMovement : Vector2;
    36.  
    37.  
    38.  
    39. var jumpVelocity : float = 20;
    40.  
    41. @HideInInspector
    42.  
    43. var grounded : boolean = false;
    44.  
    45. var maxSlope : float = 60;
    46.  
    47.  
    48.  
    49. var crouchRacio : float = 0.3;
    50.  
    51. var transitionToCrouchSec : float = 0.2;
    52.  
    53. var crouchingVelocity : float;
    54.  
    55. var currentCrouchRacio : float = 1;
    56.  
    57. var originalLocalScaleY : float;
    58.  
    59. var crouchLocalScaleY : float;
    60.  
    61. var collisionDetectionSphere : GameObject;
    62.  
    63.  
    64.  
    65. function Awake ()
    66.  
    67. {
    68.  
    69.     currentCrouchRacio = 1;
    70.  
    71.     originalLocalScaleY = transform.localScale.y;
    72.  
    73.     crouchLocalScaleY = transform.localScale.y * crouchRacio;
    74.  
    75. }
    76.  
    77.  
    78.  
    79. function LateUpdate ()
    80.  
    81. {
    82.  
    83.     waitFrameForSwitchGuns -= 1;
    84.  
    85.  
    86.  
    87.     transform.localScale.y = Mathf.Lerp(crouchLocalScaleY, originalLocalScaleY, currentCrouchRacio);
    88.  
    89.     if (Input.GetButton("Crouch"))
    90.  
    91.         currentCrouchRacio = Mathf.SmoothDamp(currentCrouchRacio, 0, crouchingVelocity, transitionToCrouchSec);
    92.  
    93.     if (Input.GetButton("Crouch") == false  collisionDetectionSphere.GetComponent(CollisionDetectionSphereScript).collisionDetected == false)
    94.  
    95.         currentCrouchRacio = Mathf.SmoothDamp(currentCrouchRacio, 1, crouchingVelocity, transitionToCrouchSec);
    96.  
    97.    
    98.  
    99.     horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
    100.  
    101.     if (horizontalMovement.magnitude > maxWalkSpeed)
    102.  
    103.     {
    104.  
    105.         horizontalMovement = horizontalMovement.normalized;
    106.  
    107.         horizontalMovement *= maxWalkSpeed;    
    108.  
    109.     }
    110.  
    111.    
    112.  
    113.     rigidbody.velocity.x = horizontalMovement.x;
    114.  
    115.     rigidbody.velocity.z = horizontalMovement.y;
    116.  
    117.    
    118.  
    119.     if (grounded){
    120.  
    121.         rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelerationVolx, walkDeacceleration);
    122.  
    123.         rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelerationVolz, walkDeacceleration);}
    124.  
    125.    
    126.  
    127.     transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
    128.  
    129.    
    130.  
    131.     if (grounded)
    132.  
    133.         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
    134.  
    135.     else
    136.  
    137.         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRacio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRacio * Time.deltaTime);
    138.  
    139.            
    140.  
    141.     if (Input.GetButtonDown("Jump")  grounded)
    142.  
    143.         rigidbody.AddForce(0,jumpVelocity,0);
    144.  
    145. }
    146.  
    147.  
    148.  
    149. function OnCollisionStay (collision : Collision)
    150.  
    151. {
    152.  
    153.     for (var contact : ContactPoint in collision.contacts)
    154.  
    155.     {
    156.  
    157.         if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
    158.  
    159.             grounded = true;
    160.  
    161.     }
    162.  
    163. }
    164.  
    165.  
    166.  
    167. function OnCollisionExit ()
    168.  
    169. {
    170.  
    171.     grounded = false;
    172.  
    173. }
    GunScript
    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.  
    104.     cameraObject.GetComponent(MouseLookScript).currentTargetCameraAngle = zoomAngle;
    105.    
    106.     if (Input.GetButton("Fire2")){
    107.         cameraObject.GetComponent(MouseLookScript).currentAimRacio = aimRacio;
    108.        
    109.        
    110.         racioHipHold = Mathf.SmoothDamp(racioHipHold, 0, racioHipHoldV, hipToAimSpeed);}
    111.     if (Input.GetButton("Fire2") == false){
    112.         cameraObject.GetComponent(MouseLookScript).currentAimRacio = 1;
    113.        
    114.        
    115.         racioHipHold = Mathf.SmoothDamp(racioHipHold, 1, racioHipHoldV, hipToAimSpeed);}
    116.    
    117.     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));
    118.    
    119.     targetXRotation = Mathf.SmoothDamp( targetXRotation, cameraObject.GetComponent(MouseLookScript).xRotation, targetXRotationV, rotateSpeed);
    120.     targetYRotation = Mathf.SmoothDamp( targetYRotation, cameraObject.GetComponent(MouseLookScript).yRotation, targetYRotationV, rotateSpeed);
    121.    
    122.     transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0);
    123. }
    124. if (!beingHeld)
    125. {
    126.     rigidbody.useGravity = true;
    127.     outsideBox.GetComponent(Collider).enabled = true;
    128.    
    129.     countToThrow -= 1;
    130.     if (countToThrow == 0)
    131.         rigidbody.AddRelativeForce(0, playerMovementScript.throwGunUpForce, playerMovementScript.throwGunForwardForce);
    132.    
    133.     if (Vector3.Distance(transform.position, playerTransform.position) < playerMovementScript.distToPickUpGun  Input.GetButtonDown("Use Key")  playerMovementScript.waitFrameForSwitchGuns <= 0)
    134.     {
    135.         playerMovementScript.currentGun.GetComponent(GunScript).beingHeld = false;
    136.         playerMovementScript.currentGun.GetComponent(GunScript).countToThrow = 2;
    137.         playerMovementScript.currentGun = gameObject;
    138.         beingHeld = true;
    139.         targetYRotation = cameraObject.GetComponent(MouseLookScript).yRotation - 180;
    140.         playerMovementScript.waitFrameForSwitchGuns = 2;
    141.     }
    142. }
    143. }
    Any help would be really nice :D:D:D
     
    Last edited: Mar 6, 2012
  2. G-man

    G-man

    Joined:
    Sep 1, 2011
    Posts:
    96
    go to his youtube channel and watch one of his latest videos. he started a new thread on this forum for those who are following his tutorials who get stuck to find help. Check it out and i bet you'll find an answer.
     
  3. DrSega

    DrSega

    Joined:
    Mar 6, 2012
    Posts:
    157
    yes he told me to make this thread so he could see my scripts but i 4got to label :/
     
  4. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    lol that's exactly what he did :) the way it's supposed to work is that everyone will post links to their thread in my thread so everyone can find each other's threads.
     
  5. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    Does the screen start shaking when you first start the game or only once you switch a gun? Does it shake when you are standing still, walking, jumping, switching back to the last gun, ect.?
     
  6. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    And the outsideBox is assigned in the inspector and is the only collider attached to the gun (or at least the only collider that isn't set to isTrigger)?
     
  7. DrSega

    DrSega

    Joined:
    Mar 6, 2012
    Posts:
    157
    The screen doesnt shake when i start the game and it doesnt matter which gun im holding it only shakes when im walking and when i walk a bit then jump, then it will shake
     
  8. DrSega

    DrSega

    Joined:
    Mar 6, 2012
    Posts:
    157
    yes the outsideBox is a child object of the gun and is the only collider
     
  9. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    weird. is there a rigidbody attached to your camera? and is the position and rotation frozen on the rigidbody of the capsule? all three x,y, and z?
     
  10. DrSega

    DrSega

    Joined:
    Mar 6, 2012
    Posts:
    157
    yes rigidbody was on but the position wasnt frozen, not sure y it was working before then.
    But thank you so much :D
     
  11. itsgeorge707

    itsgeorge707

    Joined:
    Mar 18, 2015
    Posts:
    1
    Can somebody tell me why my capsule doesnt
     
  12. Jcraft

    Jcraft

    Joined:
    May 10, 2015
    Posts:
    1
    Assets/scripts/Gun.cs(1,23): error CS8025: Parsing error
    Assets/scripts/Mouselook.cs(1,24): error CS8025: Parsing error
    Assets/scripts/PlayerMove.cs(1,16): error CS8025: Parsing error

    I have this Errors , can you help my ? Please