0
\$\begingroup\$

I'm trying to make my panel moving from one position to another one smoothly. What I get is more faster no matter what I did to make it slower never get slow !! Both Vector3.Lerp and Vector3.MoveTowards did not give me a slow movement. Here is my script:

public GameObject gunsWindow;
private Vector3 a, b;
public bool open; // use inspector
void Start()
{
    a = gunsWindow.transform.position;
    b = new Vector3(100.0f, gunsWindow.transform.position.y, gunsWindow.transform.position.z);
}

void Update()
{

    if(open)
    {
        gunsWindow.transform.position = Vector3.MoveTowards(a, b, Time.deltaTime * 1);
        //weaponPlane.transform.position =  Vector3.Lerp(a, b, Time.deltaTime); // panel shaking
    }
    else
    {
        gunsWindow.transform.position = Vector3.MoveTowards(b, a, Time.deltaTime * 1);
        //weaponPlane.transform.position =  Vector3.Lerp(b, a, Time.deltaTime); // shaking
    }

}
\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

You're using both of these functions incorrectly.

Using MovePosition

gunsWindow.transform.position = Vector3.MoveTowards(a, b, Time.deltaTime * 1); 

Here, the first argument is where we want to start from this frame. So we don't want to pass a here, we want the current value of gunsWindow.transform.position. That gives us the memory of the movement accomplished thus far, that we can add onto with this frame's increment.

Your last argument is the distance to travel, so you can make the motion faster or slower by changing the coefficient 1 to a speed variable.

Using Lerp

weaponPlane.transform.position = Vector3.Lerp(a, b, Time.deltaTime); 

Here, the last argument is your total progress from a to b, not the incremental movement to apply this frame.

So you'll want to set aside a member variable to accumulate this progress from frame to frame (outside your function):

float slideProgress = 0f;

This gives us the memory of movement accomplished thus far. We then increase it each frame in update:

slideProgress = Mathf.Clamp01(slideProgress + Time.deltaTime / slideDuration);

And finally, do the linear interpolation using this progress measure:

weaponPlane.transform.position = Vector3.Lerp(a, b, slideProgress); 

Or, you can use the Lerp function to perform an exponential ease-out as described in this answer.


As general advice, don't neglect to read the docs for the methods you're using, to ensure you're calling them with the correct arguments for your desired outcome.

\$\endgroup\$
2
\$\begingroup\$

To make your panel move slower, you just need to make the last number you pass MoveTowards smaller. Add a public variable at the top, then replace the 1 in your code with it. Something like this:

public GameObject gunsWindow;
public float moveSpeed;
public bool open;

private Vector3 a, b;

void Start()
{
    a = gunsWindow.transform.position;
    b = new Vector3(100.0f, gunsWindow.transform.position.y, gunsWindow.transform.position.z);
}

void Update()
{

    if(open)
    {
        gunsWindow.transform.position = Vector3.MoveTowards(gunsWindow.transform.position, b, Time.deltaTime * moveSpeed);
    }
    else
    {
        gunsWindow.transform.position = Vector3.MoveTowards(gunsWindow.transform.position, a, Time.deltaTime * moveSpeed);
    }

}

Now, using the inspector, you should make moveSpeed very small. This number is how many units you want to move per frame, so if it's 0.05 (1/20), your panel will be moving at three units per second. That said, your panel is moving 100 units, so maybe you want something more like 30 units per second, or 0.5.

\$\endgroup\$
0

You must log in to answer this question.

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