Search Unity

Move player towards gameObject

Discussion in 'Getting Started' started by DocJ, Dec 20, 2022.

  1. DocJ

    DocJ

    Joined:
    Dec 9, 2016
    Posts:
    98
    Hi,
    Can someone please assist me in getting this piece of code working? I am trying to move the Player to the 3rd Child of the Circle gameObject.
    I attached this code to the Player object. I'm not very skilled at Unity, but feel I'm missing something obvious that I can't nail down.

    Thank you.



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Move : MonoBehaviour
    6. {
    7.  
    8.  
    9.     float step = 0.5f * Time.deltaTime;
    10.    
    11.  
    12.     void Start()
    13.     {
    14.        
    15.  
    16.        
    17.     }
    18.  
    19.  
    20.     void Update()
    21.     {
    22.         transform.position = Vector3.MoveTowards(transform.position, circle.transform.GetChild(2), step);
    23.     }
    24. }
     
  2. Nrike458

    Nrike458

    Joined:
    Oct 6, 2019
    Posts:
    37
    So, based on what I can see here, there's no link or variable for the circle gameobject, so the code doesn't have a way to actually know what object you are referencing, what the 3rd child is, or what its position is. Make sure that you include a variable, maybe a public one/Serializefield variable, and link the Circle gameobject to it. Also, maybe include a ".position" after the GetChild(2) call. Let me know if this does/doesn't work, or if I mis-interpreted something.
     
    DocJ likes this.
  3. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    Dude, you're pasting code that has no problem working. The problem is not reading the editor's messages, which highlights what's wrong.

    The community is very happy to help, but it also cannot act as a debugger.

    Make sure you have properly configured Visual Studio Community (the easiest to integrate with Unity), then if you are a novice coder, do at least the first two courses below.
    1. Beginner Scripting: https://learn.unity.com/project/beginner-gameplay-scripting
    2. Intermediate Scripting: https://learn.unity.com/project/intermediate-gameplay-scripting
    3. Junior Programmer Pathway: https://learn.unity.com/pathway/junior-programmer
     
    DocJ likes this.
  4. DocJ

    DocJ

    Joined:
    Dec 9, 2016
    Posts:
    98
    Nrike and Angry Programmer,
    Thanks for the replies, they were both helpful. Based on the suggestions, I've revamped the code and have it running error-free. However, I'm still having trouble with it doing what I want it to do. Any further help would be greatly appreciated.

    In short, I would like to have the player, through the use of another function, stop at a certain waypoint. Then with use of this function, move to a randomly generated child of that particular waypoint (gameObject).

    The script is generating a random Child, but it is only moving the player to the Parent gameObject.

    I've attached the code and commented in it to help out. Thanks again!


    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.     int number;
    10.  
    11.  
    12.     public void Start ()
    13.     {
    14.         number = Random.Range(0, 5);
    15.        
    16.         //Assigns the transform of the child of the Game Object this script is attached to.
    17.         sideChild  = this.gameObject.transform.GetChild(number);
    18.         Debug.Log("sideChild is child #:" + sideChild );
    19.         // This part o the program is working correctly.  Returns a random Child.
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.         // Should move the player to random gameObject Child based on Random.Range
    25.         // Debug.Log shows that Random.Range is returning a random Child
    26.         // Player always only moves to the Parent and stops there
    27.         player.transform.position = Vector3.MoveTowards(transform.position, sideChild.transform.position, 0.5f * Time.deltaTime);
    28.     }
    29. }
    30.  
     
  5. DocJ

    DocJ

    Joined:
    Dec 9, 2016
    Posts:
    98
    I've reworked the code but I'm still not getting the desired result. I attached it in a separate reply above this one. Thanks again for any help!
     
  6. DocJ

    DocJ

    Joined:
    Dec 9, 2016
    Posts:
    98
    I've taken your suggestion and reworked the code, as seen above, but still don't have it working correctly. Any and all help is appreciated!
     
  7. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    What you want to do is similar to one of the Junior Programmer Pathway lessons. Lesson 4.2 specifically, but it's worth going over the entire topic.

    I did these lessons myself, even though I have been programming for many years.

    https://learn.unity.com/project/uni...5ec21a20af&missionId=5f7648a4edbc2a5578eb67df

    In short, the lesson is about generating opponents who then attack the player trying to push him out of the arena like in sumo.
     
  8. DocJ

    DocJ

    Joined:
    Dec 9, 2016
    Posts:
    98
    I appreciate the help, but after spending an afternoon going through the Pathway lessons, it didn’t really address the issues I’m having with why my player stops at the parent of the gameObject and doesn’t move to the child that was randomly selected.
     
  9. Nrike458

    Nrike458

    Joined:
    Oct 6, 2019
    Posts:
    37
    With that MoveTowards function, you need to change the first part of it(the "transform.position") to the players transform.position. Currently, it is exhibiting the behavior of what you mentioned. When you change it, you are letting that function know the current players position, not the waypoints position, and telling it to move towards the child object. That little change should get things going.
     
    DocJ likes this.
  10. DocJ

    DocJ

    Joined:
    Dec 9, 2016
    Posts:
    98
    You are absolutely correct. That’s what it turned out to be after all. I was using the starting waypoint instead of the player’s starting position.

    Thank you for all your help. I truly appreciate it.
     
  11. Nrike458

    Nrike458

    Joined:
    Oct 6, 2019
    Posts:
    37
    No problem! Good luck with your project!
     
    DocJ likes this.