how determine if the player is nearby when clicking on a game object to ?
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);
}
}
You use Raycast incorrectly. You provide a point as second argument instead of a direction. Either use Linecast
or replace the second argument by a simple direction, and provide the max distance as the third argument.
But relying on Raycast does not look like a good solution for me. Don't you have a data structure somewhere containing the character data you could query to know what is surrounding a character at a given position? Something like a 2D array.
I'll try to use Linecast. I do not have a data structure containing the character data that I can query to find out what surrounds the character at a given position.
Answer by Neengash · Mar 04, 2021 at 09:16 AM
Rather than raycast, for your case I'd use the overlap static function you can find on the Physics2D. There are different shapes with the overlap function, I believe the circle is the one that fits your scenario the most.
https://docs.unity3d.com/ScriptReference/Physics2D.OverlapCircle.html
The way this works is basically as if it created a collider with the given attributes but just for the check (non persistent on the scene) and returns all colliders it collides with. Then the same way you did with the raycast, you just check if some of those colliders have the player tag.
Your answer
Follow this Question
Related Questions
Getting trouble in migrating the Unity IAP old version to new version of IAP in existing project 0 Answers
how to automatic add numbers in text after the slider finished? 0 Answers
Some questions about 2d games in unity 2 Answers
CommandInvokationFailure: Unable to merge android manifests. ,Unable to merge android manifest Error 0 Answers