0
\$\begingroup\$
InteractableItem target;
    private void Update()
    {
        if (target != null && target.name == "kid_from_space_helmet")
            objToThrow.transform.position += (target.transform.position - objToThrow.transform.position).normalized * 2f * Time.deltaTime;

        if(Vector3.Distance(objToThrow.transform.position, target.transform.position) < 1f)
        {
            objToThrow.transform.position += (objToThrow.transform.position - target.transform.position).normalized * 2f * Time.deltaTime;
        }
    }

The object is moving to the target but never moving back. This IF is not working good :

if(Vector3.Distance(objToThrow.transform.position, target.transform.position) < 1f)

My guess is that the problem is that when the object distance is less then 1 he try to move back but then he is not less then 1 anymore. but anyway it's not working.

I want to move the object to the target then wait 1 second at the target and then to move the object back.

\$\endgroup\$
2
  • \$\begingroup\$ I want to move the object to the target then wait 1 second at the target and then to move the object back. Looks like you need something like yield return new WaitForSeconds(.1f); for your delay, some kind of state like a bool isGoingBack = true; to tell your object to go back... \$\endgroup\$
    – Vaillancourt
    Oct 27, 2020 at 11:29
  • 1
    \$\begingroup\$ Another problem I see here is that the object can not return to its original position when it doesn't know what its original position was. Its behavior is currently based only on its current position. But that's another problem for another question. \$\endgroup\$
    – Philipp
    Oct 27, 2020 at 12:53

1 Answer 1

1
\$\begingroup\$

The problem is that the if-statement will only be true while the object is close to the target. As soon as it moved far enough back to no longer be within 1.0 units, it returns to its old behavior of moving back to the target.

What you have here is an object which can be in two different states. The "moving to target" state and the "moving to origin" state. So you need to:

  • Keep track of which state the object is currently in
  • Change that state when the condition for state-change is fulfilled
  • Make the object behave differently depending on which state it is currently in

As long as you only have two states, you can store the current state in a bool variable.

private bool isMovingBack = false; // the state the object is currently in

private void Update() {
    // check if the condition for state-change is fulfilled
    if(Vector3.Distance(objToThrow.transform.position, target.transform.position) < 1f) {
        isMovingBack = true;
    }
        
    // behave according to current state:
    if (isMovingBack == false) {
        objToThrow.transform.position += (target.transform.position - objToThrow.transform.position).normalized * 2f * Time.deltaTime;
    } else {
        objToThrow.transform.position += (objToThrow.transform.position - target.transform.position).normalized * 2f * Time.deltaTime;
    }
}

A boolean state is fine as long as the object can only be in one of two states. But when you start creating objects which can be in more than two states, then it might be worth it to look into finite state machines. One simple way to implement this in C# is to have a class-local enum for all the states the object can be in and then have a switch-statement in your Update-method which runs different code depending on the current state.

\$\endgroup\$

You must log in to answer this question.

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