Hello, I have 11 cards and when I click on the enemy cards I would like to determine if the player is nearby. I tried to solve this problem by throwing 4 rays in different directions from the enemy cards, but for some reason only the bottom card works for me and all the others do not. If there is an opportunity, someone can roughly tell in what ways this can be implemented.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cardclic : MonoBehaviour
{
public float distanceheight;
public float distancewidth;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnMouseDown()
{
Physics2D.queriesStartInColliders = false;
RaycastHit2D hitup = Physics2D.Raycast(transform.position, transform.position + Vector3.up * transform.localScale.x * distanceheight);
RaycastHit2D hitlefl = Physics2D.Raycast(transform.position, transform.position + Vector3.left * transform.localScale.x * distancewidth);
RaycastHit2D hitrignt = Physics2D.Raycast(transform.position, transform.position + Vector3.right * transform.localScale.x * distancewidth);
RaycastHit2D hitdown = Physics2D.Raycast(transform.position, transform.position + Vector3.down * transform.localScale.x * distanceheight);
if ( hitup.collider.tag == "Player" || hitlefl.collider.tag == "Player" || hitrignt.collider.tag == "Player" || hitdown.collider.tag == "Player")
{
Debug.Log("23");
}
}
private void OnDrawGizmos() // луч чтобы был виден разработчику
{
Gizmos.color = Color.red;
Gizmos.DrawLine(transform.position, transform.position + Vector3.up * transform.localScale.x * distanceheight);
Gizmos.DrawLine(transform.position, transform.position + Vector3.left * transform.localScale.x * distancewidth);
Gizmos.DrawLine(transform.position, transform.position + Vector3.right * transform.localScale.x * distancewidth);
Gizmos.DrawLine(transform.position, transform.position + Vector3.down * transform.localScale.x * distanceheight);
}
}