- Home /
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)?
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) && (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 > -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.
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
scaling plane gameobject to fit orthographic view iphone unity3d 0 Answers
enabling / disabling child objects 1 Answer
DontDestory gameobject doesn't work on button click after a new scene loads 2 Answers
Unity Detonator Problems 1 Answer