Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 CassMeck · Apr 21, 2017 at 03:14 PM · c#raycastobjectsfindnearest

How can I find object ?

Hİ, This is my code to find the object nearest.Perceives the distance or proximity to the object.But the same object is being copied.When they are more than one, the code is not starting to work properly anymore. As objects multiply, I want the closest object of raycasting to react.All objects are now reacting.I've read most of the similar issues.But I do not have what I need.

 public Transform box;
 private Ray ray;
 RaycastHit hit;

 if (Input.GetButton ("Fire2")) {
 ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 if (Physics.Raycast (ray.origin, ray.direction, out hit)) {
 Vector3 pos = box.transform.position;
 float distance = Vector3.Distance (pos, hit.point);
 print ("there" + distance);
 if (distance > 1.5) {
 print ("it is long distance");
 Debug.DrawRay (pos, transform.position, Color.red, 1.0f);
 } else {
 print ("Near the distance");
 Debug.DrawRay (pos, transform.position, Color.green, 1.0f);
 }
 }
 }


It is output like this; alt text

But I want it this way, Whichever is closest, it interacts with the box; alt text

box.png (36.0 kB)
box2.png (26.9 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by SohailBukhari · Apr 25, 2017 at 11:28 AM

First of all you are not checking distance from more than one objects, so here you need all the references of the Transforms where you want to calculate the distance. Now you are just checking distance of one object, so you needs to change your code little bit. Make an array of transform and pass the Obstacles which you have in the inspector. After Passing the transforms then in the update method check distance from each object using foreach or you can use linq class.

 using UnityEngine;
 
 public class RaycastTest : MonoBehaviour
 {
     private RaycastHit _hit;
     private Ray _ray;
     public Transform[] ObstaclesaObjects;
  
     // Update is called once per frame
     private void FixedUpdate()
     {
         if (Input.GetButton("Fire2"))
         {
             _ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(_ray.origin, _ray.direction, out _hit))
             {
                 foreach (var item in ObstaclesaObjects)
                 {
                     var distance = Vector3.Distance(item.position, _hit.point);
                     print("there" + distance);
                     Debug.DrawRay(item.position, transform.position, distance > 1.5f ? Color.red : Color.green, 1.0f);
                 }
 
                
             }
         }
     }
 }
Comment
Add comment · Show 8 · 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 CassMeck · Apr 25, 2017 at 01:21 PM 0
Share

Thank you for your answer @SohailBukhari so, the black boxes in the picture are added later on the scene.When I click with the mouse the ground is being destroyed and replaced with these black boxes.It looks like I can not do what I want with the list method because it is an Instantiate.I tried many ways about it but it didn't work.In fact I only want to check around the black box.

alt text

  private Ray ray;
  private RaycastHit hit;
  private Vector3 endPoint;
  ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  
  if (Input.GetButton ("Fire2")) {
  if (Physics.Raycast(ray.origin, ray.direction, out hit, $$anonymous$$athf.Infinity))
  {
  if (Physics.Raycast (transform.position, Vector3.left, out hit, 1.0f)
  || Physics.Raycast (transform.position, Vector3.right, out hit, 1.0f)
  || Physics.Raycast (transform.position, Vector3.forward, out hit, 1.0f)
  || Physics.Raycast (transform.position, Vector3.back, out hit, 1.0f)) {
   if (hit.collider.transform.tag == "blackbox") {
  print ("found! :)");
  }
  }
  Debug.DrawRay (hit.transform.position, transform.right * r1, Color.gray);
  Debug.DrawRay (hit.transform.position, -transform.right * r1, Color.gray);
  Debug.DrawRay (hit.transform.position, transform.forward * r1, Color.gray);
  Debug.DrawRay (hit.transform.position, -transform.forward * r1, Color.gray);
  
  Debug.DrawRay (ray.origin, ray.direction * 50, Color.green);
  endPoint = hit.point;
  } else {
  endPoint = ray.origin + ray.direction.normalized*distance;
  Debug.DrawRay (ray.origin, ray.direction * 50, Color.black);
  }
  }


