Search Unity

Question Vector3.MoveTowards help needed

Discussion in 'Getting Started' started by DocJ, Jan 2, 2023.

  1. DocJ

    DocJ

    Joined:
    Dec 9, 2016
    Posts:
    98
    No matter what resource I look at, I just can't figure out what I have wrong with this code.

    I'm trying to have the Player gameObject move to a randomly selected Child of another gameObject.

    Debug.Log shows that I'm getting a random Child but the Player only moves to the Parent and stops there.

    Can someone please identify the problem? I'm not getting any errors in the console.

    Thank you so much!!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GetChild : MonoBehaviour
    6. {
    7.     private Transform sideChild;
    8.     public GameObject player;
    9.     public float speed = 1.0f;
    10.     int number;
    11.  
    12.  
    13.     public void Start ()
    14.     {
    15.         number = Random.Range(0, 5);
    16.        
    17.         //Assigns the transform of the child of the Game Object this script is attached to.
    18.        
    19.         sideChild  = this.gameObject.transform.GetChild(number);
    20.  
    21.         Debug.Log("sideChild is child #:" + sideChild );
    22.  
    23.  
    24.         // This part of the program is working correctly.  Returns a random Child.
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.         // Should move the player to randomly selected Child of gameObject based on Random.Range
    30.         // Debug.Log shows that Random.Range is returning a random Child
    31.         // Player always only moves to the Parent and stops there
    32.  
    33.         float step = speed * Time.deltaTime;
    34.  
    35.         player.transform.position = Vector3.MoveTowards(this.gameObject.transform.position, sideChild.position, step);
    36.     }
    37. }
    38.  
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,043
    Maybe debug sideChild.position to see what that value is?

    You could also try lerping the value instead to see if that works
     
    DocJ likes this.
  3. DocJ

    DocJ

    Joined:
    Dec 9, 2016
    Posts:
    98
    I took your advice and added a Debug.Log(sideChild.position);

    After running multiple times, the position of the sideChild did change with each run but, and I'm not sure it's supposed to, it didn't match the transform position of each Child in the scene. It was consistent with it's position numbers each time for each Child during the run throughs.

    I haven't tried lerping yet. I don't know how to do that so will need to research it.

    I do appreciate your help!
     
  4. DocJ

    DocJ

    Joined:
    Dec 9, 2016
    Posts:
    98
    Tried Lerping based on the Unity documentation and that didn't work either. I didn't get any errors so I hope I was using it correctly.
     
  5. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,429
    (Edited: mis-read your original code a bit.)

    You were just using MoveTowards backwards. You give the current position of the thing you're moving, then the destination, then the step.

    Code (csharp):
    1. player.transform.position = Vector3.MoveTowards(player.transform.position, sideChild.position, step);
     
    DocJ likes this.
  6. DocJ

    DocJ

    Joined:
    Dec 9, 2016
    Posts:
    98
    THAT DID IT!!!

    I'm so glad that worked! You were absolutely right! I was using the gameObject starting point when I should have been using the Player starting point instead.

    Thank you very much!!