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

Procedural Generated Mesh Dissapears when Mouse Moves Over

Discussion in 'Scripting' started by footballhono52, Jun 12, 2016.

  1. footballhono52

    footballhono52

    Joined:
    Dec 3, 2015
    Posts:
    16
    I've been following a youtube series on making a procedurally generated tile map. Everything has gone great so far until now. The videos were made in 2013 so I had to update my code to Unity 5 in order to work.

    My problem is I generate the mesh with the texture on it. When I move the mouse over the mesh at all in game mode it dissapears. What I'm trying to do is have a 1x1 cube represent where the mouse is on the tile map.

    I'm not sure what the problem could be or why it just dissapears.



    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [RequireComponent(typeof(TileMap))]
    6. public class TileMapMouse : MonoBehaviour {
    7.  
    8.     Collider collider;
    9.     Renderer renderer;
    10.     TileMap _TileMap;
    11.  
    12.     Vector3 currentTileCoord;
    13.  
    14.     public Transform selectionCube;
    15.  
    16.     void Start()
    17.     {
    18.         collider = GetComponent<Collider>();
    19.         renderer = GetComponent<Renderer>();
    20.         _TileMap = GetComponent<TileMap>();
    21.         selectionCube = GetComponent<Transform>();
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    27.         RaycastHit hitInfo;
    28.  
    29.         if( collider.Raycast( ray, out hitInfo, Mathf.Infinity))
    30.         {
    31.             int x = Mathf.FloorToInt( hitInfo.point.x / _TileMap.tileSize);
    32.             int z = Mathf.FloorToInt(hitInfo.point.z / _TileMap.tileSize);
    33.  
    34.             currentTileCoord.x = x;
    35.             currentTileCoord.z = z;
    36.  
    37.             selectionCube.transform.position = currentTileCoord * 5f;
    38.         }
    39.         else
    40.         {
    41.  
    42.         }
    43.     }
    44. }
     
  2. footballhono52

    footballhono52

    Joined:
    Dec 3, 2015
    Posts:
    16
    Actually, I just found out that the Tile Map when the mouse enters anywhere on it, the TileMap moves positions to
    (845, 0, 745) and the cube doesnt move at all. If anybody knows why or sees a flaw in in please let me know
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,798
    On line 21 you override the selectionCube transform reference, so fi you were counting on it being set manually in the inspector, it is overridden to the object that IS the map, that map may be moved by line 37.
     
  4. footballhono52

    footballhono52

    Joined:
    Dec 3, 2015
    Posts:
    16
    So I shouldn't override the transform?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,798
    Perhaps you intend to, but make sure that the GameObject this script is on (which happens to RequireComponent a TileMap, so I'm guessing it is your tilemap) is actually the one you want to move when you click. That override line is saying "point to my transform" and line 37 says "move me," which means move the thing with the required TileMap.
     
  6. footballhono52

    footballhono52

    Joined:
    Dec 3, 2015
    Posts:
    16
    Ah okay yeah what I intend to do it keep the tile map stationary but move the cube to where the mouse is over the map
     
  7. footballhono52

    footballhono52

    Joined:
    Dec 3, 2015
    Posts:
    16
    I've tried doing a few things to move the selection cube but nothing has worked so far.

    How would I grab the selection cube and move the transform on the tile map?