I am attempting to create a GUI window that slides out of the bottom of the screen when the player presses the Q key, i have gotten the window to pop in and out with the bellow code but i can’t get it to smoothly slide in and out. You can copy and paste the code bellow onto an empty game object and you’ll see what i mean.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class InventoryGUI : MonoBehaviour
{
public GUISkin iSkin;
public float buttonWidth = 40.0f;
public float buttonHeigth = 40.0f;
public float closedButtonWidth = 20.0f;
public float closedButtonHeight = 20.0f;
private float _Offset = 10.0f;
private float _MoveSpeed = 0.0001f;
private bool _IsShown = false;
private float _InventoryWindowHeight = 128.0f;
private float _InventoryWindowWidth;
private float _InventoryWindowDown;
private float _InventoryWindowUp;
private bool _DisplayInventoryWindow = false;
private const int _InventoryWindowID = 1;
private Rect _InventoryWindowRect;
void Awake()
{
_InventoryWindowWidth = Screen.width -( _Offset * 2.0f);
_InventoryWindowDown = Screen.height - 5.0f;
_InventoryWindowUp = Screen.height - (_InventoryWindowHeight + _Offset);
}
private void Start()
{
_InventoryWindowRect = new Rect( _Offset, _InventoryWindowDown, _InventoryWindowWidth, _InventoryWindowHeight);
}
private void Update()
{
if (Input.GetKey(KeyCode.Q))
_IsShown = true;
else
_IsShown = false;
if (_IsShown)
{
_InventoryWindowRect.y = Mathf.MoveTowards(_InventoryWindowDown,_InventoryWindowUp,_MoveSpeed);
}
else
{
_InventoryWindowRect.y = Mathf.MoveTowards(_InventoryWindowUp,_InventoryWindowDown,_MoveSpeed);
}
}
void OnGUI()
{
_InventoryWindowRect = GUI.Window(_InventoryWindowID, _InventoryWindowRect, InventoryWindow, "Inventory");
}
private void InventoryWindow(int iD)
{
//GUI.DragWindow( new Rect(0,0,_InventoryWindowWidth,16));
}
}
i would appreciate any help you could offer.
If i do that the box doesn't move at all.
– SirMalarkeyI had to change movespeed up to 20 or so - it's in pixels per frame. .y is also an integer, and if you move it by less than 1 pixel at a time, it's not going to move at all. I also multiplied movespeed by Time.deltaTime to keep it consistent, and then I had to knock it up to 1000 (that is, 1000 pixels per second movement, about 1/5 a second to pop up)
– Loius