So i have 2 script, the first script is just a player script, the second script is to generate the map, when i click the player, the log showed up, when i click the map, the log showed up, but when i clicked on nothing (area outside the map), log not showed up (which is good), so what i want is only by clicking the player the log showed up.
TileMap Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TileMap : MonoBehaviour {
public TileType[] tileTypes;
int[,] tiles;
int mapSizeX = 10;
int mapSizeZ = 10;
// Use this for initialization
void Start()
{
//Allocate the tiles of the map
tiles = new int[mapSizeX, mapSizeZ];
//Create the map
CreateMap();
//Generate the map
GenerateMap();
}
void GenerateMap()
{
for (int x = 0; x < mapSizeX; x++)
{
for (int z = 0; z < mapSizeZ; z++)
{
TileType tt = tileTypes[tiles[x, z]];
Instantiate(tt.tileVisualPrefab, new Vector3(x, 0, z), Quaternion.identity);
}
}
}
void CreateMap()
{
//Create the map based on the tiles
for (int x = 0; x < mapSizeX; x++)
{
for (int z = 0; z < mapSizeZ; z++)
{
tiles[x, z] = 0;
}
}
//Create the road
tiles[4, 2] = 1;
tiles[4, 3] = 1;
tiles[4, 4] = 1;
tiles[5, 4] = 1;
tiles[6, 4] = 1;
tiles[7, 4] = 1;
tiles[3, 4] = 1;
tiles[2, 4] = 1;
tiles[1, 4] = 1;
//Create entry/ exit point
tiles[4, 1] = 3;
}
// Update is called once per frame
void Update()
{
}
}
and the player script (player got capsule collider and the script attached)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerScript : MonoBehaviour
{
public static bool isClicked;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
isClicked = true;
Debug.Log("Player CLicked");
}
}
}
}