Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
avatar image
0
Question by ScreamyCat · Feb 12, 2020 at 03:43 PM · button trigger eventsrangeradiusmerginggamobject

How do you check if a game object is in the radius of another game object?

I am building a merging game where I want to check if a tower of the same kind is placed on the node beside the first one. Basically I want to check if a game object of the same kind is beside the current game object. I have tried different ways that I thought were familiar but have had no progress. (also, this happens within a button output).


My sort of script*


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Merge : MonoBehaviour
 {
     //a radius that covers 8 nodes
     public float nearTurret = 6f;
     //the prefab that is going to be merged
     public GameObject slingshot;
 
     //On click event =
     public void TowerMerge()
     {
         if () // < I don't know what to put here, what I want to do is (if slingshot is in radius of nearTurret) then ...
         {
             Debug.Log("Fuse towers");
         }
         else
         {
             Debug.Log("No tower near");
         }
     }
 
     //Radius Gizmos
     private void OnDrawGizmosSelected()
     {
         Gizmos.color = Color.white;
         Gizmos.DrawWireSphere(transform.position, nearTurret);
     }
 }





Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by tormentoarmagedoom · Feb 12, 2020 at 03:48 PM

Hello there.

You need to calculate the distance between the 2 objects:

 if (Vector3.Distance (Object1.tranform.position, Object2.tranform.position) < 100)
 {
 //Do something because the distance is less than 100
 }

IF the number of towers in the map is not very big you can check for all other towers. If you have so many towers, You should create a Trigger collider to detect all towers inside the colliider, and then check for its distance to be sure.

Good Luck!

Comment
Add comment · Show 4 · 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 ScreamyCat · Feb 12, 2020 at 04:26 PM 0
Share

okay, it kinda works this time the only problem being that it detects itself as a mergeable tower. for example. if I place one tower and hit on fuse then it says "distance to other" 0.05 and then fuses. in that case, it should just do nothing since it should not merge with itself. How can I prevent this?

my code now looks like this

 public void Tower$$anonymous$$erge()
 {
     if (other)
     {
         float dist = Vector3.Distance(other.position, transform.position);
         print("Distance to other: " + dist);

         if (dist < 5)
         {
             Debug.Log("Fused");
         }else
         {
             Debug.Log("No towers nearby");
         }
     }

     
 }
avatar image tormentoarmagedoom ScreamyCat · Feb 12, 2020 at 04:28 PM 0
Share

Just add this to the if conditions:

if (other != gameObject)

$$anonymous$$eaning if "other" is not itself

Bye!

avatar image ScreamyCat tormentoarmagedoom · Feb 12, 2020 at 04:53 PM 0
Share

yee, so that didn't work? any more ideas because I am so lost at this point.

Show more comments

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

122 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

Related Questions

Damage Within Range 2 Answers

How can i merge mesh vertices using a script? 2 Answers

Issue with spawning buttons and assigning listeners 1 Answer

Problem with Navigation Mode & EventSystem.current.currentSelectedGameObject 0 Answers

Add slippery player movement to button controls? 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