0
\$\begingroup\$

I want the gameobject to go down (position.y) after pressing a key instead of pressing the key repeatedly. So in my current code the object goes down(0.05f) each time I press a key. But I want it to go down (1.2) with a smooth animation when I press a key.

Vector2 Pos;

void Update ()
{
 Pos = transform.position;

 if(Input.anyKeyDown)
 {   
   ChangePosY();
 }
 transform.position = Pos;
}

void ChangePosY()
{
 if (Pos.y >= 1.2)
 {
  Pos.y -= 0.05f;
 }
}
\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

You may use smooth translation with Vector2.Lerp(). One new variable isKeyPressed to detect key pressed which is changed when target position condition meets, until it translates towards target position with 1.2 unit in y-axis/downward with 0.1 frequency. See Lerp() Ref.

Vector2 Pos;
bool isKeyPressed = false;

void Update()
{
    Pos = transform.position;

    if (Input.anyKeyDown)
    {
        isKeyPressed = true;
    }

    if (isKeyPressed)
    {
        ChangePosY();
    }
    transform.position = Pos;
}

void ChangePosY()
{
    if (Pos.y >= 1.2f)
    {
        Pos = Vector2.Lerp(Pos, new Vector2(Pos.x, Pos.y - 1.2f), 0.1f);
    }
    else
    {
        isKeyPressed = false;
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ How can I do this with an onscreen button? \$\endgroup\$
    – kp21
    Jul 15, 2016 at 5:23
  • \$\begingroup\$ unity3d.com/learn/tutorials/topics/user-interface-ui/ui-button . Write a public method in the same script to call from button which makes isKeyPressed true. \$\endgroup\$ Jul 15, 2016 at 5:39
  • \$\begingroup\$ I apologize I didn't mention this but instead me having to get the y position can I just increase the y position by a float. \$\endgroup\$
    – kp21
    Jul 15, 2016 at 5:58
  • \$\begingroup\$ You may keep a public or serialize-able private variable for the float representing y position. That way it doesn't have to a fixed number. You can change it on inspector as well as from other script. Private type will need to be accessed from a public method instead of direct access. \$\endgroup\$ Jul 15, 2016 at 6:24
1
\$\begingroup\$

I'd do this with a coroutine:

// Keep track of the current coroutine so we don't stack up multiple.
Coroutine translation;

void Update()
{
     if(Input.anyKeyDown)
     {
         translation = StartCoroutine(
                           TranslateTo(
                              /* Put the final position you want here */,
                              1.0f
                           )
                       ); 
     }
}

IEnumerator TranslateTo(Vector3 destination, float duration)
{
     // Stop previous coroutine if the last motion didn't finish yet.
     if(translation != null)
         StopCoroutine(translation);


     Vector3 initialPosition = transform.position;

     // Shifting the start back one frame so we move a little immediately.
     float startTime = Time.time - Time.deltaTime;
     float endTime = startTime + duration;

     while(Time.time < endTime)
     {
         float t = (Time.time - startTime)/duration;
         transform.position = Vector3.Lerp(initialPosition, destination, t);

         // Wait one frame, then resume here.
         yield return null;
     }

     // Ensure we finish exactly at the destination.
     transform.position = destination;

     // Clear coroutine cache to signal that we're done.
     translation = null;
}
\$\endgroup\$

You must log in to answer this question.

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