Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by KeIk12345 · Mar 01, 2021 at 07:31 PM · unity 5unity 2dgamegame development

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);
        
     }
 }
 

1.png (19.6 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Feb 28, 2021 at 11:04 AM 0
Share

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.

avatar image KeIk12345 Hellium · Feb 28, 2021 at 11:17 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KeIk12345 · Mar 05, 2021 at 01:00 PM 0
Share

ok I'll try

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

274 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Problem with Unity Ads - UnityAdsEditor: Initialize(1529584, False); UnityEditor.Advertisements.UnityAdsEditor:EditorOnLoad() 0 Answers

CommandInvokationFailure: Unable to merge android manifests. ,Unable to merge android manifest Error 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges