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 Bokaii · Aug 04, 2014 at 12:09 PM · raycastmultiplayerfpsraycastall

[CLOSED]RaycastAll Help

I don't understand RaycastAll, I know how to use Raycast, but I don't understand how you loop through all the hit points, and then find the closest.

I need RaycastAll because I'm hitting myself, which is not supposed to happen.

Heres the code. Its just basically saying if I hit fire1 shoot a RaycastAll, but thats it...:( I don't understand, and please explain how it works, so I can understand it.

Code: using UnityEngine; using System.Collections;

 public class Shoot : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if(Input.GetButtonDown ("Fire1")){
             Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
             RaycastHit[] hits;
 
             if(Physics.Raycast(ray, out hits)){
 
             }
         }
     }
 }

Comment
Add comment · Show 2
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 robertbu · Aug 04, 2014 at 02:12 PM 0
Share

I'm repeating my comment from your other question here.

What kind of collider do you have on your FPS? Typically a capsule collider is used, and I've never heard of a capsule collier getting hit from a raycast from the pivot of the capsule. Do a:

  Debug.Log(hits.name+", "+hits.tag);

...inside the raycast to verify what is getting hit. If you have a more complex mesh on the player, or if the player is holding something that the raycast might hit, then there are alternate solutions to solve the problem. One solution is to put your player on its own layer, and use a layer mask to eli$$anonymous$$ate the player. Be careful doing your research with layer masks. They are bitsets, not integers, and it is easy to get them wrong. I$$anonymous$$HO RaycastAll() is not the right solution to fix the probelm. There are other potential solutions to the raycast hitting the player, but I'd need to know what is being hit, its relationship to the player, and how the part that is getting hit interacts with the environment.

avatar image Bokaii · Aug 04, 2014 at 08:11 PM 0
Share

Thank you for your answer again ;) But if you go create a quick game, with only a fps controller and a normal raycast script, from the main camera like:

 function Update(){
     Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.Forward);
     RaycastHit hit;
 
     if(Physics.RaycastHit(ray, out hit)){
         print(hit.collider.name);
     }
 
 }


Just something like that, and then try to look down, It should print out the name of the first person controller. At least thats what it did when I did it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by darthtelle · Aug 04, 2014 at 01:27 PM

You aren't actually calling RaycastAll here. See the API page here. The code provides an example of how to use the function and how to loop through the results.

 int layerMask = LayerMask.NameToLayer("Player");
 
 RaycastHit[] hits;
 hits = Physics.RaycastAll(transform.position, transform.forward, 100.0F, layerMask);
     
 for(int rayIndex = 0; rayIndex < hits.Length; rayIndex++)
 {
 // Do something
 }

To stop the ray from hitting your character you could set up a layer mask, which the API explains here.

And to sort the hit results by distance, a quick Google search has some help.

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 Bokaii · Aug 04, 2014 at 08:12 PM 0
Share

I figured it out,

 void Fire(){
         RaycastHit[] hits;
 
         hits = Physics.RaycastAll (transform.position, transform.forward);
         float $$anonymous$$Dist;
         $$anonymous$$Dist = $$anonymous$$athf.Infinity;
 
         for(int i=0;i<hits.Length;i++){
             if(hits[i].transform != this.transform.parent){
                 if(Vector3.Distance(transform.position,hits[i].point) < $$anonymous$$Dist){
                     $$anonymous$$Dist = Vector3.Distance(transform.position, hits[i].point);
                     print (hits[i].collider.name);
 
                     //Applying damage
                     Health h = hits[i].collider.gameObject.GetComponent<Health>();
 
                     if(h!=null){
                         h.TakeDamage(damage);
                     }
                 }
             }
         }
     }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Bullet Effect (RaycastAll Question) 1 Answer

[CLOSED]RaycastAll find closest hit 2 Answers

Check if RaycastAll hits[i].point == null 0 Answers

RaycastAll Detect if nothing hit 1 Answer

yield WaitForSeconds c# 4 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