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 to stop moveTowards?

Discussion in 'Scripting' started by iRyan, Oct 20, 2015.

  1. iRyan

    iRyan

    Joined:
    Jan 16, 2013
    Posts:
    20
    I noticed Vector3.moveTowards causes the gameobject to constantly move to the same position even if i control it to go another place with key controls and I want it to stop when it reaches the gameobject or position.
     
  2. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    can you post your code?

    normaly you can make if statements which is set to true or false.
    or you can set targetpos to transform.pos when you don't push any button.
    atm i don't know what your button inputs do to the targetposition or how you handle it that you have this problems :)
     
  3. iRyan

    iRyan

    Joined:
    Jan 16, 2013
    Posts:
    20
    Hi, I'm currently trying to create path nodes whereby the user's character will move to these predetermined points and the only thing that the user can control is the speed but I have not yet started on doing the speed control yet as I am still trying to fiddle with the movement first.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MoveTowardsDirection : MonoBehaviour
    5. {
    6.    
    7.     public Transform target1;
    8.     public Transform target2;
    9.     public float speed;
    10.     public Transform target;
    11.    
    12.    
    13.     // Use this for initialization
    14.     void Start ()
    15.     {
    16.        
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update ()
    21.     {
    22.         MoveToTarget ();
    23.        
    24.     }
    25.     void MoveToTarget ()
    26.     {
    27.         //float step = speed * 10;
    28.  
    29.         for (int i = 0; i < 3; i++) {
    30.            
    31.             if (i == 1) {
    32.                
    33.                 target = target1;
    34.                 transform.position = Vector3.MoveTowards (transform.position, target.position, speed);
    35.                 if (GameObject.FindGameObjectWithTag ("Bike").transform.position == GameObject.FindGameObjectWithTag ("NANANA Batman").transform.position) {
    36.                     //GameObject.FindGameObjectWithTag("Bike").transform.Rotate(0,-10,0);
    37.                    
    38.                     transform.LookAt (target2);
    39.                 }
    40.             } else if (i == 2) {
    41.                
    42.                 target = target2;
    43.                 transform.position = Vector3.MoveTowards (transform.position, target.position, speed);
    44.                 if (GameObject.FindGameObjectWithTag ("Bike").transform.position == GameObject.FindGameObjectWithTag ("Stop sign").transform.position) {
    45.  
    46.                 }
    47.                
    48.             }
    49.            
    50.         }
    51.  
    52.        
    53.     }  
    54.    
    55. }
    56.  
     
  4. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    As far as your code is working you will never reach your target. because every frame your gameobject is moving to target1 and than to target2.
     
  5. iRyan

    iRyan

    Joined:
    Jan 16, 2013
    Posts:
    20
    Hi, I'm relatively new to programming so I do not know of a way to solve this, would you be able to provide a better way to go around doing this?
     
  6. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    so when i think correct, you want to first reach target1 and than target2?
     
  7. iRyan

    iRyan

    Joined:
    Jan 16, 2013
    Posts:
    20
    Yes, the gameobject would move progressively from the start point to towards target1 and then to target2.

    Something like path nodes in unreal engine
     
  8. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
  9. iRyan

    iRyan

    Joined:
    Jan 16, 2013
    Posts:
    20
    Hi currently this is my scene view, the bike is the main character, and the batman and stop signs are going to be the waypoints.
    1321.PNG
     
  10. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    your problem is solved in your other post ;)
     
    iRyan likes this.
  11. iRyan

    iRyan

    Joined:
    Jan 16, 2013
    Posts:
    20
    Hi, the nav mesh works perfectly, thanks alot!