Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by Mechanics55, Aug 13, 2015.

  1. Mechanics55

    Mechanics55

    Joined:
    Jul 27, 2015
    Posts:
    14
    So I have this code here, on its way to becoming a working ladder climbing script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LadderClimb : MonoBehaviour {
    5.     public GameObject player;
    6.     public GameObject elevator;
    7.     public GameObject stairTop;
    8.    
    9.     public bool isClimbing = false;
    10.    
    11.     public float upSpeed;
    12.     public float distance;
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.         player = GameObject.Find("Player");
    17.         elevator = GameObject.Find("stairElevator");
    18.         stairTop = GameObject.Find("stairTop");
    19.     }
    20.  
    21.    
    22.     void OnTriggerEnter (Collider other) {
    23.         if (other.gameObject.tag == "Player") {
    24.             isClimbing = true;
    25.         }
    26.     }
    27.    
    28.     void OnTriggerExit (Collider other) {
    29.         if (other.gameObject.tag == "Player") {
    30.             isClimbing = false;
    31.         }
    32.     }
    33.    
    34.     // Update is called once per frame
    35.     void Update () {
    36.         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);
    37.        
    38.         if (isClimbing == true) {
    39.             if (difference.y > 0) {
    40.                 elevator.transform.Translate(Vector3.up * Time.deltaTime);
    41.             }
    42.             else {
    43.                 isClimbing = false;
    44.             }
    45.             if (isClimbing == true) {
    46.                 if (Input.GetKeyDown(KeyCode.W)) {
    47.                     player.transform.position = Vector3.MoveTowards(transform.position, elevator.transform.position, upSpeed * Time.deltaTime);
    48.                     Debug.Log(player.transform.position);
    49.                 }
    50.             }
    51.            
    52.         }
    53.     }
    54. }
    55.  
    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!
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    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.
     
  3. Mechanics55

    Mechanics55

    Joined:
    Jul 27, 2015
    Posts:
    14
    Now it is making me run in place, sort of glitchy. It's not translating me upwards at all. Any ideas?
     
  4. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    upSpeed may be zero?
     
  5. Mechanics55

    Mechanics55

    Joined:
    Jul 27, 2015
    Posts:
    14
    I have it at 5. Any other number pretty much has the same result.
     
  6. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Is the first argument to MoveTowards() the correct one? Shouldn't it be player.transform.position?
     
  7. Mechanics55

    Mechanics55

    Joined:
    Jul 27, 2015
    Posts:
    14
    I'm not quite sure what you're asking. I have the player.transform.position before the Vector3.MoveTowards() method.
     
  8. Carvuh

    Carvuh

    Joined:
    Mar 25, 2013
    Posts:
    25
    If this is just a simple ladder, you could try something like this?

    Code (csharp):
    1. // Move the object upward in world space 1 unit/second.
    2. transform.Translate(Vector3.up * Time.deltaTime, Space.World);
    http://docs.unity3d.com/ScriptReference/Transform.Translate.html

    Throw this into here:

    Code (csharp):
    1.  
    2. if (isClimbing == true) {
    3.     if (Input.GetKey(KeyCode.W)) {
    4.         transform.Translate(Vector3.up * Time.deltaTime, Space.World);
    5.     }
    6. }
    7.  
    If you ever wanted him to go down, I'm sure you could use something like,

    Code (csharp):
    1. Vector3.down
    Instead of up. I'm not at home, so I havent been able to try this.
     
  9. Mechanics55

    Mechanics55

    Joined:
    Jul 27, 2015
    Posts:
    14
    I think I found a good solution. I used this code:

    Code (CSharp):
    1.     void Update () {
    2.         if (isClimbing == true) {
    3.             if (Input.GetKey(KeyCode.W)) {
    4.                 player.transform.Translate(new Vector3(0, upSpeed, 0));
    5.                 Debug.Log(player.transform.position);
    6.             }
    7.         }
    8.            
    9.     }
    10. }
    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?
     
  10. Carvuh

    Carvuh

    Joined:
    Mar 25, 2013
    Posts:
    25
    The only way that you would be going back down is that you are resetting your player's transform somewhere else in the code?
     
  11. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    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.

    Code (csharp):
    1.  
    2. player.transform.tRanslate(new Vector3(0, upSpeed * Time.deltaTime, 0));
    3.  
    2) Do you have gravity on? You should disable it when on a ladder.
     
  12. Mechanics55

    Mechanics55

    Joined:
    Jul 27, 2015
    Posts:
    14
    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.
     
  13. Mechanics55

    Mechanics55

    Joined:
    Jul 27, 2015
    Posts:
    14
    Also, here is my full code if it helps at all:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LadderClimb : MonoBehaviour {
    5.     public GameObject player;
    6.    
    7.     public bool isClimbing = false;
    8.    
    9.     public float upSpeed;
    10.     public float distance;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         player = GameObject.Find("Player");
    15.     }
    16.  
    17.    
    18.     void OnTriggerEnter (Collider other) {
    19.         if (other.gameObject.tag == "Player") {
    20.             isClimbing = true;
    21.         }
    22.     }
    23.    
    24.     void OnTriggerExit (Collider other) {
    25.         if (other.gameObject.tag == "Player") {
    26.             isClimbing = false;
    27.         }
    28.     }
    29.    
    30.     // Update is called once per frame
    31.     void Update () {
    32.         if (isClimbing == true) {
    33.             if (Input.GetKey(KeyCode.W)) {
    34.                 player.transform.Translate(new Vector3(0, upSpeed * Time.deltaTime, 0));
    35.             }
    36.         }  
    37.     }
    38. }
     
  14. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Nothing here forces you down.

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

    Mechanics55

    Joined:
    Jul 27, 2015
    Posts:
    14
    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!