- Home /
[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)){
}
}
}
}
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.
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.
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.
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
Follow this Question
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