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

Why is this script making the GameObject shake?

Discussion in 'Scripting' started by Treasureman, May 22, 2016.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have a script that works as a crouching script. Here's the code...
    Code (JavaScript):
    1. var LeftTexture :GameObject;
    2. var RightTexture :GameObject;
    3. var player : CharacterController;
    4. var standHeight : float = 2.5;
    5. var crouchHeight : float = 1.5;
    6. var smooth : float = 0;
    7. var cam : GameObject;
    8. var standCamera : float = 0;
    9. var crouchCamera : float = -1.5;
    10. var footsteps : AudioSource;
    11.  
    12. function Update () {
    13.     if (Input.GetButtonDown ("Crouch"))
    14.         if( LeftTexture.activeSelf){
    15.             RightTexture.SetActive (true);
    16.             LeftTexture.SetActive (false);
    17.         }
    18.         else if (RightTexture.activeSelf){
    19.             LeftTexture.SetActive (true);
    20.             RightTexture.SetActive (false);
    21.         }
    22.  
    23.     if (LeftTexture.activeSelf)
    24.     {
    25.         player.height = player.height = Mathf.Lerp(player.height,crouchHeight,Time.deltaTime*smooth);
    26.     }
    27.  
    28.     if (RightTexture.activeSelf)
    29.     {
    30.         player.height = player.height = Mathf.Lerp(player.height,standHeight,Time.deltaTime*smooth);
    31.     }
    32.  
    33.     if(player.height < 1.3 || player.height > 1.3 && player.height < 2.5)
    34.     {
    35.         footsteps.enabled = false;
    36.     }
    37.  
    38.     if(player.height == 1.3 || player.height == 2.5)
    39.     {
    40.         footsteps.enabled = true;
    41.     }
    42.  
    43.     if (LeftTexture.activeSelf)
    44.     {
    45.         cam.transform.position.y = cam.transform.position.y = Mathf.Lerp(cam.transform.position.y,crouchCamera,Time.deltaTime*smooth);
    46.     }
    47.  
    48.     if (RightTexture.activeSelf)
    49.     {
    50.         cam.transform.position.y = cam.transform.position.y = Mathf.Lerp(cam.transform.position.y,standCamera,Time.deltaTime*smooth);
    51.     }
    52. }
    53.  
    54.    
    But the part where I make the variable cam lerp y positions, it only makes the GameObject shake up and down. How do I fix this?
     
  2. Thomas-Mountainborn

    Thomas-Mountainborn

    Joined:
    Jun 11, 2015
    Posts:
    488
    You're not using linear interpolation correctly - a very common beginner's trap, mostly caused by the official documentation being wrong. Here is a very nice explanation on how you should use it.
     
  3. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,727
    Your lerp speed is always 0, so I don't know how the camera could even move. Or is it being set somewhere else in the code?
    Set the lerp speed to 2 for example.

    It looks like you're are trying to set it up to use SmoothDamp:
    http://docs.unity3d.com/ScriptReference/Mathf.SmoothDamp.html