I wrote this code before but it didn't work.I do not know if my method is wrong, but can't it be controlled around Raycast?

draw.png (23.7 kB)
avatar image SohailBukhari CassMeck · Apr 25, 2017 at 02:35 PM 0
Share

i can't understand whats really your problem,What you tried in the list method ?. if you want to detect only black box then simply check whether its tag matched or not. assign a tag to black box and then check.

avatar image SohailBukhari SohailBukhari · Apr 25, 2017 at 02:37 PM 0
Share

you can track your instantiated objects by simply saving in array or list

Show more comments
avatar image SohailBukhari · Apr 26, 2017 at 06:26 AM 0
Share

you are casting ray from transform transform.position so tell me one thing whats your transform. If the script is attached on the box then transform will be your box. ins$$anonymous$$d of casting ray detect other boxes by math

Lets Suppose you have i number of rows and j number of columns. then find i by j-1 for left and j+1 for right. Then j by i-1 up box and j+1 down box. If your problem not solved then send me Unity package of your code and i will check.

avatar image CassMeck SohailBukhari · Apr 26, 2017 at 02:39 PM 0
Share

I've been dealing with it since morning, but I failed. :( I put the link down.Thank you for your help. @SohailBukhari

Project Link

avatar image SohailBukhari CassMeck · Apr 27, 2017 at 08:03 AM 0
Share

Script raycast code which i change.

 if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
             {
                 var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                 if (Physics.Raycast(ray.origin, ray.direction, out _hit, $$anonymous$$athf.Infinity))
                 {
                     if (Physics.Raycast(_hit.transform.position, Vector3.left, out _hitLeft, 1.0f))
                     {
                         if (_hitLeft.collider.gameObject.tag == "plane" && !_left)
                         {
                             Debug.Log("tag found");
                             _left = true;
                             _found++;
                         }
                     }
                     else if (Physics.Raycast(_hit.transform.position, Vector3.right, out _hitRight, 1.0f))
                     {
                         if (_hitRight.collider.gameObject.tag == "plane" && !_right)
                         {
                             _right = true;
                             _found++;
                         }
                     }
                     else if (Physics.Raycast(_hit.transform.position, Vector3.forward, out _hitup, 1.0f))
                     {
                         if (_hitup.collider.gameObject.tag == "plane" && !_up)
                         {
                             _up = true;
                             _found++;
                         }
                     }
                     else if (Physics.Raycast(_hit.transform.position, Vector3.back, out _hitDown, 1.0f))
                     {
                         if (_hitDown.collider.gameObject.tag == "plane" && !_down)
                         {
                             _down = true;
                             _found++;
                         }
                     }
     
                     if (_found == 4)
                     {
                         Debug.Log("Found");
                     }
                     Debug.DrawRay(_hit.transform.position, Vector3.left*_res, Color.yellow);
                     Debug.DrawRay(_hit.transform.position, Vector3.right*_res, Color.blue);
                     Debug.DrawRay(_hit.transform.position, Vector3.forward*_res, Color.green);
                     Debug.DrawRay(_hit.transform.position, Vector3.back*_res, Color.black);
                 }
             }



Download Project and check.

Show more comments
avatar image
0

Answer by CassMeck · Apr 27, 2017 at 10:21 AM

Thank you so much @SohailBukhari.I have reviewed the changes.I could not think of using an boolean.I used the same raycast for every direction.I guess I could not do it for this reason. By the way, the reset was super.When each box is placed, the boolean starts from scratch.Very logical.Thank you for your time. :)

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

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

346 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Rotated object raycasting in wrong directions!!? 3 Answers

No gameobject instance again in update 1 Answer

How can I move an object in the direction another object is facing. 1 Answer

change rigidbodies in all objects to kinematic 1 Answer

Bugging ragdoll 1 Answer


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