Search Unity

How to climb in Unity 2d

Discussion in 'Scripting' started by kristenmg, Aug 2, 2019.

  1. kristenmg

    kristenmg

    Joined:
    Jul 23, 2019
    Posts:
    10
    hello,

    I have a project due next Thursday and I only have one thing left to figure out. I have ZERO knowledge of coding, but I need to make a script that allows me to climb. I'm currently working on a 2d space and could use all the help anyone is willing to offer.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    I'm sorry but this forum isn't so much for "please write my code" as it is for you to give it the old college try and then post "I have tried these specific code steps and I don't know why it's not working."

    Be sure to post your work-in-progress code with formatting (see first post in this forum), what you expect the code to do, and what is is actually doing.

    To get you started with Unity scripting, everything you need is right here: Learn

    There's also plenty of Youtube tutorials and Google is pretty good at returning what you want if it exists.

    Hop right in! The water's fine.
     
  3. kristenmg

    kristenmg

    Joined:
    Jul 23, 2019
    Posts:
    10
    this is what I have, but its really not working.
     
  4. kristenmg

    kristenmg

    Joined:
    Jul 23, 2019
    Posts:
    10
    Also, I'm really not asking anyone to write me a code. I'm just looking for some good ole direction
     
  5. kristenmg

    kristenmg

    Joined:
    Jul 23, 2019
    Posts:
    10
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    As noted above, please review the first post in the forum so your code is formatted readably, like so:

    Code (csharp):
    1. bool MyCodeIsReadable = true;
    Don't forget this part too. Nobody here has your project so staring at a huge script without any indication of what it does or doesn't do isn't going to be helpful.
     
  7. Please use code tags. Your code is not readable.
    https://forum.unity.com/threads/using-code-tags-properly.143875/

    Also what version of Unity are you working with?
    Why do you use UnityScript (JS)? It's been deprecated a long time ago. You really want to use C# with Unity. UnityScript/JS has been removed already, there is no future (not even present) there.

    Can you please describe what is "not working"? What do you expect from the code and what does it do instead? Do you have any errors in the console?
     
  8. kristenmg

    kristenmg

    Joined:
    Jul 23, 2019
    Posts:
    10
    Code (CSharp):
    1. //make speed for climbing and slipping idepentent, just in case
    2. var climbingSpeed = 3.0;
    3. var slippingSpeed = 3.0;
    4. var horizontalSpeed = 3.0;
    5. //a nice little touch, disable for more control
    6. var slipsDownABit = true;
    7. var slipsUpABit = false;
    8. //enable to be able to move horizontally
    9. var canMoveHorizontally = false;
    10. //enable to apply force on the carrier (overrides the above)
    11. var canShakeCarrier = false;
    12. //these will be used for switching between climbing and sliding
    13. @System.NonSerialized
    14.     var climbingSwitch = 0;
    15. @System.NonSerialized
    16.     var slippingSwitch = 0;
    17.  
    18. //this will keep track of how many segments we are climbing at the moment
    19. var segmentHashtable: Hashtable;
    20. //the next lower segment the player could hang onto
    21. @System.NonSerialized
    22.     var previousParent: GameObject;
    23. //the horizontal offset for climbing the target (0 is recommended), will be set by object to climb
    24. private var xOffset = 0.0;
    25. @System.NonSerialized
    26.     var directionToFace = 1;
    27. function Awake(){
    28.     segmentHashtable = new Hashtable();
    29. }
    30. function Update () {
    31.     //first snap to the thing we climb
    32.     transform.localPosition.x = xOffset;
    33.  
    34.     //cache raw and smoothed axis values
    35.     var rawVerticalAxis = Input.GetAxisRaw("Vertical");
    36.     var smoothVerticalAxis = Input.GetAxis("Vertical");
    37.     var rawHorizontalAxis = Input.GetAxisRaw("Horizontal");
    38.     var smoothHorizontalAxis = Input.GetAxis("Horizontal");
    39.  
    40.     //set the switches
    41.     if (smoothVerticalAxis > 0){
    42.         //climbing up
    43.         climbingSwitch = 1;
    44.         slippingSwitch = 0;
    45.     } else if (smoothVerticalAxis < 0){
    46.         //sliding down
    47.         climbingSwitch = 0;
    48.         slippingSwitch = 1;
    49.     } else{
    50.         //hanging still
    51.         climbingSwitch = 0;
    52.         slippingSwitch = 0;
    53.     }
    54.  
    55.     //now move, but only if a button is pressed OR shortly after releasing down (or up) to simulate some minor sliding
    56.     if (rawVerticalAxis != 0 || (smoothVerticalAxis < 0  slipsDownABit) || (smoothVerticalAxis > 0  slipsUpABit))
    57.         transform.Translate(Vector3.up * Mathf.Pow(climbingSpeed *smoothVerticalAxis, climbingSwitch) *  Mathf.Pow(slippingSpeed * smoothVerticalAxis, slippingSwitch) * Time.deltaTime);
    58.     //horizontal movement
    59.     if(smoothHorizontalAxis != 0  canMoveHorizontally)
    60.         transform.Translate(Vector3.right * horizontalSpeed * smoothHorizontalAxis * Time.deltaTime);
    61.      
    62.     //shake it!
    63.     if(canShakeCarrier)
    64.         transform.parent.gameObject.rigidbody.AddRelativeForce(Vector3.right * smoothHorizontalAxis * 10);
    65.      
    66.     if(Input.GetButtonDown("Jump"))
    67.         transform.parent.gameObject.GetComponent(RopeSegmentBehaviour).JumpOff(rawHorizontalAxis, gameObject);
    68.     if(rawHorizontalAxis != 0)
    69.         directionToFace = rawHorizontalAxis;
    70. }
    71. function LateUpdate(){
    72.     //set the rotation to fit the parent, if parented
    73. //  if(transform.parent)
    74. //      transform.rotation = transform.parent.transform.rotation;
    75. //  Debug.Log (GetSegments());
    76.  
    77.     /*
    78.     if(directionToFace == 1){
    79.         transform.localEulerAngles.y = 0;
    80.     } else{
    81.         transform.localEulerAngles.y = 180;
    82.     }
    83.     */
    84. }
    85. function RegisterSegment(segment: GameObject){
    86.     segmentHashtable.Add(segment.name, segment);
    87. }
    88. function UnregisterSegment(segment: GameObject){
    89.     segmentHashtable.Remove(segment.name);
    90. }
    91. function SetXOffest(offset: float){
    92.     xOffset = offset;
    93. }
    94. function SetUpController(newClimbingSpeed: float, newSlippingSpeed: float, newHorizontalSpeed: float, slipUp: boolean, slipDown: boolean, moveHorizontally: boolean, shake: boolean){
    95.     //if a passed value is 0, then ignore it
    96.     if (newClimbingSpeed != 0.0)
    97.         climbingSpeed = newClimbingSpeed;
    98.     if ( newSlippingSpeed != 0.0)
    99.         slippingSpeed = newSlippingSpeed;
    100.     if ( newHorizontalSpeed != 0.0)
    101.         horizontalSpeed = newHorizontalSpeed;
    102.     slipsDownABit = slipUp;
    103.     slipsUpABit = slipDown;
    104.     canMoveHorizontally = moveHorizontally;
    105.     canShakeCarrier = shake;
    106.  
    107.     //override canMoveHorizontally if needed
    108.     if(canShakeCarrier)
    109.         canMoveHorizontally = false;
    110. }
    111.  
     
  9. kristenmg

    kristenmg

    Joined:
    Jul 23, 2019
    Posts:
    10

    Thank you for taking the time to reply! You're so nice. And when I go to test play it, unity presents an error code and won't let me. So I don't really know what is wrong with it
     
  10. kristenmg

    kristenmg

    Joined:
    Jul 23, 2019
    Posts:
    10
    Also, its the newest version
     
  11. Unity does not have JS/UnityScript support since 2017 as far as I remember.
    You need to use C#. Or if you're really adventurous, you can try to convert this piece of code into C#, but since you're a beginner, I don't recommend it.
    It's better if you build up your knowledge in C# until you can write a replacement.

    And BTW, the error message tells you what is wrong with it.
     
    kristenmg likes this.
  12. kristenmg

    kristenmg

    Joined:
    Jul 23, 2019
    Posts:
    10
    OK, thanks for the help and for being so nice/ patient with me. I really apreciate it.