How do I get the player to move towards a game object?

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.

Now it is making me run in place, sort of glitchy. It’s not translating me upwards at all. Any ideas?

upSpeed may be zero?

I have it at 5. Any other number pretty much has the same result.

Is the first argument to MoveTowards() the correct one? Shouldn’t it be player.transform.position?

I’m not quite sure what you’re asking. I have the player.transform.position before the Vector3.MoveTowards() method.

If this is just a simple ladder, you could try something like this?

// Move the object upward in world space 1 unit/second.
transform.Translate(Vector3.up * Time.deltaTime, Space.World);

Throw this into here:

if (isClimbing == true) {
    if (Input.GetKey(KeyCode.W)) {
        transform.Translate(Vector3.up * Time.deltaTime, Space.World);
    }
}

If you ever wanted him to go down, I’m sure you could use something like,

Vector3.down

Instead of up. I’m not at home, so I havent been able to try this.

I think I found a good solution. I used this code:

    void Update () {
        if (isClimbing == true) {
            if (Input.GetKey(KeyCode.W)) {
                player.transform.Translate(new Vector3(0, upSpeed, 0));
                Debug.Log(player.transform.position);
            }
        }
           
    }
}

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?

The only way that you would be going back down is that you are resetting your player’s transform somewhere else in the code?

  1. You’re not translating with a delta time, so your game isn’t framerate independent. Certain users will go up the user faster than others.
player.transform.tRanslate(new Vector3(0, upSpeed * Time.deltaTime, 0));
  1. Do you have gravity on? You should disable it when on a ladder.

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.

Also, here is my full code if it helps at all:

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));
            }
        }   
    }
}

Nothing here forces you down.

It has to be in your other player movement scripts or gravity.

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!