Search Unity

Click to Spawn Help

Discussion in 'Scripting' started by KudoJimmy, Dec 3, 2018.

  1. KudoJimmy

    KudoJimmy

    Joined:
    Sep 6, 2017
    Posts:
    27
    As the title say, I want to create a 2D Texture of my game map. Then, when a player click somewhere on the map, he would get teleported to that same location. I know I will have to convert 2D points to 3D transform, but I can not think how to do it.. can you please help me? Also, I think this same will be applied to place marker on the map?

    Thanks.
     
  2. KudoJimmy

    KudoJimmy

    Joined:
    Sep 6, 2017
    Posts:
    27
    Bump..
     
  3. First of all, please don't bump. We read your post.
    Second: spawn is not the right wording, you're teleporting or moving objects
    Third:
    here is what you need to know and what you need to do
    - you have the Image contains the minimap texture and it has a size X and Y
    - you have a game space with coordinate system, but you know the boundaries and the sizes
    - you should listen to click on the image
    - upon click you need to read the mouse position on the image (raycast, coordinates)
    - you should calculate the relative position of the click to the image origin
    - you should calculate this relative position on the game space
    - you should move your player to the position if allowed
    - be careful if it's a 3D world, you need to mind the 3rd dimension as well, you don't want to teleport your player inside a mountain or something (you can also solve this with raycast)
     
    KudoJimmy likes this.
  4. KudoJimmy

    KudoJimmy

    Joined:
    Sep 6, 2017
    Posts:
    27
    Thank you for your guidance. Now I will try to achieve this using steps you mentioned. And I just need to ask you one thing. My game will be indeed a 3D game and it will have 3D terrain which may/may not be flat at all coordinates. So I will have to use Raycast. Can you give me example for this snippet?

    PS, I said spawn, because I could implement this thing to spawn the player as well. something like ROE if you know about it.
     
  5. KudoJimmy

    KudoJimmy

    Joined:
    Sep 6, 2017
    Posts:
    27
    Okay, I followed your steps and created a system where a marker is displayed on the map when user clicks on it. There are 2 things I still want to know:

    -How to restrict the click inside a RAW Image of size 500x500 which is my Map shown on the screen.
    -How to convert that Pixel coordinates to real world 3D coordinates to set player's position there? I am using Input.MousePosition to get the click's position.


    EDIT:
    Answer to First Problem: Here
     
    Last edited: Dec 8, 2018
  6. KudoJimmy

    KudoJimmy

    Joined:
    Sep 6, 2017
    Posts:
    27
    I tried using multiple ways.. Raycast2D using colliders and RigidBody2D but notthing works. If I use .name property to check if Raycast hit the Map game object, it gives runtime error, that Object reference not set to an instance of an object. And On that scene, there will be only 1 collider, So I thought may be using,
    Code (CSharp):
    1. if(hit.collider != null)
    would provide me the result that I want. But, I click where ever on the screen, this returns true. Even though I have set the collider only on the Map's Raw Image... Can someone help me??
     
  7. KudoJimmy

    KudoJimmy

    Joined:
    Sep 6, 2017
    Posts:
    27
    Anyone?
     
  8. AixS

    AixS

    Joined:
    Apr 9, 2018
    Posts:
    7
    Set a tag to the map object and check if the raycast hits it.
    Code (CSharp):
    1. if(hit.collider.tag == "MapTag") {}
     
  9. KudoJimmy

    KudoJimmy

    Joined:
    Sep 6, 2017
    Posts:
    27
    Nope... its not working. Its not able to locate the object. I tried using gameobject.find but its still not able to find it. It says Object Reference is not set to an instance of an object. I have set up tags properly and checked several times. I even tried removing the BoxCollider2D from inspector and add it runtime on Awake(), but then it doesnt just do anything. I added Debug.Log() if it enters Map collider and even if it doesnt, it should log, that not entered. But nothing gets logged in console.

    Can this be because I'm using Raw Image to display Map? I dont think so that can be the problem..
     
  10. KudoJimmy

    KudoJimmy

    Joined:
    Sep 6, 2017
    Posts:
    27
    Okay, So I think this is progressing a little bit. Collider and Object are now found properly. Even Collider is set as trigger. STILL, OnTriggerEnter2D is not getting called. At All. I even added a RigidBody2D to the map but it still is not working.
    Here is my debugging code:

    Code (csharp):
    1.  
    2. void Start()
    3. {
    4.        MainMap = GameObject.Find("Map");
    5.         if (MainMap)
    6.             Debug.Log("Map Object Found! "+MainMap.gameObject.name);
    7.  
    8.         bc = MainMap.GetComponent<BoxCollider2D>();
    9.         if (bc)
    10.         {
    11.             Debug.Log("Collider also loaded!! ");
    12.  
    13.             Debug.Log(bc.size.x + " , " + bc.size.y);
    14.             bc.size = new Vector2(500f, 500f);
    15.             Debug.Log(bc.size.x + " , " + bc.size.y);
    16.  
    17.             Debug.Log(""+bc.isTrigger);
    18.             bc.isTrigger = true;
    19.             Debug.Log("" + bc.isTrigger);
    20.        }
    21. }
    22.  
    All the Debug.Log are showing correct information.

    And this is OnTriggerEnter2D:

    Code (csharp):
    1.  
    2. public void OnTriggerEnter2D(Collider2D collider2D)    {
    3.  
    4.  
    5.         Debug.Log("Object: " + collider2D.gameObject.name);
    6.        
    7.             Debug.Log("Click Registered... Collider to: "+ collider2D.gameObject.name);
    8.  
    9.             if (collider2D.gameObject == MainMap)
    10.                 Debug.Log("CLICKED");
    11.             else
    12.                 Debug.Log("NULL");
    13.     }
    14.  
    Even the first Debug.Log in this function doesnt get called, that means, function is not getting called at all...
     
  11. KudoJimmy

    KudoJimmy

    Joined:
    Sep 6, 2017
    Posts:
    27
    OKAY.. SO I HAVE FINALLY SOLVED the problem.

    I searched Unity Docs and found out about Graphics RayCaster. For UI Elements, You have to use Graphics Raycaster. It is now working and I'm able to identify if click was on Map or not. Thanks everyone who tried to help me. I believe it was RawImage what was causing Collider problem. Only Sprites(Image) can call TriggerEnter may be. Havent tried it but thought to mention this for future, if someone is also having same problem... Cheers...


    Okay so my second problem now.
    How can I convert the coordinates received from Input.MousePosition on the map, to real coordinates of 3D world... Can someone help me solve this?
     
  12. AixS

    AixS

    Joined:
    Apr 9, 2018
    Posts:
    7
    Here's an idea, but i am not sure if it can pull off what you are looking for. Basically getting the mouse click position relative to the image, then dividing it with the image size and multiplying by the world size.
    Code (CSharp):
    1.  
    2. worldposY = mouseClickY / mapHeight * worldHeight;
    3. worldposX = mouseClickX / mapWidth * worldWidth;
    4.  
    Getting the mouse click pos relative to the image is something I can't come out with on the spot, but shouldn't be too hard.