#Lerp Movement + Input

1 messages · Page 1 of 1 (latest)

opal fossil
#

Hey, I am working on character movement for my game and I am trying to set up a Point A to Point B movement system using Lerping. The desired outcome is for the character (small white dot) to move from circle to circle in about a quarter or half a second, however, it instead teleports rather than shiftly moves and I don't understand why since I used a Youtube video as a reference to Lerping
Link to YT Video: https://youtu.be/IymDGkzwJts?si=h26aQHbNeI2Dinet

Here is my code that I have worked on (ik it is messy)

Lerping is one of the foundational things you need to know about in video game development! There are so many use cases for lerping it's impossible to name them all! Almost any time you want to rotate, move, or change a value over time, lerping is an option.

💸 Ongoing sales 💸
⚫ See all active asset sales on the Asset Store: https://assetstore.u...

▶ Play video
#
public class PlayerMovement : Player
{
    [SerializeField] private Rigidbody2D hitbox;

    private Vector2 startingPos = new Vector2(0, 3);
    int current = 0;
    int next = 1;
    int previous = 3;
    float time = 0f;
    //private ListOfPositions theRoses = new ListOfPositions();

    void Start()
    {
        if (hitbox.position != startingPos)
        {
            hitbox.position = startingPos;
        }
        if (roses.positions.Count == 0)
        {
            roses.AddPositions();
        }
    }

    // Update is called once per frame
    void Update()
    {
        PlayerInput();
    }

    public void PlayerInput()
    {
        if (Input.GetKeyDown(KeyCode.D))
        {
            StartCoroutine(MovePlayerToNextPosition());
        }

        if (Input.GetKeyDown(KeyCode.A))
        {
            StartCoroutine(MovePlayerToPreviousPosition());
        }
    }

    public IEnumerator MovePlayerToNextPosition()
    {
        while (time < 1)
        {
            hitbox.position = Vector3.Lerp(roses.positions[current].position, roses.positions[next].position, time);
            time += Time.deltaTime * 0.25f;
        }
        time = 0;

        next++;
        current++;

        if (next > roses.positions.Count - 1)
        {
            next = 0;
        }
        if (current > roses.positions.Count - 1)
        {
            current = 0;
        }
        yield return null;
    }

    public IEnumerator MovePlayerToPreviousPosition()
    {
        while (time < 1)
        {
            hitbox.position = Vector3.Lerp(roses.positions[current].position, roses.positions[previous].position, time);
            time += Time.deltaTime * 0.25f;
        }
        time = 0;

        previous--;
        current--;

        if (previous < 0)
        {
            previous = 3;
        }
        if (current < 0)
        {
            current = 3;
        }
        yield return null;
    }
}
slender zealot
#

The while loops you have are doing nothing btw

opal fossil
#

was one of the things I took directly from the video

slender zealot
#

the entire contents of those loops will run to completion in one frame

#

as you don't yield inside of them

#

They yield inside of the loops

opal fossil
#

checking something

#

realizes I put the yields outside

#

brb

#

adding StopCoroutines rq since it is acting weirder now

#

alright fixed, it turns out I also needed to move the additional logic outside of the While Loop as well (I'll also need to adjust the speed to my liking however but that is an easy fix)

#

gonna also try to make it more accurate on where it stops (it sometimes stops off center)