Good day. Slowly learn Unity3D and develop a simple 2D game. When you press the pause button (call the Pause() method), a darkened background pops up in the interface, and when you click on the background, the game continues. I decided to implement this task like this: background as a button. So that the rest of the elements outside the pause under the button were available, I decided to hang on the RigidBody2D button and move / move the background of the pause when clicking. Everything seemed to work out, but there were questions.

 public Rigidbody2D back_pause = new Rigidbody2D(); bool pause = false; void Start () { //при старте сцены сразу отодвигаю фон паузы back_pause.transform.position = Vector2.left * 3000f; } public void Pause(){ if(pause == false){ //клик по кнопке "пауза" pause = true; back_pause.transform.position = Vector2.right * 3f; }else{ //клик по фону, отмена паузы pause = false; back_pause.transform.position = Vector2.left * 3000f; } } 

As can be seen from the code, we move to the right by 3f, to the left by 3000f, the difference is a thousand (!) Times, and in the game everything works as it should. It is not clear ... Why is this happening? Apparently, I do not correctly understand the principle of movement and vectors ... Maybe something can be done differently?

  • So far I have no answer to the question about left / right, but to show / remove the background: it’s easier to use enable = true/false just to display an object on the stage or hide it rather than move the background somewhere to the side - Alexey Shimansky
  • I worked on this option first. When enable = false button does not disappear, but simply disables its functionality (it is impossible to click) and becomes “depressed”, but does not disappear ... - Pollux
  • Maybe I SetActive confused with SetActive )) here, look ... you can just arrange the background to full screen ru.stackoverflow.com/a/537964/191482 ........ at this background, if you like to click click - you can use eventTrigger youtu.be/3NBYqPAA5Es?t=189 and that's it. - Alexey Shimansky
  • @ Alexey Shimansky, thanks to eventTrigger , I didn’t know about it!) I understand you mean the background as Image, but it’s also like the button as a layer, and if you make it invisible .CrossFadeAlpha(0, 0, false); , through it it will be impossible to click on the elements of the game ... - Pollux
  • Why do you need to click on the elements of the game during the pause? After all, a pause is done in order not to click on them ...... and it is better not to do invisible (or rather not active) CrossFadeAlpha and SetActive(false) ... - Alexey Shimansky

1 answer 1

To create menus, use the correctly configured Canvas + UI objects. Using GUI objects I do not advise. Slow and outdated technology.

for animations I advise you to use LeanTween Assets. Animation turns one pleasure + the library itself is very fast in work and also free. To use more beautiful animations (non-linear function), use the SetEase method.