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.
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.
As noted above, please review the first post in the forum so your code is formatted readably, like so: Code (csharp): 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.
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?
Code (CSharp): //make speed for climbing and slipping idepentent, just in case var climbingSpeed = 3.0; var slippingSpeed = 3.0; var horizontalSpeed = 3.0; //a nice little touch, disable for more control var slipsDownABit = true; var slipsUpABit = false; //enable to be able to move horizontally var canMoveHorizontally = false; //enable to apply force on the carrier (overrides the above) var canShakeCarrier = false; //these will be used for switching between climbing and sliding @System.NonSerialized var climbingSwitch = 0; @System.NonSerialized var slippingSwitch = 0; //this will keep track of how many segments we are climbing at the moment var segmentHashtable: Hashtable; //the next lower segment the player could hang onto @System.NonSerialized var previousParent: GameObject; //the horizontal offset for climbing the target (0 is recommended), will be set by object to climb private var xOffset = 0.0; @System.NonSerialized var directionToFace = 1; function Awake(){ segmentHashtable = new Hashtable(); } function Update () { //first snap to the thing we climb transform.localPosition.x = xOffset; //cache raw and smoothed axis values var rawVerticalAxis = Input.GetAxisRaw("Vertical"); var smoothVerticalAxis = Input.GetAxis("Vertical"); var rawHorizontalAxis = Input.GetAxisRaw("Horizontal"); var smoothHorizontalAxis = Input.GetAxis("Horizontal"); //set the switches if (smoothVerticalAxis > 0){ //climbing up climbingSwitch = 1; slippingSwitch = 0; } else if (smoothVerticalAxis < 0){ //sliding down climbingSwitch = 0; slippingSwitch = 1; } else{ //hanging still climbingSwitch = 0; slippingSwitch = 0; } //now move, but only if a button is pressed OR shortly after releasing down (or up) to simulate some minor sliding if (rawVerticalAxis != 0 || (smoothVerticalAxis < 0 slipsDownABit) || (smoothVerticalAxis > 0 slipsUpABit)) transform.Translate(Vector3.up * Mathf.Pow(climbingSpeed *smoothVerticalAxis, climbingSwitch) * Mathf.Pow(slippingSpeed * smoothVerticalAxis, slippingSwitch) * Time.deltaTime); //horizontal movement if(smoothHorizontalAxis != 0 canMoveHorizontally) transform.Translate(Vector3.right * horizontalSpeed * smoothHorizontalAxis * Time.deltaTime); //shake it! if(canShakeCarrier) transform.parent.gameObject.rigidbody.AddRelativeForce(Vector3.right * smoothHorizontalAxis * 10); if(Input.GetButtonDown("Jump")) transform.parent.gameObject.GetComponent(RopeSegmentBehaviour).JumpOff(rawHorizontalAxis, gameObject); if(rawHorizontalAxis != 0) directionToFace = rawHorizontalAxis; } function LateUpdate(){ //set the rotation to fit the parent, if parented // if(transform.parent) // transform.rotation = transform.parent.transform.rotation; // Debug.Log (GetSegments()); /* if(directionToFace == 1){ transform.localEulerAngles.y = 0; } else{ transform.localEulerAngles.y = 180; } */ } function RegisterSegment(segment: GameObject){ segmentHashtable.Add(segment.name, segment); } function UnregisterSegment(segment: GameObject){ segmentHashtable.Remove(segment.name); } function SetXOffest(offset: float){ xOffset = offset; } function SetUpController(newClimbingSpeed: float, newSlippingSpeed: float, newHorizontalSpeed: float, slipUp: boolean, slipDown: boolean, moveHorizontally: boolean, shake: boolean){ //if a passed value is 0, then ignore it if (newClimbingSpeed != 0.0) climbingSpeed = newClimbingSpeed; if ( newSlippingSpeed != 0.0) slippingSpeed = newSlippingSpeed; if ( newHorizontalSpeed != 0.0) horizontalSpeed = newHorizontalSpeed; slipsDownABit = slipUp; slipsUpABit = slipDown; canMoveHorizontally = moveHorizontally; canShakeCarrier = shake; //override canMoveHorizontally if needed if(canShakeCarrier) canMoveHorizontally = false; }
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
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.