• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by Jamey · Jan 09, 2010 at 09:45 PM · gameobject

How do I create a climbable ladder?

I've been looking all over the place and on the internet, but I've not the faintest clue as how you can make a ladder than can be climbed by a standard FPS Controller. I tried looking at how VALVe does it for Half Life 2, but it's evil to try and emulate in Unity. I've no real skills when it comes to this part, so can anyone explain how to make a ladder that works in unity? Or does anyone have a prefab (That I can pay for in return)?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
10
Best Answer

Answer by Jaap Kreijkamp · Jan 10, 2010 at 01:27 AM

To make a ladder you need to tie it with your character controller script, but to give an example implementation with a controller script derived from the character animation demo. The ladder needs to have a collider set to trigger that's a bit larger than the ladder itself so player will trigger it when in close proximity to it. Attach the ladder script to the ladder and set bottom and top properties to the lowest and highest pos the player can reach, if you have difficulty to find the right values, uncomment the debug statement in the Move function.

The ladder script:

var bottom : float; var top : float;

private var ctrl : CharacterController;

// function called by controller script to tell what it's character controller is function WhoAmI(c : CharacterController) { ctrl = c; }

// function called by controller script when controller is attached to ladder // delta is the forward/backward movement function Move(delta : float) { // we calc vertical position using current vertical position + delta movement corrected for time interval var vertPos : float = ctrl.transform.position.y + delta * Time.deltaTime; ctrl.transform.rotation = transform.rotation; // Debug.Log("Move: " + vertPos); // debug usefull to determine top and bottom values if ((vertPos > top) || (vertPos < bottom)) { // we're at top or bottom and moving away from ladder, use walk animation ctrl.SendMessage("SetCurrentSpeed", delta, SendMessageOptions.DontRequireReceiver);

     // set climb speed 0 to stop climb animation
     ctrl.SendMessage("SetCurrentClimbSpeed", 0, SendMessageOptions.DontRequireReceiver);

     // move forwards/backwards, orientation determined by lader orientation
     ctrl.Move(transform.rotation * new Vector3(0.0, 0.0, delta) * Time.deltaTime);        
 }
 else {
     // no forward movement as we're climbing
     ctrl.SendMessage("SetCurrentSpeed", 0, SendMessageOptions.DontRequireReceiver);

     // set climb animation
     ctrl.SendMessage("SetCurrentClimbSpeed", delta, SendMessageOptions.DontRequireReceiver);

     var pos : Vector3 = transform.position; // get position of ladder, use it for x and z pos of the character
     pos.y = vertPos; // the y pos (vertical) is determined by calculated vertical position
     ctrl.transform.position = pos;
 }

}

function OnTriggerEnter(other : Collider) { // tell controller we take over control other.gameObject.SendMessage("TakeOver", this.gameObject, SendMessageOptions.DontRequireReceiver); }

function OnTriggerExit(other : Collider) { // tell controller we give back control other.gameObject.SendMessage("Release", this.gameObject, SendMessageOptions.DontRequireReceiver); }

The adjusted Player Controller script:

var speed = 3.0; var rotatationSpeed = 200.0; private var curSpeed = 0.0;

private var vertSpeed = -10.0;

private var takeOverCtrl : GameObject;

// called when the ladder (or whatever wants to take control takes over the player) function TakeOver(t : GameObject) { takeOverCtrl = t; }

// we left the ladder (or whatever) function Release() { takeOverCtrl = null; }

function Update () { var controller : CharacterController = GetComponent (CharacterController);

 // Rotate around y-axis
 var newRotation = Input.GetAxis("Horizontal") * rotatationSpeed;
 transform.Rotate(0, newRotation * Time.deltaTime, 0);

 // Calculate speed
 var newSpeed = Input.GetAxis("Vertical") * speed;
 if (Input.GetKey("left shift"))
     newSpeed *= 1.5;

 // Move the controller
 var forward = transform.TransformDirection(Vector3.forward);

 if ((controller.isGrounded) &amp;&amp; (Input.GetKey("space"))) vertSpeed = 5.0;

 if (takeOverCtrl) {
     // we have some object (ladder) taken over control 
     SendMessage("SetCurrentSpeed", 0, SendMessageOptions.DontRequireReceiver);
     // a bit of a hack to send two messages as the receiver can have only one parameter
     // a better solution would to have the two parameters encapsulated in a object/struct
     takeOverCtrl.SendMessage("WhoAmI", controller, SendMessageOptions.DontRequireReceiver);
     takeOverCtrl.SendMessage("Move", newSpeed, SendMessageOptions.DontRequireReceiver);
 }
 else {
     // the normal movement handling
     var delta = Time.deltaTime;

     controller.Move((forward * newSpeed + Vector3(0, vertSpeed, 0)) * delta);
     if (vertSpeed &gt; -10.0) vertSpeed -= delta * 10;

     // Update the speed in the Animation script
     SendMessage("SetCurrentSpeed", newSpeed, SendMessageOptions.DontRequireReceiver);
     SendMessage("SetCurrentLean", Input.GetAxis("Horizontal"), SendMessageOptions.DontRequireReceiver);
 }

}

@script RequireComponent (CharacterController)

The ladder and player controller script also assumes there are scripts for handling animations, the ComplexWalkBlend.js from the demo project already implement the SetCurrentSpeed and SetCurrentLean functions but you must add an function SetCurrentClimbSpeed to have the char have a working climb ladder animation.

The ladder is not perfect, when at the top you need to climb down backwards, when walking forwards to the ladder you will move off immediately (as you holding controls forwards which is climbing up).

If you need more help, let me know.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Get first gameobject in a list and cycle through on keypress 1 Answer

Change target after destroying it. 2 Answers

How to check a sequence of gameobjects and tell if it was correct or not? 2 Answers

How can I make a GameObject invisible while maintaining its interractivity? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges