Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Click on an Entity

Discussion in 'Project Tiny' started by damogi, Feb 12, 2019.

  1. damogi

    damogi

    Joined:
    Feb 10, 2019
    Posts:
    5
    Hi, I´m trying to detect when I click on a sprite2D, hoe can I to do that in Tiny Mode.
     
  2. Rupture13

    Rupture13

    Joined:
    Apr 12, 2016
    Posts:
    131
    1. Firstly, for a sprite2D to be able to be clicked, it needs the Sprite2DRendererHitBox2D component
    2. Then you have to detect your click or mouseDown
    3. Then you have to get the world-space point of clicking
    4. Then you have to raycast (or equivalent) to your world-space point of clicking
    5. Maybe do some additional checks to see if the entity you hit is the one you want to hit
    In code (assuming you have the Sprite2DRendererHitBox2D component set on your entity), it would look something like this:

    Code (CSharp):
    1. if (ut.Core2D.Input.getMouseButtonDown(0)) {
    2.     let mousePos = ut.Core2D.Input.getWorldInputPosition(this.world);
    3.     let hit = ut.HitBox2D.HitBox2DService.hitTest(this.world, mousePos, this.world.GetEntityByName("Camera");
    4.  
    5.     if(!hit.entityHit.isNone() && this.world.getEntityName(hit.entityHit) == "MyEntity") {
    6.         //Do something with the hit entity
    7.     }
    8. }
    Note that for better code, you probably want to store a reference to your camera entity instead of finding it by name. Same goes for identifying your hit entity by name.
     
    vincismurf likes this.
  3. damogi

    damogi

    Joined:
    Feb 10, 2019
    Posts:
    5
    Thank you so much
     
  4. Revolter

    Revolter

    Joined:
    Mar 15, 2014
    Posts:
    216
    What about MouseInteraction, does it work only with UI?
     
    Zoelovezle likes this.
  5. Revolter

    Revolter

    Joined:
    Mar 15, 2014
    Posts:
    216
    Also I tried to usePhysics2D.Physics2DService.hitTest, but for some reason the hit is always null.
    Code (CSharp):
    1. if (ut.Core2D.Input.getMouseButtonDown(0)) {
    2.                 let mousePos = ut.Core2D.Input.getWorldInputPosition(this.world);
    3.                 let mousePos2D = new Vector2(mousePos.x, mousePos.y);
    4.                 let hit = ut.Physics2D.Physics2DService.hitTest(this.world, mousePos2D);
    5.                 console.log(mousePos2D);
    6.            
    7.                 if(!hit.isNone)
    8.                 {
    9.                     console.log("Hit");
    10.                 }
    11.             }
    Edit: The hitbox example above works, but I need to detect clicks on an Entity with no sprites
     
    Zoelovezle likes this.
  6. Zoelovezle

    Zoelovezle

    Joined:
    Aug 7, 2015
    Posts:
    54
    You could just drop down the alpha of sprite to 0 in sprite renderer :)
     
  7. Revolter

    Revolter

    Joined:
    Mar 15, 2014
    Posts:
    216
    upload_2019-2-17_18-9-52.png
     
  8. Revolter

    Revolter

    Joined:
    Mar 15, 2014
    Posts:
    216
    Nevermind, solved the mistery. hit.isNone is a method, missing the brackets there ()