I am very new to all of this and am trying to piece together many tutorials, but figured after 6-7 hours of them not being specific to my issue I was better off asking for help.
I am sure however I am doing this is wrong and convoluted, so I am open to hearing corrections. My aim here is learning.
First here is what I have for a grid. Hexagons with flat tops (almost every movement tutorial was pointed top, this made it difficult)
The white Hex is the player (game object Player). The ball is the game object I want to move before the player to check for collision data (game object PlayerMovePoint). There are 6 game objects that surround the player and are attached to it as children (those are the hexes with transparency around them next to the player) (top, topleft, bottomleft, bottom, bottomright, topright)
I made a script called playerMovementGrid and created gameobjects within the script for each hex surrounding the player(top, topleft,bottomleft, etc). I then attached those game objects to the script.
I tried all kinds of Vector3 movements to try to detect what object (topleft, top, topright, etc) was clicked, and then move the PlayerMovePoint ball to that grid. If it did not detect collision, then the player (and in turn all its children, the grid surrounding it) also moved.
I tried getting the mouse location, I tried detecting which object was clicked with
if (eventData.pointerCurrentRaycast.gameObject.name == "upperright")
which clearly was not correct, as it wouldn’t give me a debug message when I attached one.
I will attach the mess of code that is my latest attempt that I used, but I am sure it is all 100% useless and wrong and that I should be starting again with something more simple. (There is no collision detection in this script, I was going to try to figure that out after, though I have a collision layer and collision items)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class PlayerMovementGrid : MonoBehaviour, IPointerDownHandler
{
public Transform movePoint;
public float moveSpeed = 5f;
public GameObject topRight;
public GameObject bottomRight;
public GameObject bottomLeft;
public GameObject topleft;
public GameObject top;
public GameObject bottom;
public GameObject CurrentObject;
private void Start()
{
movePoint.parent = null;
AddPhysics2DRaycaster();
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);
Debug.Log(Input.mousePosition);
if (eventData.pointerCurrentRaycast.gameObject.name == "upperright")
{
movePoint.position += Vector3.MoveTowards(transform.position, topRight.transform.position , Time.deltaTime * moveSpeed) ;
}
}
private void AddPhysics2DRaycaster()
{
Physics2DRaycaster physicsRaycaster = FindObjectOfType<Physics2DRaycaster>();
if (physicsRaycaster == null)
{
Camera.main.gameObject.AddComponent<Physics2DRaycaster>();
}
}
}
Apologizes that this is all incorrect and Surely I need to start over. I have only been learning code and unity for a few days.
I guess in the end I want to know why my attempts are failing, and what I should have been doing this entire time (and I really don’t know how to determine how far I need to move my player to line up in one of the grid cells, X, Y, Z)
Any help or even links to FLAT top based hex tutorials that LIMIT movement to one grid next to you (most I’ve found move you as far as you want, I only want to move ONE grid, as the player will have Action Points per turn, and if that is at 0, they cannot move)
My goal is to move the playerMovePoint ONE grid, check for Collision, and if there is none (and player has an action point), then the Player moves to the grid that was clicked. (and now the playerMovePoint and the Player should both be in the same spot)
Thank you if you read this far. Please keep in mind my low knowledge level when explaining.