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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

problem with movetowards

Discussion in 'Scripting' started by blober81, Aug 24, 2018.

  1. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    Hi everyone,

    When I move an object with movetowards to it's destination it gives no problems but when I change the destination before it reaches it's previous destination the object doesn't move towards it's newest target location.

    Code (CSharp):
    1.    
    2. void PosSet()
    3. {
    4. newPOS = wPpos;
    5. }
    6.  
    7. void Update()
    8.     {
    9.         Dummy1.transform.position = Vector3.MoveTowards(new Vector3(Dummy1.transform.position.x,      Dummy1.transform.position.y,0), newPOS, 8 * Time.deltaTime);
    10.     }
    11.  
    12.     void AwayFA()
    13.     {
    14.         if (transform.position.y > distChck) //when Dummy1 reaches a certain y-value (distChck) the target destination vector will be adjusted. When dc is true the Dummy1 object should fly back to it's starting point (strtPoS) but the x-value of it's destination doesn't match with the starting point. The y-value does however.
    15.         {
    16.             if (!dc)
    17.             {
    18.                 if (!PosX) //linksom
    19.                 {
    20.                     newPOS.x = newPOS.x - 7f;
    21.                     newPOS.y = newPOS.y + 3.5f;
    22.                 }
    23.                 else //rechtsom
    24.                 {
    25.                     newPOS.x = newPOS.x + 7f;
    26.                     newPOS.y = newPOS.y + 3.5f;
    27.                 }
    28.                 distChck = Dummy1.transform.position.y - (0.5f * 3.5f);
    29.                 dc = true;
    30.             }
    31.             else
    32.             {
    33.                 newPOS = strtPoS;
    34.                 Breturn = true;
    35.             }
    36.         }
    37.     }

    Does movetowards malfunction when I change the target destination while running or is there something else going on.

    Thanks in advance !
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    Probably you override somewhere position with PosSet ()

    On side note
    newPOS.y = newPOS.y + 3.5f ;
    is same what
    newPOS.y += 3.5f ;

    Do Debug.Log and/or breakpoints, to track problem down.
     
  3. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    Thanks for your reply. I made the code very simple so I can emphasize the core of the problem.
    The object will change destination before it reaches it's first destinaton (goal1) but it will never reach it's new destination (goal2).

    The file is uploaded so you can see it for yourself. The problem is that the object doesn't move to it's given destination when using movetowards.

    Here is the code;
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class move2 : MonoBehaviour {
    6.  
    7.     Vector3 newPOS;
    8.  
    9.     // Use this for initialization
    10.     void Start ()
    11.     {
    12.         newPOS = GameObject.Find("goal1").transform.position;
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update ()
    17.     {
    18.         SetNewPos();
    19.  
    20.         this.transform.position = Vector3.MoveTowards(new Vector3(transform.position.x, transform.position.y, 0), newPOS, 8 * Time.deltaTime);
    21.     }
    22.  
    23.     void SetNewPos()
    24.     {
    25.         if (transform.position.y > (newPOS.y - 2))
    26.         {
    27.             Debug.Log("change destination");
    28.             newPOS = GameObject.Find("goal2").transform.position;
    29.         }
    30.     }
    31. }
    32.  
    Here is the file;

    https://ufile.io/m4ich
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    Just to double check and confirm.
    Are you want to move in right axis?
    In normal Unity 3D convention, forward/back axis is Z axis, while it is set to 0, in your case.
    While, Y axis is up/down and X is left/right.
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    So what does it do instead? Just stop? Go to some other location? Throw an error (perhaps a null reference exception)?

    Try adding another Debug.Log at line 29, to print out the value of newPOS after you think you've changed it.

    Also consider making newPOS public so you can watch it in the inspector. I suspect it's not what you think it is.
     
  6. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    It's top down so the z-axis is left untouched.
     
  7. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    The object moves to a different vector despite NewPos and goal2 having the same vector values. If you don't trust the file I added I can make a video to show what happens and upload it to Youtube.
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    Not sure what setup you have there, but this worked for me

    Code (CSharp):
    1. using UnityEngine;
    2. public class Move3 : MonoBehaviour {
    3.     Vector3 newPOS;
    4.  
    5.     public Transform goal1 ;
    6.  
    7.     public Transform goal2 ;
    8.  
    9.     public bool isSet = true ;
    10.     // Use this for initialization
    11.     void Start ()
    12.     {
    13.         newPOS = goal1.position;
    14.     }
    15.     // Update is called once per frame
    16.     void Update ()
    17.     {
    18.         if ( isSet )
    19.         {
    20.             isSet = false ;
    21.  
    22.             SetNewPos();
    23.         }
    24.                
    25.         this.transform.position = Vector3.Lerp (new Vector3(transform.position.x, transform.position.y, 0), newPOS, 1 * Time.deltaTime);
    26.     }
    27.     void SetNewPos()
    28.     {
    29.         if (transform.position.y > (newPOS.y - 2))
    30.         {
    31.             Debug.Log("change destination");
    32.             newPOS =goal2.position;
    33.         }
    34.     }
    35. }
     
  9. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    Thanks for your reply. I used your code and in my setup the object moves to goal1 without changing destination to goal2. All my objects are empty game objects.

    Also when I add a child object to my empty gameobject the y-position is much higher than the goal y-position while having the same y-vector value in the inspector.

    Is there anyway I can mail or share this file so you can inspect my setup ? It makes it easier for you to help me out. Thanks in advance.
     
    Last edited: Aug 24, 2018
  10. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    You trigger bool flag in game object with script, to change direction. Tick the bool flag in inspector.
    Objects can be empty objects that fine. Is just bit harder to visualize. Make sure they are in different positions.
     
  11. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    Here is my setup:
    https://ibb.co/gJ7OTU

    Here's my video


    Notice that my bomber object moves way beyond my goal1 and goal2 object.

    I changed your bool to y-position.

    Code (CSharp):
    1. using UnityEngine;
    2. public class Move3 : MonoBehaviour
    3. {
    4.     Vector3 newPOS;
    5.  
    6.     public Transform goal1;
    7.  
    8.     public Transform goal2;
    9.  
    10.     public bool isSet = true;
    11.     // Use this for initialization
    12.     void Start()
    13.     {
    14.         newPOS = goal1.position;
    15.     }
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         //if (isSet)
    20.         if (transform.position.y > (goal1.position.y - 1))
    21.         {
    22.            // isSet = false;
    23.             SetNewPos();
    24.         }
    25.  
    26.         this.transform.position = Vector3.Lerp(new Vector3(transform.position.x, transform.position.y, 0), newPOS, 1 * Time.deltaTime);
    27.     }
    28.     void SetNewPos()
    29.     {
    30.         Debug.Log("SetNewPos");
    31.         if (transform.position.y > (newPOS.y - 0.5f))
    32.         {
    33.             Debug.Log("change destination");
    34.             newPOS = goal2.position;
    35.         }
    36.     }
    37. }
     
  12. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Please add Debug.Logs as I suggested above. You are making some assumption that is incorrect. Debugging is the process of finding out what your incorrect assumptions are. So Debug.Log every step, and compare what you get to what you expected.
     
  13. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    I added a debug.log at line 29 and the position vector of my movable object (bomber) and my destination object is the same. I didn't got any errors. Not sure though if that is what you mean by adding debug.logs. You want me to upload another video were you can see my position vectors in my debug.logs ?

    If possible I'd like to sent you the file so you can see it for yourself. It makes it easier to detect were the problem is.
    https://ufile.io/m4ich
     
    Last edited: Aug 24, 2018
  14. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    You want to add a Debug.Log at every step, so you can see where what you think is happening differs from what is actually happening.

    That means, where you switch to goal2, you should have code like:

    Code (csharp):
    1.  
    2.   Debug.Log("changed to " + goal2 + " at " + newPOS);
    3.  
    and if that still doesn't lead to insight, then in your Update method (which by the way is now incorrectly using Lerp rather than MoveTowards), you should have:

    Code (csharp):
    1.  
    2.   Debug.Log("Moving towards " + newPOS + "; now at " + transform.position);
    3.  
    and actually read threw the spew this produces in the console. I guarantee you that MoveTowards does what the docs claim it should do. The problem is somewhere else, e.g., your goal2 isn't what you think it is, or it isn't where you think it is, or your y-position-based logic is wrong for where your goals are, or something like that. Don't ask us to solve it for you; you are the one sitting at your computer, in front of your project, and that is where the problem lies. Just keep gathering clues and checking assumptions, and at some point you will see what assumption is false, smack yourself in the forehead, and be on your way. :)
     
    Antypodish likes this.
  15. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    Hey guys I fixed the problem by using an empty gameobject as target destination without any child objects attached to it. That way my object moves to it's destination. Thanks everyone for your help.

    cheers
     
  16. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    It doesn't matter whether or not it has children. My guess is, in switching to the empty gameobject, you inadvertently fixed whatever wasn't properly assigned before.

    But anyway, I'm glad you got it working! :)
     
    blober81 likes this.
  17. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    If you're going to do that, you might want to consider using the debugger instead. Just press F9 then F5 and you're away. No coding, no recompiling, no forgetting to back out coding, no thinking "oh if only I'd logged out that other parameter instead", etc. :)
     
    Antypodish, JoeStrout and blober81 like this.
  18. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Yes, if you can learn to use the debugger that's even better. But sometimes new users find that intimidating (and the how-to details depend on what platform and code editor you use), so I usually start by suggesting Debug.Logs.

    But definitely learn to use the debugger at some point; nothing like stepping through your code line by line to see what's going on!
     
    Antypodish and Doug_B like this.
  19. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    Same here. I rather give a rod (debugging knowledge/exerciser), rather a fish (quick temp solution). Is very important skill, resolving many issues.
     
    JoeStrout likes this.