So I have this code here, on its way to becoming a working ladder climbing script:
using UnityEngine;
using System.Collections;
public class LadderClimb : MonoBehaviour {
public GameObject player;
public GameObject elevator;
public GameObject stairTop;
public bool isClimbing = false;
public float upSpeed;
public float distance;
// Use this for initialization
void Start () {
player = GameObject.Find("Player");
elevator = GameObject.Find("stairElevator");
stairTop = GameObject.Find("stairTop");
}
void OnTriggerEnter (Collider other) {
if (other.gameObject.tag == "Player") {
isClimbing = true;
}
}
void OnTriggerExit (Collider other) {
if (other.gameObject.tag == "Player") {
isClimbing = false;
}
}
// Update is called once per frame
void Update () {
Vector3 difference = new Vector3(stairTop.transform.position.x - elevator.transform.position.x, stairTop.transform.position.y - elevator.transform.position.y, stairTop.transform.position.z - elevator.transform.position.z);
if (isClimbing == true) {
if (difference.y > 0) {
elevator.transform.Translate(Vector3.up * Time.deltaTime);
}
else {
isClimbing = false;
}
if (isClimbing == true) {
if (Input.GetKeyDown(KeyCode.W)) {
player.transform.position = Vector3.MoveTowards(transform.position, elevator.transform.position, upSpeed * Time.deltaTime);
Debug.Log(player.transform.position);
}
}
}
}
}
My issue is the “Vector3.MoveTowards” part of the script (under update). My elevator gameobject moves up my ladder model once I trigger a collider. But when I press the W key nothing happens. I want my player to move towards the elevator gameobject (which is slowly moving into the air, so once my character begins moving towards it it will have the effect of my player climbing a ladder). I am new to scripting so if there is a more efficient way to do this, please let me know!
Think you want GetKey not GetKeyDown. GetKeyDown only returns true for the first frame it’s pressed, it’s for detecting when a new key is pressed not if a key is being held.
My upSpeed is set to 0.1f (1 is way too fast and I get to the top of the ladder in half a second). However, when I set it to 0.1f, it goes up at a realistic speed, but only slightly up. After a second, I go right back down. Is there a way to repeat this process using a for loop or something?
I tried disabling the gravity but it didn’t seem to make a difference. My player still seems to be falling down the ladder after going up a certain distance. The more I increase the upSpeed, the faster I go up, which means theoretically I can get to the top of the ladder with a high upSpeed, but it’s like I am climbing the ladder in half a second, which is of course unrealistic.
using UnityEngine;
using System.Collections;
public class LadderClimb : MonoBehaviour {
public GameObject player;
public bool isClimbing = false;
public float upSpeed;
public float distance;
// Use this for initialization
void Start () {
player = GameObject.Find("Player");
}
void OnTriggerEnter (Collider other) {
if (other.gameObject.tag == "Player") {
isClimbing = true;
}
}
void OnTriggerExit (Collider other) {
if (other.gameObject.tag == "Player") {
isClimbing = false;
}
}
// Update is called once per frame
void Update () {
if (isClimbing == true) {
if (Input.GetKey(KeyCode.W)) {
player.transform.Translate(new Vector3(0, upSpeed * Time.deltaTime, 0));
}
}
}
}
Is it possible to reference the FirstPersonController script which is a part of the Standard Assets? I read somewhere that apparently they’re compiled first, and hence I am not able to reference them in the script. I want to be able to change the “Gravity Multiplier” to 0, which seems to do the trick!