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 sacajawhoohoo · Jun 30, 2016 at 04:59 AM · errorraycastraycastingnot workingraycasts

Raycast hits and affects all items within the same tag

So I am using raycast to detect player interactions in the world, and I am using tags to separate the different types of objects you can interact with. This seemingly works fine in separating the different types of objects, but my issue is that is I have more than one object in the same tag the raycast affects all of the items in that tag instead of the single object that I want it to.

Here is the code:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class HungerAdd : MonoBehaviour {
     public GameObject self;
     public GameObject EatMeGUI;
     public GameObject Audio1;
     public Slider slider;
     public GameObject GunCheckIntObj2;
     public GameObject gameOverMan;
 
     void FixedUpdate() 
     {
         if (gameOverMan.activeSelf == false) {
             Ray mouseRay = Camera.main.ScreenPointToRay (Input.mousePosition);
             RaycastHit rayHit = new RaycastHit ();
 
             if (Physics.Raycast (mouseRay, out rayHit, 2) && (rayHit.collider.gameObject.tag == "Food") && (GunCheckIntObj2.activeSelf == false)) {
                 EatMeGUI.gameObject.SetActive (true);
             } else {
                 EatMeGUI.gameObject.SetActive (false);
             }
 
             if (Input.GetButtonDown ("Fire1") && (Physics.Raycast (mouseRay, out rayHit, 2)) && (rayHit.collider.gameObject.tag == "Food") && (GunCheckIntObj2.activeSelf == false)) {
                 EatMeGUI.gameObject.SetActive (false);
                 slider.value += 10;
                 self.gameObject.SetActive (false);
                 Audio1.gameObject.GetComponent<AudioSource> ().Play ();
             }
         }
     }
 }

The only thing I can think is that the "new RaycastHit" only creates one call per script so is multiple items use the same script it affects all of those items. Am I missing something here, and how would I fix it?

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

Answer by JigneshKoradiya · Jun 30, 2016 at 06:48 AM

 if (Physics.Raycast (mouseRay, out rayHit, 2) && (rayHit.collider.gameObject.tag == "Food") && (GunCheckIntObj2.activeSelf == false))
 
 {
    EatMeGUI.gameObject.SetActive (true);
              
 }
 else 
 {
      EatMeGUI.gameObject.SetActive (false);
              
 }


in this if condition add rayHit.collider.gameObject.name=="yourplayrname" in OR condition so it will only work if your game object has same name

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

Answer by giorashc · Jun 30, 2016 at 07:33 AM

Yes, if your script runs on more then one object then you will have several ray casts in the scene per fixed update. Since you are using the referenced objects (instead of the object that was actually hit by the ray) you see all "Food" items affected.

To solve your two problems:

  1. Use only one game object (an empty) which runs a script with ray collision detection for these "food" objects.

  2. When you detect a hit use the rayHit.collider.gameObject to perform the logic you need on that specific object.

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

73 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

Related Questions

Use RayCasting and simulate collision physics 1 Answer

Raycasting from object rotation? 1 Answer

Raycast returns null for no apparent reason 0 Answers

Raycast Having Pointless Errors 1 Answer

[help]RayCast check [SOLVED] 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