Search Unity

Troubleshooting basic movement problems

Discussion in 'Scripting' started by ICarb0n, Sep 2, 2014.

  1. ICarb0n

    ICarb0n

    Joined:
    Apr 19, 2014
    Posts:
    1
    Hi there, I'm very new to unity and programming (so sorry if this i posted in the wrong place), but have a little experience with c++, I have followed several tutorials by quil18, but im having trouble with one particular thing. I have followed his tilemap tutorial :




    I'm having trouble implementing a 'selection indicator' to highlight wight tile the mouse is hovering over. In the tutorial he uses a slightly transparent cube, raycast from the mousepointer to the tilemap mesh, get the coordinates from the mesh (hit info point), a little maths to determine which tile you are pointing to, then move the cube to that tile. My only problem is the cube isnt moving, i have put in debug logs, the correct tile is being shown, the transform position line runs, but the cube doesn't move. Im sorry if this is a really blindingly simple error but its quite frustrating. My code below:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. [RequireComponent(typeof(TileMap))]
    5. public class TileMapMouse : MonoBehaviour {
    6.  
    7. TileMap _tileMap;
    8. Vector3 currentTileCoord;
    9. public Transform selectionIndicator;
    10.  
    11.     void Start(){
    12.         _tileMap = GetComponent<TileMap>();
    13.     }
    14.  
    15.         // Update is called once per frame
    16.     void Update () {
    17.  
    18.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    19.         RaycastHit hitInfo;
    20.  
    21.         if ( collider.Raycast(ray , out hitInfo, Mathf.Infinity) ){
    22.             int x = Mathf.FloorToInt(hitInfo.point.x / _tileMap.tileSize);
    23.             int z = Mathf.FloorToInt(hitInfo.point.z / _tileMap.tileSize);
    24.             Debug.Log ("Tile"+ x + "," + z);
    25.             currentTileCoord.x = x;
    26.             currentTileCoord.z = z;
    27.             selectionIndicator.transform.position = currentTileCoord * 5f;
    28.         }
    29.  
    30.         else{
    31.             //stuff
    32.         }
    33.     }
    34. }
    35.  
    Edit:format
     
    Last edited: Sep 2, 2014