• 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 sam2003hemp · Apr 17, 2020 at 05:49 PM · raycastraycastingboxcollider

Why does my Raycast stop detecting colliders

When the game is first ran the raycast will pick up the collider however it will just stop at random ive tried 2 differnt ways of doing it and nothing has fixed it any help would be nice.

Comment
Add comment · Show 3
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 matthewosb003 · Apr 17, 2020 at 06:11 PM 0
Share

Could you share the code? might be greatly useful to see what you're doing

avatar image sam2003hemp matthewosb003 · Apr 17, 2020 at 06:12 PM 0
Share

the codes been posted

avatar image matthewosb003 sam2003hemp · Apr 17, 2020 at 06:20 PM 0
Share

yey, OK well Imma leave that to someone else I've not used ray casters for at least a year.

2 Replies

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

Answer by Quickz · Apr 17, 2020 at 06:26 PM

Physics.Raycast stops as soon as it hits a single collider. If you want the ray to not stop and return more than a single hit, use Physics.RaycastAll instead. Then iterate through the collection it returns and check for what you need there.

Here's a sample based on what you provided:

 using System.Linq;
 using UnityEngine;
 
 public class TestScript : MonoBehaviour
 {
     private void FixedUpdate()
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit[] hits = Physics.RaycastAll(ray, Mathf.Infinity);
 
         if (hits.Any(x => x.collider.tag == "Unit"))
         {
             Debug.Log("Bang");
         }
     }
 }

Also an extra note regarding the Physics.Raycast. It returns a boolean. If you want to access the object the raycast found. Use the hit variable (example: hit.collider.gameObject).

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 sam2003hemp · Apr 17, 2020 at 06:52 PM 0
Share

thanks dad

avatar image
0

Answer by sam2003hemp · Apr 17, 2020 at 06:11 PM

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class test : MonoBehaviour { public List Highlit; public List Selected;

 private Transform Sunit;
 
 private GameObject Curtuse;
 public Material highlit;
 public Material selc;
 public Material unSelec;
 private bool high;
  
 private RaycastHit hit;
 

   
 // Start is called before the first frame update
 void Start()
 {
     high = false;       
     Highlit = new List<GameObject>();
     Selected = new List<GameObject>();

 }

 // Update is called once per frame#
 void FixedUpdate(){
     Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
     //RaycastHit hit;
     LayerMask m = ~8;
     
     if (Physics.Raycast(r ,out hit, Mathf.Infinity) == GameObject.FindGameObjectWithTag("unit")){
       Debug.Log("Bang");
      }

 }

 void Update()
 {
        if(hit.collider.tag =="unit"){            
         Curtuse = hit.collider.gameObject;
             if(Highlit.Contains(Curtuse)){
          Curtuse.GetComponent<MeshRenderer> ().material = highlit;
          
         
         }
         if (Highlit.Contains(Curtuse) == false){
            Highlit.Add(Curtuse);
         }
         

        
         

      }
      else
      {
          foreach(GameObject obj in Highlit){
            Curtuse.GetComponent<MeshRenderer> ().material = unSelec;
            Debug.Log("hit floor");
         }
         Highlit.Clear();
        
        
        
     
          
         
     }
     

} }

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.

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

178 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

Related Questions

How can I make diagonal collisions work with raycasts from a boxcollider? (3D, C#) 0 Answers

Is this way of making a raycast correct 2 Answers

Mouse Over Questions 1 Answer

¿Why Raycast sometimes don't collide with rigidbody (plus collider)? 0 Answers

For raycast hits, what is a "barycentric coordinate"? 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