Search Unity

GUI Texture Movement

Discussion in 'Immediate Mode GUI (IMGUI)' started by TechnoObi, Oct 17, 2014.

  1. TechnoObi

    TechnoObi

    Joined:
    Nov 30, 2013
    Posts:
    82
    Hello,

    I have "Rect1", "Rect2" and float "speed". Whats the best way, that my texture moves from "Rect1" to "Rect2" with the speed from "speed"?
     
  2. Avalion

    Avalion

    Joined:
    May 2, 2012
    Posts:
    34
    You should use the Mathf.Lerp function.

    To use this, you'll have to create a variable named "lerp" or whatever you want that will be incremented to Time.deltaTime / speed every frame (so it vary between 0 and 1 in speed time).

    and, your OnGUI function will get a line like
    Rect position = new Rect(Mathf.Lerp(Rect1.x, Rect2.x, lerp), Mathf.Lerp(Rect1.y, Rect2.y, lerp), Mathf.Lerp(Rect1.width, Rect2.width, lerp), Mathf.Lerp(Rect1.height, Rect2.height, lerp))

    You can create a function to do this with two Rect and a float.

    Don't forget to stop the movement if lerp > 1 !

    For more informations, the Mathf.Lerp function will only return something like this : value1 + (value2 - value1) * lerp

    Hope this helps

    Avalion