• 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 spitu · Jan 07, 2013 at 06:05 AM · raycasthit.pointnearest

Get nearest object by using raycast

well I'm trying to use a get nearest object script to work with raycasting but its not quite working.

 public Transform GetNearestTaggedMouseObject()
     {
         var nearestDistanceSqr = Mathf.Infinity;
         var taggedGameObjects = GameObject.FindGameObjectsWithTag("waypoint"); 
         Transform nearestObj2 = null;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         //Debug.DrawRay (ray.origin, ray.direction * 20, Color.red);
             
             if (Physics.Raycast(ray, out hit, 20)) 
             {
                 Vector3 destPoint = hit.point;
                 
                 foreach (var obj in taggedGameObjects)
                 {
                     var objectPos = obj.transform.position;
                     var distanceSqr = (objectPos - destPoint).sqrMagnitude;
                     
                     if (distanceSqr < nearestDistanceSqr)
                     {
                         nearestObj2 = obj.transform;
                         nearestDistanceSqr = distanceSqr;
                     }
                 }
             }
         if(Input.GetMouseButtonDown(0))
             moving2 = true;
         if(Vector3.Distance(transform.position, nearestObj2.transform.position) <= .1)
             moving2 = false;
         if(moving2 == true)
         {
             transform.LookAt(nearestObj2);
             transform.position = Vector3.MoveTowards(transform.position, nearestObj2.transform.position, Time.deltaTime);
         }
         return nearestObj2;
     }

This will make the controller move and follow the mouse cursor but not where you have clicked. I have also tried to put the raycast inside the if(Input.GetMouseButtonDown(0)) but it just made it so I had to hold down the mouse button for some reason. If there is a better way to do this without raycasting, I think I would rather do it that way but i haven't found anything else yet.

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
0

Answer by jogo13 · Jan 07, 2013 at 07:38 AM

I may be reading it wrong but i don't think you'll need raycasting for this.

Once you've got a waypoint you can just calculate the distance from your current point:

 float distance = Vector3.Distance(transform.position, waypoint.transform.position);

You'll need to go through all the waypoints and only keep the one with the shortest distance of course.

I would avoid using : foreach (var obj in taggedGameObjects) as even components are considered objects. (And may mean even more in javascript...)

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 spitu · Jan 07, 2013 at 07:14 PM 0
Share

sorry. I should have gave more detail of what this does. What it should do is select the nearest tagged object in a group of objects closest to where mouse is clicked and then the player should move to that object. AT$$anonymous$$ the player is moving towards and will follow the mouse cursor and not where you click.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

Moving gameobject along terrain based on player movement using raycast 1 Answer

Raycast.hitpoint always returning back to the camera 0 Answers

Linecast between character and camera doesn't ignoring Characters collider. 2 Answers

Can't Make Raycast Target Nearest Character 1 Answer

Strange Behavior with Hit.point 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