Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 vittu1994 · Apr 27, 2016 at 04:24 PM · gameobjectraycasttransformlistchildren

use list of gameobjects transform in raycast

Hey i would like to cast a raycast that involves more than 1 raycasthit. I have a list of gameobjects that i want to use. I want to use these gameobjects and their transform to position where i want to shoot these raycasts.

 public void GetParts()
     {
         aquiredParts = aquiredShip.GetComponent<Ship>().shipParts;
     }

aquiredParts is the list with the gameObjects i wanna raycast on. I access this function from another script which is on a parent gameobject. The list consists of the childs of this parent and it updates everytime i aquire a new gameobject. This is how it looks in that script:

  public void CheckParts()
     {
         foreach (Transform child in gameObject.transform)
         {
             if (child.gameObject.tag == "shipPart")
             {
                 shipParts.Add(child.gameObject);
             }
         }
     }

Since both these functions run in start() the list will always update with each new created object.

But for the main point of the question. How can i cast several raycasts (either a raycast array or a raycastAll) on each of these gameobjects that are in this list?

Thanks.

Comment
Add comment · Show 7
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 Cherno · Apr 27, 2016 at 05:30 PM 0
Share
 foreach(GameObject go in shipParts) {
      RaycastHit hit = new RaycastHit();
      if(Physics.Raycast(go.transform.position, go.transform.forward, out hit) {
 
      }
 }
avatar image Glurth Cherno · Apr 27, 2016 at 05:56 PM 0
Share

Just to expand on this slightly, this RaycastHit "hit" passed OUT from Physics.Raycast will include the First collider hit by the raycast operation. Note: Chero's suggest will raycast "forward" from each ship part- I'm not sure that's actually what you want it do. For example, if detecting a hit by an enemy laser beam, your Ray's source and direction would remain constant for all those Raycasts.

You could also use RayCastAll, which returns an array of RaycastHit objects, that represents ALL the colliders hit by the ray. You can then go through that list to find "shipPart" objects, or whatverer.

avatar image vittu1994 Glurth · Apr 27, 2016 at 06:29 PM 0
Share

Yeah the direction of the raycast can change since i can rotate the object with another function. However i need each raycast to meet the same condition if the if-statement is gonna go through. (for example: if hit.collider.tag == "placeToBuild"). All the raycasthits need to get gameobject "placeToBuild for the if statement to be true, not that only one match is needed to break the loop.

Show more comments
Show more comments
avatar image Glurth · Apr 27, 2016 at 07:43 PM 0
Share

Ah, ok. And do you need to detect if the raycast hits another object before it reaches your target object? Or do you want to ignore all other objects?

If you want to ignore the other objects use collider.raycast. (http://docs.unity3d.com/ScriptReference/Collider.Raycast.html)

If you need to detect other objects that might be in the way, use physics.raycast (http://docs.unity3d.com/ScriptReference/Physics.Raycast.html)

Either way it sounds like you will need to make the raycast function call a FEW times, once for each ray, and logically AND all the results of your hit-tag-checking logic.

0 Replies

· Add your reply
  • Sort: 

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

62 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

Related Questions

Keep adding targets to a list 2 Answers

how to get the snake body parts to follow the snake head? 1 Answer

The Weirdest Bug Ever: GameObject Transform Appears in Editor to Be in One Spot but Instantiates and Registers in Another 2 Answers

Layered Collisions 1 Answer

How Should I Get a List of Child Objects 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