Search Unity

How to load and then hide a random image from multiple ones on canvas. Urgent (please respond)

Discussion in 'General Discussion' started by Zerkalo108, Jun 1, 2022.

  1. Zerkalo108

    Zerkalo108

    Joined:
    Mar 27, 2020
    Posts:
    3
    Hi there! Pretty new to Unity so please bear with me!
    The project I must do is very simple: I have different simple 3d objects (spheres,stars, cubes) which act like buttons. OnTap or OnMouseButtonDown I must zoom in on the object I tapped (or make it bigger as if to open up) and load a random Jpg from a list of Jpegs. It should look as if the image was in the object or atleast the canvas should fade in very close to it's surface.

    What I have done so far: I have succesfully set up and hooked up the models to be reactive to tap/mouseclick. Now I must understand how to make only one of the thirty images appear within the canvas before I close it.
    I have added some images and converted them to 2d/UI sprites and put the images on the canvas.

    -How to call each of the images within the canvas (let's say I've named them image1, image2 ... image30)
    -How to make the zoom in on the object before the canvas appears- Should I use a camera zoom to each object or rather make the object come closer to the camera (whichever is easier to configure)

    Thanks so much for any advice in advance!
    I would usually resort to tutorials and documentaion but I have only a day and a half till this is due!
     
  2. CodeKiwi

    CodeKiwi

    Joined:
    Oct 27, 2016
    Posts:
    119
    This might help as a starting point. Might be worth checking Cinemachine if you have the time. This kind of question should also probably be posted in Scripting.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. [RequireComponent(typeof(Camera))]
    5. public class Test : MonoBehaviour
    6. {
    7.     public Image image;
    8.     public Sprite[] sprites;
    9.     public Transform moveToTarget;
    10.     public float maxDistanceDelta = 1;
    11.     public float stopDistance = 2;
    12.  
    13.     void Start()
    14.     {
    15.         image.sprite = sprites[Random.Range(0, sprites.Length)];
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         if (Vector3.Distance(moveToTarget.position, transform.position) > stopDistance)
    21.         {
    22.             transform.position = Vector3.MoveTowards(transform.position, moveToTarget.position, maxDistanceDelta * Time.deltaTime);
    23.         }
    24.         transform.LookAt(moveToTarget);
    25.     }
    26. }