Home > Net >  Unity DOTween move an instantiated object using DOJump
Unity DOTween move an instantiated object using DOJump

Time:01-12

¡Hi! I'm having a problem trying to move an object that I instantiated. What I want is that when the object appears, it "jumps" towards the character to get it using this DOTween command:

DOJump(Vector3 endValue, float jumpPower, int numJumps, float duration, bool snapping).

I'm looking for an effect similar to this: https://youtu.be/Mk4R241AZxk?t=786

The current code:

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        StartCoroutine(SpawnItem(0.5f, 0.8f));
    }
}

IEnumerator SpawnItem(float delayStart, float delayDisplay)
{
    Transform item = poolItems.GetRandom();
    Instantiate(item, new Vector3(-21.09f, 5.43f, -17.55f), Quaternion.Euler(new Vector3(0, 127.29f, 0)));       
    archeologistAnimator.SetBool("Action", true);
    sarcophagusAnimator.SetBool("SarcophagusOpen", true);
    yield return new WaitForSeconds(delayDisplay);
    item.transform.DOJump(new Vector3(-5.22f, 2.44f, 40.4f), 2, 1, 5);
    
}

"poolItems" is another script file where I call the functions that allows me to list prefab objects and get one at random. The numbers in vector 3 are coordinates of where I want the object to be instantiated in the scene and where I want it to jump to, respectively.

But it doesn't do anything nor does the console give me an error. Any ideas?

CodePudding user response:

You are changing the transform of your original item, not your newly created instance.

I have not tested this and only changed the needed lines but it should be doing what you want. You could add your directly to the Dotween sequence instead of using the yield return.

private void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        StartCoroutine(SpawnItem(0.5f, 0.8f));
    }
}

private IEnumerator SpawnItem(float delayStart, float delayDisplay)
{
    Transform item = poolItems.GetRandom();
    var itemInstance = Instantiate(item, new Vector3(-21.09f, 5.43f, -17.55f), Quaternion.Euler(new Vector3(0, 127.29f, 0)));       
    archeologistAnimator.SetBool("Action", true);
    sarcophagusAnimator.SetBool("SarcophagusOpen", true);
    yield return new WaitForSeconds(delayDisplay);
    itemInstance.transform.DOJump(new Vector3(-5.22f, 2.44f, 40.4f), 2, 1, 5);
    
}

Two tips: Avoid magic numbers and give more context. It would be hard for someone else to reproduce since your poolItems class is unknown and would need a stand in.

  • Related