adding a delayed moved to an object not working.

Hey

i am tying myself in knots trying to work with this, specifically (Vector3.Lerp) and a pause function (CoRoutine). The problem here is, if i substitue the “nextMove” boolean and “StartCoRoutine” pieces for standard “input.GetKeydown” the object lerps as expected.

the expected movement is that the key would be pressed the object would “Lerp” to the end position with a nice animated jump (justjump animation) in between. Subsequent presses of a key input also result in the expected behaviour.

rather than be player controlled, i wanted the same movement to occur every X no of seconds.

so adding the “nextmove” boolean in place of the keypress and then then adding the StartCoRoutine when the Lerp is epected to finish produces a result which appears

Jump then Move then Wait rather than
Jump and Move then Wait…

JustJump is just a refernce to a boolean variable attached to the animatorto call the Jump animation. I ave fone through it time and time again and cannot seem to get the right result.

private void Update()
    {
        moveEnemy();
    }
   
void moveEnemy()
    {
        if (nextMove)
        {
            if (perc == 1f)
            {
                lerpTime = 1f;
                currentLerpTime = 0f;
                firstInput = true;
                justJump = true;
                lerpMovementCompleted = false;
            }
        }

        startPos = gameObject.transform.position;

        if (nextMove && startPos == endPos)
        {
            nextMove = false;

            endPos = new Vector3(startPos.x, startPos.y, startPos.z + 15);
        }

        if (!lerpMovementCompleted)
        {
            if (firstInput)
            {
                currentLerpTime += Time.deltaTime * 2.5F;

                if (currentLerpTime > lerpTime)
                {
                    currentLerpTime = lerpTime;
                }

                perc = currentLerpTime / lerpTime;

                gameObject.transform.position = Vector3.Lerp(startPos, endPos, perc);

                if (perc > 0.85f)
                {
                    perc = 1f;

                    justJump = false;

                    lerpMovementCompleted = true;

                    StartCoroutine(EnemyPause());
                }
            }
        }
    }

    IEnumerator EnemyPause()
    {
        yield return new WaitForSeconds(2F);

        nextMove = true;
    }

You could just do it all in a coroutine;

using System.Collections;
using UnityEngine;

public class AutoLerpMove : MonoBehaviour {

    public void Start()
    {
        StartCoroutine(LerpMove(2f));
    }

    private IEnumerator LerpMove(float LerpTime)
    {
        var lerpTime = LerpTime;
       
        while (true)
        {
            var startPos = transform.position;
            var endPos = new Vector3(startPos.x, startPos.y, startPos.z + 15);
            var currentLerpTime = 0f;

            while (currentLerpTime <= lerpTime)
            {
                currentLerpTime += Time.deltaTime;
                transform.position = Vector3.Lerp(startPos, endPos, currentLerpTime / lerpTime);
                yield return null;
            }
            yield return new WaitForSeconds(2f);
        }     
    }
}
1 Like

I’m tired, but I noticed that you set the startpos every frame. That’s not how it should work for lerp. You should set the startpos at the beginning and reuse the variable for the Lerp calls. I also wonder about > .85f – I reckon that might mean you are ending the Lerp sequence, so to speak , prematurely?
And you got another optional response while I was writing. Also a good plan, I was thinking of that, too (but probably wouldn’t have written it out ;)).

1 Like

Really appreciate the reply. i like that idea and can kick myself for not trying it.

Thanks for the reply, even though you’re tired. These forums are built on people like you. When i didn’t put that piece in the jump took ages. i also see what you are saying about the “startpos” i’ll update along with the comment from WarmedXMints :wink:

Cool :slight_smile: Good luck. Hope it works out nicely for ya.

You’re welcome. I needed something to do while I was waiting for the kettle to boil :smile:

actually chaps, utilising both sets of comments, and by economising my code from “WarmxMints” which shows how rusty i am with C# and by simply moving the “startpos” parameter from “methos5K”, i have managed to get what i want working.

i have found 2 things here that have been really difficult for me to get my head around and that is using the animation with a Lerp and the IEnumerator and how to use the yield command properly.

I am glad that you got it working. :slight_smile: