0

I'm pretty sure that this is a rookie mistake which im looking over, but I need some directions. here is my code:

void OnMouseDown()
{
    if(clickedContainers.Contains(transform.parent.name)) //if the containerblock is in "up" state
    {
        //transform.parent.transform.Translate(Vector3.down * 10 * Time.deltaTime);
        newPos = transform.parent.position;
        newPos.y -= 200;
        clickedContainers.Remove(transform.parent.name);
        Application.ExternalCall("Test", "Hello from Unity!" + transform.parent.name);
    }
    else //if the container block is in "down" state = default
    {
        newPos = transform.parent.position;
        newPos.y += 200;
        //transform.parent.position = Vector3.Lerp(transform.position, newPos, Time.deltaTime);
        clickedContainers.Add(transform.parent.name);
    }

    transform.parent.position = Vector3.Lerp(transform.position, newPos, Time.deltaTime);


}

few problems, when using the commented code within the if statements it all works fine; I click an object and it moves the parent object with all child objects up. now I want to smooth this movement instead of it being like its teleporting. So im trying to do this with Lerp, but I'm missing something here. right now it seems like the parent is "teleporting" (so no lerp here) and it is teleporting in all directions, so not only in Y direction.

sorry if this is a really dumb question, but I need some help on this. Cheers

Programmer
  • 121,791
  • 22
  • 236
  • 328
Sten Martens
  • 61
  • 1
  • 6
  • You want to move Gameobject over time....Replace `transform.parent.position = Vector3.Lerp...` with `StartCoroutine(moveToX(transform.parent, newPos, 1.0f));`. See duplicate for the `moveToX` function. – Programmer Sep 26 '17 at 08:49
  • I suggest you don't use `OnMouseDown` even though it has been recommended to you by others in your other question. Use `OnPointerDown` instead. See [this](https://stackoverflow.com/questions/41391708/how-to-detect-click-touch-events-on-ui-and-gameobjects/41392130#41392130) for a complete example of how to use `OnPointerDown` on any type of GameObject. – Programmer Sep 26 '17 at 08:51
  • `OnMouseDown()` will fire once when you pressed the button. So i guess your lerp will not work correctly. – Thalthanas Sep 26 '17 at 08:56

0 Answers0