Search Unity

Sliding script prevents player from collecting items?(Js)

Discussion in 'Scripting' started by Warrior1424, Oct 29, 2014.

  1. Warrior1424

    Warrior1424

    Joined:
    Sep 30, 2010
    Posts:
    984
    My game is a 3D platformer.
    To make it so that the player could ascend stairs and small ledges, and not cliffsides, I made a separate script to handle sliding.
    For some reason, when I use this script, the player cannot pick up items (items are collected when the player enters their trigger).
    If the code is enabled, the player cannot pickup items (I don't even get errors).
    If the code is disabled, they can.

    The code on the item pickup merely checks for the tag of the objects that enter its trigger, and the tag never changes on the player, so by elimination this sliding script has to be the problem.

    Here is the code:
    Code (JavaScript):
    1. #pragma strict
    2. //Put this script on the player
    3. //This will cause the player to lose control of their character and slide when the surface directly below them is too steep
    4. var maxAngle = 45.0;
    5. private var defaultAngle = 0.0;
    6. private var controller : CharacterController;
    7. private var motor : CharacterMotor;
    8.  
    9. function Start () {
    10. controller = GetComponent(CharacterController);
    11. motor = GetComponent(CharacterMotor);
    12. defaultAngle = controller.slopeLimit;
    13. }
    14.  
    15. function Update () {
    16. var hit : RaycastHit;
    17.  
    18. if (Physics.Raycast(transform.position + Vector3(0,0.2,0), -Vector3.up, hit) && controller.isGrounded == true)
    19.     {
    20.     if (Vector3.Angle(Vector3.up, hit.normal) > maxAngle)
    21.         {
    22.         controller.slopeLimit = maxAngle;
    23.         motor.canControl = false;
    24.         }
    25.     else
    26.         {
    27.         controller.slopeLimit = defaultAngle;
    28.         motor.canControl = true;
    29.         }
    30.     }
    31. else
    32.     {
    33.     controller.slopeLimit = defaultAngle;
    34.     motor.canControl = true;
    35.     }
    36.    
    37. }
     
  2. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    Comment out lines until you find out what's causing it. My money's on the motor.canControl lines.
     
  3. Warrior1424

    Warrior1424

    Joined:
    Sep 30, 2010
    Posts:
    984
    So I found the line that seems to be causing the problem (commenting it out makes everything work correctly.

    Line 27:
    Code (JavaScript):
    1. controller.slopeLimit = defaultAngle;
    Although I still have no idea why that line specifically would cause this to happen.
    Line 33 is identical, yet it causes no problems.
     
  4. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    Maybe the other one never gets called? What value is *actually* being assigned to it at each of those points?