Daniel Lip
Daniel Lip

Reputation: 11317

How can I make the object move back to his original position after he reached the target?

Starting by :

StartCoroutine(ThrowObject(objToThrow.transform, primaryTarget.transform, throwSpeed));

Then :

IEnumerator ThrowObject(Transform objectToMove, Transform toPosition, float duration)
    {
        float counter = 0;

        while (counter < duration)
        {
            counter += Time.deltaTime;
            Vector3 currentPos = objectToMove.position;

            float time = Vector3.Distance(currentPos, toPosition.position) / (duration - counter) * Time.deltaTime;

            objectToMove.position = Vector3.MoveTowards(currentPos, toPosition.position, time);

            yield return null;
        }
    }

When the objectToMove has reached the target(toPosition) then I want the script to do some action for now does not matter what waiting 5 seconds or make some effect whatever but to do something and then to make the objectToMove to return back to the original position it was thrown from the same speed.

This is what I tried :

IEnumerator ThrowObject(Transform objectToMove, Transform toPosition, float duration)
    {
        float counter = 0;

        while (counter < duration)
        {
            counter += Time.deltaTime;
            Vector3 currentPos = objectToMove.position;

            float time = Vector3.Distance(currentPos, toPosition.position) / (duration - counter) * Time.deltaTime;

            objectToMove.position = Vector3.MoveTowards(currentPos, toPosition.position, time);

            yield return null;
        }

        StartCoroutine(ThrowBack(objectToMove, toPosition, duration));
    }

    IEnumerator ThrowBack(Transform objectToMove, Transform toPosition, float duration)
    {
        float counter = 0;

        while (counter < duration)
        {
            counter += Time.deltaTime;
            Vector3 currentPos = objectToMove.position;

            float time = Vector3.Distance(currentPos, toPosition.position) / (duration - counter) * Time.deltaTime;

            objectToMove.position = Vector3.MoveTowards(toPosition.position, currentPos, time);

            yield return null;
        }
    }

But it's never move it back. It's getting to the ThrowBack to the line :

objectToMove.position = Vector3.MoveTowards(toPosition.position, currentPos, time);

But the objectToMove is never move back and move at all it keep staying at the target position.

Upvotes: 1

Views: 219

Answers (1)

Jadon Wolfgang
Jadon Wolfgang

Reputation: 180

The easiest thing to do is to get the start position at the start of the program or instantiation of the object like this:

//Variables
private Vector3 startPos;

//Code
private void Start() //or when you want to get the position
{
    startPos = transform.position;
}

//when you want to move back to the original position do this:

  objectToMove.position = Vector3.MoveTowards(startPos);

for your instance i would likely do the start void in the start of the enumerator. this will set startpos, but startpos is only 1 private variable, so you would need to also declare it in the enumerator so it will be relative to each objectToMove.

Upvotes: 1

Related Questions