- Home /
Playing a button pressed animation on a box
I have a boxin my world (made in onGUI) and was wondering if it was possible to do a have a simular animation as the button press be played on the box?
I have a spell that is cast when the key 1 is pressed. I have a box displaying the name and hotkey and also if the player is in range or not. I want to have an animation that shows when the spell button is being pressed. Is this possible or is it just a stupid idea?
I would recommend not using OnGUI() anymore, it is deprecated. The newer Unity UI system replaced it and has a lot better functionality. In Unity UI you model a button with a GameObject with a Button script on it, which automatically handles being pressed.
I have heard people saying this, but i feel like the new system is extremely bad at different sizes of screens. If i place a button in the bottom of the screen on my computer it is not even in the screen on my laptop.
Well, designing the UI for various resolutions is difficult, but once you understand anchoring and the CanvasScaler component, it becomes a lot easier. And with OnGUI() you still have the same problem, but there you have to change code to move the UI, and calculate with resolution and ratios...
Check out this Unity manual about how to design for various resolutions, it is not perfect, but it explains the basics well.
And also i find it hard to eg. have a text be displayed over an enemy or the player.
That's actually really easy, just add this script to the UI element you want to move:
public class AnchorIn3D : $$anonymous$$onoBehaviour {
[SerializeField] private RectTransform ownRectTransform = null;
[SerializeField] private Transform target = null;
public void SetTarget (Transform value) { target = value; }
[SerializeField] private Vector3 offset = Vector3.zero;
public void SetOffset (Vector3 value) { offset = value; }
private void Awake () {
if (ownRectTransform == null) { ownRectTransform = GetComponent<RectTransform>(); }
}
private void Update () {
if (ownRectTransform != null && target != null) {
ownRectTransform.position = Camera.main.WorldSpaceToScreen(target.position + offset);
}
}
}
Answer by rerenzel · Feb 28, 2018 at 05:02 PM
yes it is if(Input.GetButtonDown(yourButton) { // this is where your animation starts }