How to make a simple ladder?

This is the script i made without really thinking. I’ve never actually made a script on my own without anybodys help but i want to be able to. So this is the script i made randomly to show the basic idea of what i want to happen
var canClimb : false;

function Update () {
 if (canClimb = true) && (Input.GetButtonDown(KeyCode.W)) {
 	transform.Translate (Vector3(0,0,1) * Time.deltaTime*speed);
 }
 if (canClimb = true) && (Input.GetButtonDown(KeyCode.S)) {
 	transform.Translate (Vector3(0,0,-1) * Time.deltaTime*speed);
}
function OnTriggerEnter () {
	canClimb = true;
}
function OnTriggerExit () {
	canClimb = false;
}

Okay, so i want it so that when a player enters a collider the letter w makes you go up and the letter s makes you go down. What i need to do is make it actually work. Like i know this would never work because unity doesnt know what to move up, but i dont know how to tell it what to move up. I know you can use tags but i dont know how to do that. So tell me if this script is crap and useless, or help me fix it mabye? I just want to have a ladder basically. Its first person so no need for animation.

I’m going to assume that the script is attached to the ladder, if so, this is what you use

var playerObject : GameObject;
var canClimb = false;
var speed : float = 1;

function Start () {
	playerObject = gameObject.Find("Player");
}

function OnCollisionEnter (coll : Collision){
	if(coll.gameObject == playerObject){
		canClimb = true;
	}
}

function OnCollisionExit (coll2 : Collision){
	if(coll2.gameObject == playerObject){
		canClimb = false;
	}
}
function Update () {
	if(canClimb){
		if(Input.GetKey(KeyCode.W)){
			playerObject.transform.Translate (Vector3(0,1,0) * Time.deltaTime*speed);
		}
		if(Input.GetKey(KeyCode.S)){
			playerObject.transform.Translate (Vector3(0,-1,0) * Time.deltaTime*speed);
		}
	}
}

the above script doesn’t find an object with the tag “Player”, but rather finds the game object called “Player”

@awesomeface’s answer in C#, and doesn’t require gameobject’s name to be playerObject. Just need tag to be “Player”, and it should be that anyways :stuck_out_tongue:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ladder : MonoBehaviour {

	GameObject playerOBJ;
	bool canClimb = false;
	float speed = 1;

	void OnCollisionEnter(Collision coll)
	{
		if (coll.gameObject.tag == "Player")
		{
			canClimb = true;
			playerOBJ = coll.gameObject;
		}
	}

	void OnCollisionExit(Collision coll2)
	{
		if (coll2.gameObject.tag == "Player")
		{
			canClimb = false;
			playerOBJ = null;
		}
	}
	void Update()
	{
		if (canClimb)
		{
			if (Input.GetKey(KeyCode.W))
			{
				playerOBJ.transform.Translate(new Vector3(0, 1, 0) * Time.deltaTime * speed);
			}
			if (Input.GetKey(KeyCode.S))
			{
				playerOBJ.transform.Translate(new Vector3(0, -1, 0) * Time.deltaTime * speed);
			}
		}
	}
}

Hi, try using animations with a trigger so something like:

function OnTriggerEnter() {
	animation.Play();
}

Hope this helps!