Ladder script

Hello, i have problem with Ladder script. No error, but i can’t climb up. I chatch on ladder but i can’t climb up :frowning: . Please how to do it?
Code for walking

var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;

private var moveDirection = Vector3.zero;
private var grounded : boolean = false;

// *** Added for UseLadder ***
private var mainCamera : GameObject = null;
private var controller : CharacterController = null;
private var onLadder = false;
var ladderHopSpeed = 6.0;

function Start () {
	mainCamera = GameObject.FindWithTag("MainCamera");
	controller = GetComponent(CharacterController);
}

function FixedUpdate() {
	
	// *** Added for UseLadder ***
	// If we are on the ladder, let the ladder code take over and handle FixedUpdate calls.
	if(onLadder) {
		gameObject.SendMessage("LadderFixedUpdate", null, SendMessageOptions.RequireReceiver);
		return;
	}
	// ***
	
	if (grounded) {
		// We are grounded, so recalculate movedirection directly from axes
		moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
		moveDirection = transform.TransformDirection(moveDirection);
		moveDirection *= speed;
		
		if (Input.GetButton ("Jump")) {
			moveDirection.y = jumpSpeed;
		}
	}

	// Apply gravity
	moveDirection.y -= gravity * Time.deltaTime;
	
	// Move the controller
	var flags = controller.Move(moveDirection * Time.deltaTime);
	grounded = (flags  CollisionFlags.CollidedBelow) != 0;
}

// *** Added for UseLadder ***
function OnLadder () {
	onLadder = true;
	moveDirection = Vector3.zero;
}

function OffLadder (ladderMovement) {
	onLadder = false;
	
	// perform off-ladder hop
	var hop : Vector3 = mainCamera.transform.forward;
	hop = transform.TransformDirection(hop);
	moveDirection = (ladderMovement.normalized + hop.normalized) * ladderHopSpeed;
}
// ***

@script RequireComponent(CharacterController)

UseLadder script

// The speed of the player up and down the ladder. Roughly equal to walking speed is a good starting point.
var climbSpeed = 6.0;
// In the range -1 to 1 where -1 == -90 deg, 1 = 90 deg, angle of view camera at which the user climbs down rather than up when moving with the forward key.
var climbDownThreshold = -0.4;

private var climbDirection : Vector3 = Vector3.zero;
private var lateralMove : Vector3 = Vector3.zero;
private var forwardMove : Vector3 = Vector3.zero;
private var ladderMovement : Vector3 = Vector3.zero;
private var currentLadder : Ladder = null;
private var latchedToLadder : boolean = false;
// DEBUG :: private var inMotionControl : boolean = false;
private var inLandingPad : boolean = false;
private var mainCamera : GameObject = null;
private var controller : CharacterController = null;
private var landingPads : ArrayList = null;
// DEBUG :: var landPadCount : int = 0;

function Start () {
	mainCamera = GameObject.FindWithTag("MainCamera");
	controller = GetComponent(CharacterController);
	landingPads = new ArrayList();
}

function OnTriggerEnter (other : Collider) {
	if(other.tag == "LadderLandingPad") {
		// add this landing pad to the list of landing pads if it's not already there
		if(!landingPads.Contains(other)){
			landingPads.Add(other);
		}
		
		// set the flag
		inLandingPad = true;
		// DEBUG :: landPadCount = landingPads.Count;
	}
	
	if(other.tag == "Ladder") {		
		LatchLadder(other.gameObject, other);
	}
}

function OnTriggerExit (other : Collider) {		
	if(other.tag == "LadderLandingPad") {
		// remove this landing pad from the list
		landingPads.Remove(other);
		
		// if the list is now empty, set the flag
		if(landingPads.Count == 0) inLandingPad = false;
		// DEBUG :: landPadCount = landingPads.Count;
	}

	if(other.tag == "Ladder") {
		UnlatchLadder();
	}
}

/**
 *	Connect the player to the ladder, and shunt FixedUpdate calls to the special ladder movment functions.
 */
function LatchLadder (latchedLadder : GameObject, collisionWaypoint : Collider) {
	// typecast the latchedLadder as a Ladder object from GameObject
	currentLadder = latchedLadder.GetComponent(Ladder);
	latchedToLadder = true;
	
	// get the climb direction
	climbDirection = currentLadder.ClimbDirection();
	
	// let the other scripts know we are on the ladder now
	gameObject.SendMessage("OnLadder", null, SendMessageOptions.RequireReceiver);
}

/**
 *	Shut off special ladder movement controls and return to normal movement operations.
 */
function UnlatchLadder () {
	latchedToLadder = false;
	currentLadder = null;
	
	// let the other scripts know we are off the ladder now
	gameObject.SendMessage("OffLadder", ladderMovement, SendMessageOptions.RequireReceiver);
	
	// DEBUG :: inMotionControl = false;
}

/**
 *	Convert the player's normal forward and backward motion into up and down motion on the ladder.
 */
function LadderFixedUpdate () {
	
	// If we jumpped, then revert back to the original behavior
	if (Input.GetButton("Jump")) {
		UnlatchLadder();
		gameObject.SendMessage("FixedUpdate", null, SendMessageOptions.RequireReceiver);
		return;
	}
	
	// DEBUG :: inMotionControl = true;
	
	// find the climbing direction
	verticalMove = climbDirection.normalized;
	
	// convert forward motion to vertical motion
	verticalMove *= Input.GetAxis("Vertical");
	verticalMove *= (mainCamera.transform.forward.y > climbDownThreshold) ? 1 : -1;
	
	// find lateral component of motion
	if (inLandingPad) {
		lateralMove = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
	} else {
		lateralMove = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
	}
	lateralMove = transform.TransformDirection(lateralMove);
		
	// move
	ladderMovement = verticalMove + lateralMove;
	var flags = controller.Move(ladderMovement * climbSpeed * Time.deltaTime);
}

@script RequireComponent(CharacterController)

Ladder script

var ladderBottom : GameObject;
var ladderTop : GameObject;

private var climbDirection : Vector3 = Vector3.zero;

function Start () {
	climbDirection = ladderTop.transform.position -  ladderBottom.transform.position;
}

function ClimbDirection () {
	return climbDirection;
}

Thanks.

Word, while on the ladder you may want to turn off gravity.

try something like this

function OnLadder () {
onLadder = true;
moveDirection.y += 0.2*Time.deltaTime;
}

PS, Peep my ladder vid link.

I added it to my script, but don’t work… Your Ladder system look cool, but i don’t have money for buy it…

In latest version of Unity my script work fine, but in newest version it don’t work :frowning: Anyone please help.

You may be getting the null Reference Exception from a broken prefab or unassigned element in the inspector.

Did you re name any scripts?

No, but tags yes…

Thanks, when i repaired tags it work fine.