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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question 2D shadow for all images in same time

Discussion in 'Scripting' started by julien83franceschi, Jan 18, 2023.

  1. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    187
    Hello to all,

    The shadow effect appears when you click on the pentamino and disappears when you release it.

    Code (CSharp):
    1. public class dragImage : MonoBehaviour, IDragHandler
    2. {
    3.  
    4.     public Image image;
    5.  
    6.     private void Start()
    7.     {
    8.      this.GetComponent<Image>().alphaHitTestMinimumThreshold = 0.001f;
    9.     }
    10.  
    11.     public void OnDrag(PointerEventData eventData)
    12.     {
    13.         transform.position += (Vector3)eventData.delta;
    14.         transform.SetAsLastSibling();
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         if (Input.GetMouseButtonDown(0))
    20.         {
    21.             image.GetComponent<TrueShadow>().OffsetDistance = 12f;
    22.         }
    23.         if (Input.GetMouseButtonUp(0))
    24.         {
    25.             image.GetComponent<TrueShadow>().OffsetDistance = 0f;
    26.         }
    27.     }
    28. }
    The problem is that there are several pentominoes and when you click on a pentomino there is good a shadow effect. But the shadow effect appears on all the pentominoes in the scene (even when not clicked on).

    See the video above :
    https://youtube.com/shorts/nrCM3yaqzek?feature=share

    Your code is welcome.

    Thanks for your help.

    A+
     
    Last edited: Jan 19, 2023
  2. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    233
    Your video is set to private, we can't see it
     
  3. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    488
    All of your pentominoes are just checking in their Update method that the mouse button has been clicked or released, and setting their own image offset based on that. There is nothing there to check if it is this specific game object that is being clicked on. You would need to add some extra logic to check that you are actually clicking on this game object and if so, then only then change the offset for the shadow effect.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,938
    BABIA_GameStudio likes this.
  5. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    187
    Hi all,

    Here is a video wich good resume my problem:


    I have tried this code:
    Code (CSharp):
    1.  if (Input.GetMouseButtonDown(0))
    2. {
    3.    Destroy(image.GetComponent<Trudow>());
    4. }
    It does remove the shadow effect of the pentamino (bottom left image).
    But when you click on this last, it creates a shadow effect only with the pentamino on the top right (see video).



    I arrive to destroy the shadow but I don't know how to recreate it.

    Thanks for your help.

    A+
     
    Last edited: Jan 19, 2023
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,938
    As we've already mentioned when you click the code is running on all your pentaminos, as there is no check in place to determine which one in particular is being clicked. You need to adjust your code to only affect the one being clicked.
     
  7. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    187
    Hi,

    You have understood my problem well when you say:
    I deleted the component that makes the shadow of image (the one that makes the shadow of the pentamino at the bottom left of the scene).
    The line of code is the following:
    Code (CSharp):
    1.  if (Input.GetMouseButtonDown(0))
    2. {
    3.    Destroy(image.GetComponent<TrueShadow>());
    4. }
    So there is only one left. The effect is successful (with the right top pentamino).

    The problem is that the shadow has been removed.
    And I don't know how to recreate it (the shadow of the bottom left pentamino).
    Your code is welcome.

    Thanks for your help.
     
    Last edited: Jan 20, 2023
  8. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    233