• 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 Tschlompf · Aug 16, 2016 at 06:02 PM · raycasthit

Find out sprite of an object my raycast hit

Hi! I asked this question before but it seemed to buggy or something, so I post it here again:

I have a raycast which goes through different objects. They all are one gameobject created from a prefab, but with two different sprites in my sprite renderer. I want to find out, which sprite the object hit by my raycast has on it. What is the best way to do that? :)

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
Best Answer

Answer by lunoland · Aug 16, 2016 at 06:25 PM

The raycasting functions return a struct which contains data about what the ray hit. You can use this to access components on the object that was hit, including the sprite. In your script, set up some references to the two sprites you care about:

 public Sprite yourSprite1; // Hook these up in the inspector, or load them from Resources.
 public Sprite yourSprite2;

Then, in whatever method you do your raycasting:

 Ray myRay = /* Code that specifies your ray goes here. */ ;
 RaycastHit[] hits = RaycastAll(myRay);
 
 foreach (RaycastHit hit in hits) {
     SpriteRenderer spriteRenderer = hit.collider.GetComponent<SpriteRenderer>();
     
     if (spriteRenderer != null) {
         if (spriteRenderer.sprite == yourSprite1)
             // Do something.
         else if (spriteRenderer.sprite == yourSprite2)
             // Do something else.
     }
 }

Comment
Add comment · Show 14 · 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 Tschlompf · Aug 16, 2016 at 08:52 PM 0
Share

That helped me very much, thank you :D

avatar image lunoland Tschlompf · Aug 16, 2016 at 08:56 PM 0
Share

No problem :) If this was all you needed, be sure to accept the answer so the thread closes.

avatar image Tschlompf · Aug 16, 2016 at 09:32 PM 0
Share

Hi! I got another problem now. When I write this:

Ray2D myRay = new Vector2(transformStone.position.x + 1, transformStone.position.y), Vector2.right;

I get this error: " Cannot implicitly convert type 'UnityEngine.Vector2' to 'UnityEngine.Ray2D' "

I think I am just making this ray wrong, but I can't find any solution.

avatar image lunoland Tschlompf · Aug 16, 2016 at 11:46 PM 0
Share

https://docs.unity3d.com/ScriptReference/Ray2D.html

The syntax is wrong for declaring a Ray2D. You need to declare a new instance of the Ray2D struct and pass in the two Vector2 parameters.

 Ray2D myRay = new Ray2D(new Vector2(transformStone.position.x + 1, transformStone.position.y), Vector2.right);



avatar image Tschlompf lunoland · Aug 18, 2016 at 07:49 AM 0
Share

Hi! Thanks for your answer. When I write it like this:

Ray2D myRay = New Ray2D(new Vector2(transformStone.position.x + 1, transformStone.position.y), Vector2.right); RaycastHit2D[] hits = Physics2D.RaycastAll(myRay);

An error says: "No overload for method 'RaycastAll' takes 1 arguments"

So I wrote it like this: RaycastHit2D[] hits = Physics2D.RaycastAll(new Vector2(transformStone.position.x + 1, transformStone.position.y), Vector2.right);

But my script does not work. It isn't showing any error. That's my full script:

using UnityEngine; using System.Collections;

public class RaycastWin : $$anonymous$$onoBehaviour { public Sprite orangeStone;

 // Use this for initialization
 void Start() {
     orangeStone = Resources.Load<Sprite>("OrangenerStein");
     
     
     
     

     
 }

 // Update is called once per frame
 void Update() {
     var transformStone = GetComponent<Transform>();
     RaycastHit2D[] hits = Physics2D.RaycastAll(new Vector2(transformStone.position.x + 1, transformStone.position.y), Vector2.right);

     foreach (RaycastHit2D hit in hits) {
         SpriteRenderer spriteRenderer = hit.collider.GetComponent<SpriteRenderer>();

         if (spriteRenderer != null) {

             if (spriteRenderer.sprite == orangeStone) {
                 Debug.Log("HIT!");
           } else { Debug.Log("NOT!"); }
         }
     }
 }
     
     

 }

I'm attaching that script to my prefab-gameobject. In another script I place it on mouseclick, and then my console says "NOT!", as expected. But when I place another object of that prefab to the right site of the first object, with the orangeStone sprite attached, the console still says "NOT!".

Am I doing something wrong in my script? Thanks in advance :)

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.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

raycast not working after mesh deformed via shader 1 Answer

Raycast returns null for no apparent reason 0 Answers

Can't Instantiate the GameObject and make it parent to GameObject which it hits. 0 Answers

ScreenToRayPoint not working in VR, how to substitute? 0 Answers

Raycast + Instantiate 2 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