0

I am currently working on a 2D isometric game where the player will be able to control different units, and interact through them.

I created a Scriptable Object called UnitType. Thanks to this system I can define an action range and a moving range, that is the maximum distance in cells that the player can move to or interact with.

The problem is I don't know how to implement this through code. This is what I want to achieve, using an action range of 2 cells for the demonstration. This is the goal

With a friend of mine, we thought about calculating the linear equation of those 4 lines to check if the raycast hit was within them, but it's not working right with negative x. This is the current system

What would be the best practice ?

Thank you very much for your time and attention,

LaGrange
  • 21
  • 3
  • Provide some code – Saurabh Srivastava Feb 17 '21 at 14:59
  • Can there be obstacles? Can actions be blocked by obstacles? Can actions bend around obstacles? What distance metric do you want? Max x/y? – JonasH Feb 17 '21 at 15:19
  • Also, movement usually is done by pathfinding, while actions might need a system for line of sight, and these are different problems. You might also need to consider if you allow diagonal movement or not, and if these are more "expensive". – JonasH Feb 17 '21 at 15:22

1 Answers1

0

Mousehit is a Unity GameObject that is the child of the character. Its positions is bound to the center of the tilemap cells thanks to mousePosition that is a script attached to the GameManager.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

public class Interact : MonoBehaviour
{
    [SerializeField] private Sprite upperLeft, upperRight, midLeft, midRight, bottomLeft, bottomMid, bottomRight;
    [SerializeField] private GameObject mouseHit;
    public UnitType unitType;
    private GameObject gameManager;
    private CheckMousePosition mousePosition;
    private bool isInteracting, isAtGoodDistance;
    private SpriteRenderer spriteRenderer;
    private Tilemap tilemap;
    private RaycastHit2D hit;
    //private Transform mouseHit;
    void Start()
    {
        tilemap = GameObject.Find("Tilemap").GetComponent<Tilemap>();
        gameManager = GameObject.FindGameObjectWithTag("GameManager");
        mousePosition = gameManager.GetComponent<CheckMousePosition>();
        spriteRenderer = gameObject.GetComponent<SpriteRenderer>();

        isInteracting = false;
        mouseHit.SetActive(false);
    }

    // Update is called once per frame
    void Update()
    {
        if (isInteracting)
        {
            mouseHit.SetActive(true);
            mouseHit.transform.position = new Vector3(mousePosition.tilemap.GetCellCenterLocal(mousePosition.cellPosition).x, mousePosition.tilemap.GetCellCenterLocal(mousePosition.cellPosition).y, 1);

           

            if (Input.GetMouseButtonDown(0))
            {
                Interaction();
            }
        }

        if (isInteracting == false)
        {
            spriteRenderer.sprite = null;
            mouseHit.SetActive(false);
        }
    }

    public void InitInteraction()
    {
        isInteracting = true;
        transform.root.GetComponent<ContextualMenu>().CloseContextualMenu();
    }

    private void Interaction()
    {
        //When the player interacts, we cast a ray that will determine what he is interacting with.
        hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
        if (hit.collider != null)
        {
            if (isInteracting)
            {
                isInteracting = false; //the player is no longer interacting.

                //If the player is interacting with a farmable cell using the worker.
                if (transform.root.name == "Worker"
                    && hit.collider.GetComponent<FarmableCell>() != null)
                {
                    CheckInteractionDistance();
                    //hit.collider.GetComponent<FarmableCell>().CheckIfFarmed();
                }
            }
        }
    }

    private void CheckInteractionDistance()
    {
        if (mouseHit.transform.localPosition.y <= (-0.5 * (mouseHit.transform.localPosition.x) + unitType.actionDistance / 2)
            && mouseHit.transform.localPosition.x >= (-0.5 * (mouseHit.transform.localPosition.x) - unitType.actionDistance / 2)
            && mouseHit.transform.localPosition.x >= (0.5 * (mouseHit.transform.localPosition.x) - unitType.actionDistance / 2)
            && mouseHit.transform.localPosition.x <= (0.5 * (mouseHit.transform.localPosition.x) + unitType.actionDistance / 2))
        {
            Debug.Log("ok");
        }
        else
        {
            Debug.Log("not ok");
        }
    }
}
LaGrange
  • 21
  • 3