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. Dismiss Notice

Question Smoothly changing size large to small

Discussion in 'Scripting' started by Kindacringegamemaker, Jul 16, 2023.

  1. Kindacringegamemaker

    Kindacringegamemaker

    Joined:
    Jul 8, 2023
    Posts:
    10
    Hey guys, I'm pretty new to unity and this is my first post, I'm trying to make a title screen for my mobile game, but I'm not sure how to smoothly size slightly up and then size down to 0,0 for an image, can you help me?
    Because I have the button click event down, I'm just unsure on how to size it down.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Drawing;
    4. using Unity.VisualScripting;
    5. using UnityEngine;
    6. using UnityEngine.EventSystems;
    7. using UnityEngine.UI;
    8. using UnityEngine.UIElements;
    9.  
    10. public class StartGame : MonoBehaviour,IPointerDownHandler
    11. {
    12.  
    13.     [SerializeField] UnityEngine.UIElements.Image image;
    14.     public  bool SizeDown = false;
    15.    public bool ButtonPressed = false;
    16.  
    17.     void Update()
    18.     {
    19.         if (ButtonPressed && SizeDown == false)
    20.         {
    21.            
    22.             SizeDown = true;
    23.  
    24.            
    25.  
    26.  
    27.          
    28.            
    29.         }
    30.    
    31.     }
    32.     void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
    33.     {
    34.  
    35.  
    36.         ButtonPressed = true;
    37.        
    38.     }
    39.  
    40. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    I suggest looking into a tweening library. It will make stuff like this super simple. Leantween and DoTween are both free on the asset store. That tends to be my preference on things like this.
     
  3. Kindacringegamemaker

    Kindacringegamemaker

    Joined:
    Jul 8, 2023
    Posts:
    10
    thanks I'll try that!
     
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,913
    If you want to learn to program, your thing is doable in code. Step one is figuring out how to change the size of your image at all. Look it up then, in your first buttonPressed block, add that one line -- maybe use 0.5 for the size. Test if pressing a button snaps the image to half size.

    If instead of 0.5, you could had some variable that went down to 0, then back up, you could use that. So step two is to forget the image for now and to figure out how to make a value go down to 0 and so on. Try making it public so you can easily see it change. You'll probably need an extra variable to remember whether it's going down, or not changing, or... . Doing that isn't super easy, but it's like a 2nd week exercise in programming.

    Then put them together. Many problems can be broken into part that way.