- Home /
FPSController doesn't climb ladders
I've been trying to make the FPSController (the one that comes within unity) capable of climbing ladders. I've used this script that I saw in the forums and attached it to the ladder:
#pragma strict
var playerObject : GameObject;
var canClimb = false;
var speed : float = 1;
function Start ()
{
playerObject = gameObject.Find("FPSController");
}
function OnTriggerEnter (collider : Collider)
{
if(collider.gameObject == playerObject)
{
canClimb = true;
playerObject.rigidbody.useGravity = false;
}
}
function OnTriggerExit (collider : Collider)
{
if(collider.gameObject == playerObject)
{
canClimb = false;
playerObject.rigidbody.useGravity = true;
}
}
function Update ()
{
if(canClimb == true)
{
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 variable that makes sure that the player is in range works and in fact the "useGravity" variable of the rigidbody unchecks but the character is still not able to climb the ladder. I'm new to Unity I can't figure out what's the problem.
I am just wondering why you use the Z key to go up and not W (forward), but you are using the S (backwards key) for down...
Apparently I made a typo there. I've changed it to W but still doesn't work. I guess it's something related to the FPSController?
Your entire Update loop within ladder's script is unnecessary. Simply disabling the gravity for the player should be enough, the controls in Player's script should take care of the rest (looking up and pressing W should make you go up).
Alternatively, if you really want W/S to move you up and down regardless of look direction, implement that within Player's control script.
Also, don't use transform.Translate, try setting rigidbody.velocity first.
Your answer

Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Ladder script not working? 0 Answers
How to make a simple ladder? 3 Answers
Player stucks on ladder (with picture) 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers