climb a ladder. How to do?

i like to climb a ladder with my character. What is wrong with my code?

function climb (){
    if(Input.GetButton("k")){
        transform.position += Vector3(0,0,80);
    }
}

Maybe the right code is this

function climb ()
{ 
     if(Input.GetButton("k"))
     { 
          transform.position += Vector3(0,80,0); 
     } 
}

you must write your code like this one

function Update ()
{
      if(Input.GetButton("climb"))
      {
           Climb();
      }
}

function Climb ()
{
      transform.position += Vector3(0,0,80);
}

then go into Unity, click on Edit - Project Settings - Input, then in Size add one button (Ex: if in Size there is 15, you must put 16) and then at the end of the list click on the last button, in “name” put climb and in “Positive Button” put k, then try if the code is valid.

Thanks
It’s good but the same. I want to climb en not jump above.

Stijn

wait few minutes and i try to make a script on my project

re : function Update () { if(Input.GetButton(“k”)){ transform.position += Vector3(0,0.5,0); } } i am climing but it looks more jumping very high. How can i make a smooth climb on my ladder

instead of accessing position directly, use transform.Translate(Vector3(0,0.5,0) * Time.deltaTime);

thanks

Stijn

Thanks TheBombix

Stijn

the script is here

private var speed = 6.0;
var aimSpeed = 2.0;
var sprintSpeed = 10.0;
private var canSprint : boolean = true;

var sprintJumpSpeed = 8.0;
var normSpeed = 6.0;
var crouchSpeed = 6.0;
var crouchDeltaHeight = 1.0;
var jumpSpeed = 8.0;
var normJumpSpeed = 8.0;
var gravity = 20.0;
private var mainCamera : GameObject;
private var weaponCamera : GameObject;
static var crouching : boolean = false;
private var stopCrouching : boolean = false;
private var standardCamHeight : float;
private var crouchingCamHeight : float;

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

static var walking : boolean = false;

function Start() {
speed = normSpeed;
mainCamera = gameObject.FindWithTag(“MainCamera”);
weaponCamera = gameObject.FindWithTag(“WeaponCamera”);
crouching = false;
standardCamHeight = mainCamera.transform.localPosition.y;
crouchingCamHeight = standardCamHeight - crouchDeltaHeight;
}

function Update() {
if(mainCamera.transform.localPosition.y > standardCamHeight){
mainCamera.transform.localPosition.y = standardCamHeight;
} else if(mainCamera.transform.localPosition.y < crouchingCamHeight){
mainCamera.transform.localPosition.y = crouchingCamHeight;
}
if(weaponCamera.transform.localPosition.y > standardCamHeight){
weaponCamera.transform.localPosition.y = standardCamHeight;
} else if(weaponCamera.transform.localPosition.y < crouchingCamHeight){
weaponCamera.transform.localPosition.y = crouchingCamHeight;
}

if (Input.GetButtonDown ("Crouch")){
   if(crouching){
     stopCrouching = true;
     normalSpeed();
     return;
   }

   if(!crouching)
     crouch();
}
if(crouching){
   if(mainCamera.transform.localPosition.y > crouchingCamHeight){
     if(mainCamera.transform.localPosition.y - (crouchDeltaHeight*Time.deltaTime*8) < crouchingCamHeight){
      mainCamera.transform.localPosition.y = crouchingCamHeight;
     } else {
      mainCamera.transform.localPosition.y -= crouchDeltaHeight*Time.deltaTime*8;
     }
   }
   if(weaponCamera.transform.localPosition.y > crouchingCamHeight){
     if(weaponCamera.transform.localPosition.y - (crouchDeltaHeight*Time.deltaTime*8) < crouchingCamHeight){
      weaponCamera.transform.localPosition.y = crouchingCamHeight;
     } else {
      weaponCamera.transform.localPosition.y -= crouchDeltaHeight*Time.deltaTime*8;
     }
   }
} else {
   if(mainCamera.transform.localPosition.y < standardCamHeight){
     if(mainCamera.transform.localPosition.y + (crouchDeltaHeight*Time.deltaTime*8) > standardCamHeight){
      mainCamera.transform.localPosition.y = standardCamHeight;
     } else {
      mainCamera.transform.localPosition.y += crouchDeltaHeight*Time.deltaTime*8;
     }
   }
   if(weaponCamera.transform.localPosition.y < standardCamHeight){
     if(weaponCamera.transform.localPosition.y - (crouchDeltaHeight*Time.deltaTime*8) > standardCamHeight){
      weaponCamera.transform.localPosition.y = standardCamHeight;
     } else {
      weaponCamera.transform.localPosition.y += standardCamHeight*Time.deltaTime*8;
     }
   }

}

}
function FixedUpdate() {

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;
     if(crouching){
      stopCrouching = true;
      normalSpeed();
     }
   }

}

// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;

// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);   
grounded = (flags & CollisionFlags.CollidedBelow) != 0;

if((Mathf.Abs(moveDirection.x) > 0) && grounded || (Mathf.Abs(moveDirection.z) > 0 && grounded)){
   if(!walking){
     walking = true;
     BroadcastMessage("walking", SendMessageOptions.DontRequireReceiver);
   }
} else if(walking){
   walking = false;
   BroadcastMessage("stopWalking", SendMessageOptions.DontRequireReceiver);
}

}

@script RequireComponent(CharacterController)

function aiming() {
speed = aimSpeed;
}

function crouch() {
speed = crouchSpeed;
//mainCamera.transform.position.y -= crouchDeltaHeight;
//weaponCamera.transform.position.y -= crouchDeltaHeight;
//BroadcastMessage (“crouching”, SendMessageOptions.DontRequireReceiver);
this.GetComponent(CharacterController).height -= crouchDeltaHeight;
this.GetComponent(CharacterController).center -= Vector3(0,crouchDeltaHeight/2, 0);
crouching = true;
}

function normalSpeed () {
if(stopCrouching){
crouching = false;
//mainCamera.transform.position.y += crouchDeltaHeight;
//weaponCamera.transform.position.y += crouchDeltaHeight;
this.GetComponent(“CharacterController”).height += crouchDeltaHeight;
this.GetComponent(“CharacterController”).center += Vector3(0,crouchDeltaHeight/2, 0);
BroadcastMessage(“stopWalking”, SendMessageOptions.DontRequireReceiver);

   stopCrouching = false;       
} else if(crouching){
   speed = crouchSpeed;
   return;
}
   speed = normSpeed;
   jumpSpeed = normJumpSpeed;

}
function sprinting () {
if(crouching){
crouching = false;
//mainCamera.transform.position.y += crouchDeltaHeight;
//weaponCamera.transform.position.y += crouchDeltaHeight;
this.GetComponent(CharacterController).height += crouchDeltaHeight;
this.GetComponent(CharacterController).center += Vector3(0,crouchDeltaHeight/2, 0);

}
if(canSprint){
   speed = sprintSpeed;
   jumpSpeed = sprintJumpSpeed;
}

}

Try this:

function Update ()
{
      if(Input.GetButton(KeyCode.K))
      {
           Climb();
      }
}
 
function Climb ()
{
      transform.position += Vector3(0,0,80);
}