How to get first object in which Ray hit?
Hi, I have problem with get first object in which ray hit. I have two same objects in line. When i cast ray it come through first and second object and then return rayHit in the second collider. How can i get the first collider?
Answer by Vylax · Mar 20, 2016 at 09:22 PM
You can take the first collider by some different solution : 1. When the raycasthit hit something, make the script executing the commands and destroy after (it would be shorter to don't hit the second one). 2. use something like this :
var aGameObject : GameObject;
var aNumber : int = 1;
...
if("I don't remember the code for "if the raycasthit hit something" and I'm on phone so do this yourself" && aNumber == 1) {
aGameObject = "the object you hit (I don't remember the code to)";
aNumber = 2;
}
and here's other way to do it but I forget these while writing ^^ good luck with my bad envlish ^^ I hope it will help you
Answer by kanaron · Mar 21, 2016 at 07:12 AM
Thanks, but that don't work. I have script:
...
Debug.DrawRay(pos4, (pos2 - pos4), Color.red, Vector3.Distance(pos4, pos2)); //for see raycast
if (Physics.Raycast(pos4, (pos2 - pos4), out rayHit, Vector3.Distance(pos4, pos2))) //pos2 = actual position, pos4= position 2 frames ago
{
...
Debug.Log("HIT in " + rayHit.collider.name); // for see in what objects ray hit.
...
}
And this script works good. But Raycast don't work. I see the raycast is one frame too slow, but that i think doesn't matter. The main problem is with hit detection:
First i see raycast hit in object in which i cast ray. That's correct.
Then ray comes through plane, and this hit was detect. That's correctt too.
But now: I have object with left and right collider (left and right armor): First is right armor. Ray comes through and... don't detect collision... I should see in console "HIT in Right armor" but there is nothing. And then is left armor. And that collider is detected and script make some damage. But that damage should be in right side, not left.
can you add screenshots or a video for help mento visualize please ?
Okay, i find where is the problem: i have quite much colliders there, about which i forgot and that colliders are before (very near) armor colliders. So the line of ray was: 1. cast ray 2. plane 3. other collider and very near armor collider which wasn't detected.
So i change positions of armor colliders to position just before that other colliders and now everything works perfectly :D
can you don't use raycast an use an object (like a bullet) wich had a box collider and jse the function OnCollisionEnter like this :
function OnCollisionEnter (other : Collision) {
if(canhit == true)
if(other.gameObject.tag =="left side" || other.gameObject.tag == "right side") {
var thehitobject : GameObject = other.gameObject;
DoWhatYouWant();
canhit = false;
}
}
}
function DoWhatYouWant () {
print("do something");
}
tell me what you get
In fact i don't use raycast only. I use bullet with rigidbody because i need artillery shot so i need curve line of fall, not just straight line like raycast. I try to use only object with collider without raycast, but that doesn't work. Raycast is just between position of bullet from two frames ago and current position. And i use raycast for detecting hit and check angle of hit.
And i can't use only collider and OnCollisionEnter(). I must use object with sphere collider and raycast because my bullet have very high start speed (it is very important). In some cases the speed is so high and armor (object) is so small that very often bullet just comes through all object without detecting collision because in one frame bullet was just before armor and in next frame bullet was behind object or inside object. In that case there was no detect of hit or hit wasn't detect correctly (angle of collision was detected like 90* when in fact that was near 0*).