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

Vector3.MoveTowards question?

Discussion in 'Scripting' started by Quincey, Mar 5, 2020.

  1. Quincey

    Quincey

    Joined:
    Aug 14, 2019
    Posts:
    46
    I've done some serious research trying to find out more about a Vector3.Move Towards I read all the static parameters in the Unity API. I've looked at a few videos on YouTube to make sure I got the general purpose of Vector3.MoveTowards. The issue I'm having a hard time figuring out is how do you "actually" stop a V3.MoveTowards since it's called in the Update() function can 3.MoveTowards be stopped?

    The reason I'm asking is because I created an empty scene with a cube as the targetPosition and I have a little silly green low poly dragon that slides across the ground made from a cube. When it gets to the cube it keeps pushing up against the cube. What ideas do you think would work if I wanted the dragon to stop 1-2 units from the cube? Any feedback would be greatly appreciated as usual and sorry to ask such a silly or basic question.
     
  2. Nyfirex

    Nyfirex

    Joined:
    Jun 26, 2019
    Posts:
    179
    Check first the distance before doing move towards
    Code (CSharp):
    1. Update(){
    2.   if (Vector3.Distance(transform.position, target.position) < distance) {
    3.         //Move towards
    4.    }
    5.  
    6. }
     
    Quincey likes this.
  3. Quincey

    Quincey

    Joined:
    Aug 14, 2019
    Posts:
    46
    Nyfirex,

    In my previous code I was using I tried implementing what you suggested, but it's still doing the same thing it walk up to the cube and continues to keep walking and pushing the cube lol. I've tried experimenting with different variables from floats to Vector3 so I'm not really sure at this point if I'm messing something up and truthfully speaking I'm sure it's something I've done wrong in my code.
    I'm thinking maybe I need to use a NavMesh and apply stoppingDistance to get the desired outcome, because this code I've typed up makes the gameobject lookat the cube and walk straight to it without stopping 1-2 units away from the target. I've looked at so many videos on Vector3.MoveTowards and read the API on MoveTowards hoping I'd find something in the static conditions that would allow you to simply type a syntax similar to "Vector3.Stop" but unfortunately this doesn't exist. If you think it's my variables I'm using or have the wrong operator could you let me know? The small tutorial online only showed how to make gameobjects move from point A to point B. Now I'm trying to learn by building off this tutorial.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GreenDragon : MonoBehaviour
    6. {
    7.  
    8.     public Animator anim;
    9.     public Rigidbody rigid;
    10.     public Transform targetPosition;
    11.     public float dragonSpeed;
    12.     public float stopDistance = 2f;
    13. //  public Vector3 endPosition;
    14.     public Vector3 direction;
    15.     public float endPosition;
    16.    
    17.  
    18.     // Start is called before the first frame update
    19.     void Awake()
    20.     {
    21.         anim = GetComponent<Animator>();
    22.         rigid = GetComponent<Rigidbody>();
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         GoToPlayer();
    29.  
    30.         direction = Vector3.MoveTowards(transform.position, targetPosition.position, dragonSpeed* Time.deltaTime);
    31.  
    32. //      endPosition = targetPosition.position;
    33.     }
    34.  
    35.     public void GoToPlayer()
    36.     {
    37.         this.transform.LookAt(targetPosition);
    38.  
    39.         this.transform.position = Vector3.MoveTowards(transform.position, targetPosition.position, dragonSpeed * Time.deltaTime);
    40.  
    41.       if (Vector3.Distance(transform.position, targetPosition.position) > stopDistance)
    42.       {
    43.             endPosition = stopDistance;
    44.       }
    45.  
    46. /*      if (Vector3.Distance(stopDistance <= targetPosition.position) <= 2f)
    47.         {
    48.             endPosition = stopDistance;
    49.         }
    50. */
    51.     }
    52. }
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,386
    What Nyfirex suggested was to only movetoward the object only if the distance is greater than a certain amount, but the code you posted still does the movetoward no matter what. You are testing the distance, but you aren't doing anything different when the distance meets your condition.
     
    Quincey likes this.
  5. Nyfirex

    Nyfirex

    Joined:
    Jun 26, 2019
    Posts:
    179
    Yes it's wrong. The code you wrote is doing nothing, let me explain. Inside Update() you call GoToPlayer(). GoToPlayer() the first line is LookAt, the second one MoveTowards then you have the if statament that doesn't have control to MoveTowards. You have to write MoveTowards inside the if statament. Another thing you don't need is endPosition
     
    Quincey likes this.
  6. Quincey

    Quincey

    Joined:
    Aug 14, 2019
    Posts:
    46
    Yikes,

    I really appreciate the candor and honest feedback I literally thought I was on the right track, but you mentioned that my code is literally doing nothing. I went back to my code so I could follow what you was telling me. I assumed the GoToPlayer() function would have the little dragon look at the cube and then slide over to the cube. I really thought the if-statement was saying if the position of the dragon and position to the cube is less than the stopDistance the code inside of the code block would cause it to stop at the endPosition