Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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