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 /
avatar image
0
Question by Benjam · Nov 25, 2013 at 10:15 PM · physicsraycasthitcollider.raycast

How to stop RaycastHit2D from using the last object in the hierachy?

Hello all,

I am having a few issues with my raycasting and I was wondering if anyone could help.

Basically what I am doing is using the raycast to gather info on objects that have been clicked on. When you click on an object all the child objects that contain tags are gathered and the info is used to change animations and collections of sprites etc.

The problem I am having is when the object is clicked on instead of returning the object clicked on it is returning the object that is last in the hierarchy.

Here is my raycast function:

 public virtual void mouseRayInput()
     {
 
         if(Input.GetMouseButtonDown(0))
         {
             mouseScreenPos = Input.mousePosition; //Gets the mouse position in 2D space
             mouseWorldPos = Camera.main.ScreenToWorldPoint(mouseScreenPos); //converts 2d mouse position into 3d world
             oldMouseWorldPos = mouseWorldPos; //stores the last known mouse click position
             hasClicked = true;
 
             RaycastHit2D hit = Physics2D.Raycast(mouseWorldPos, Vector2.zero);
 
             if(hit)
             {
                 colliderName = hit.collider.tag;
                 print ("Your object is = " + hit.collider.tag);
                 clickedInsideBounds = true;
             }
             else
             {
                 clickedInsideBounds = false;
             }
         }
         else if(Input.GetMouseButtonUp(0))
         {
             hasClicked = false;
         }
     }

and here is my clicked inside sprite function:

 public void clickedInsideSpriteSequence()
 {
     string [] arrayOfTags = new string[transform.childCount]; //Sets up a local array to store the child tags in
     int numOfChildrenCount = 0;

     if(clickedInsideBounds == true && hasClicked == true && colliderName != "Player")
     {
         rayCast = false;
         //Gets the child of the sprite this script is attached to and returns its tag
         foreach (Transform child in transform)
         {
             arrayOfTags[numOfChildrenCount] = child.tag; //store the tag in the array
             numOfChildrenCount++;
         }
         print (arrayOfTags[0]);
          //Sets each property to be that of the children under the object clicked on
         Animation_Class.libraryName = clickableSprite.tag;
         Animation_Class.collectionName = arrayOfTags[0];
         Animation_Class.currentAnim = arrayOfTags[1];
         Animation_Class.currentIdleAnim = arrayOfTags[2];
         Animation_Class.numOfAnims = int.Parse(arrayOfTags[3]); //converts string to integer using int.parse

         changeLibrary(libraryName, collectionName, currentAnim + animNum as string);
         Animation_Class.playSequence();
         sequencing = true;

         print (collectionName);
         print (currentAnim);
         print (numOfAnims);
         //Destroy(this); //remove the script preventing any more animation changes
     }

 }

I read somewhere briefly about using collider.raycast but then I read somewhere else that it doesn't really work??

Has anyone had this type of issue before, if so any help would be appreciated :)

Cheers!!

Comment
Add comment · Show 1
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 Benjam · Dec 04, 2013 at 12:53 PM 0
Share

I have tried looking for a solution to this but I am totally stumped and have been for a while. The script is attached to more than one object. Could this be the problem?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by DenisM · Dec 15, 2013 at 09:27 AM

You need to use RaycastAll instead

RaycastHit2D [] hit = Physics2D.RaycastAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

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

17 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

Related Questions

2D 360 degress platformer example needed 0 Answers

Physics.Raycast Help 2 Answers

Raycasting will not hit some imported meshes with mesh colliders? 2 Answers

Raycast not applying force to point 1 Answer

RaycastHit if/else Problem (bug?) 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