2
\$\begingroup\$

I built a subGraph in Unity Visual Scripting to move a GameObject ("returnThisGameObject") from its current location (Point A) to its originalPosition (Point B).

enter image description here

The code run dozen of times a minute and usually does its job. But sometimes the GameObject doesn't move the full distance, and just stops part of the way there. The While loop in the subgraph doesn't finish in those cases, as subsequent graphs after this one do not run.

I could hack it so that the While loop only runs a certain number of frames before moving on. But I'm trying to figure out the root cause of why the While loop doesn't always work. I am not very familiar with Smooth Damp (or LERP) though, so I could use some guidance.

Any ideas why this While Loop isn't always completing?

EDIT: As suggested, I added a variable for objectVelocity and plugged it into the Smooth Damp node on both sides. Still encountering the issue where the GameObject doesn't always make it all the way to Point B...

enter image description here

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Is there anything in the scene it could be getting stuck on, like a collider (even an edge between two colliders that should make a flush seam in the floor with no protruding obstacle)? Do you have any other script in play that could be interrupting or fighting with this one for control of the position? To test things like this, try to make a Minimal Complete Verifiable Example: one scene just a single object running only this script. Does the problem still occur in that case? If not, try adding one of the other scripts or scene elements and test again. \$\endgroup\$
    – DMGregory
    Commented Mar 9, 2023 at 13:16

1 Answer 1

0
\$\begingroup\$

Just wanted to confirm once and for all that there was nothing wrong with the code above! It does successfully and consistently move a GameObject from Point A to Point B.

But when I tried to move 2 GameObjects in rapid succession.... sometimes the first GameObject would not make it all the way to its target.

Here's how I had the code structured:

  • I had the movement code running in a single GameObject called "Move Manager".
  • Move Manager had a Queue that stacked up the GameObjects to be moved,
  • Move Manager had code would move the top GameObject from Point A to Point B... then once that was done, it would move the the next GameObject from Point A to Point B... etc.

The code worked perfectly if it only had to move one GameObject. But for some reason that I still don't understand, if two GameObjects were moved in rapid succession... sometimes the first GameObject would stop moving before it reached its target.

To solve this, I took the movement code inside of Move Manager and moved it into a Prefab for the GameObjects. As a result:

  • The Move Manager could manage the queue of GameObjects (to be moved)
  • And the GameObjects themselves would handle their own movement.

This solved the problem completely. I am still not sure why the movement code didn't work when it was inside Move Manager, but am just glad it's all working now.

Thanks to everyone who helped me confirm that there wasn't a problem in the movement code! This helped me isolate the problem, and ultimate figure out a workaround.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .