- Home /
Use ray cast against objects but avoid colliding against other objects
I am trying to make a fruit ninja rip off.
My issue is where I don't want the fruits and bombs to collide but still want to be able to raycast onto them.
I don't know if there some way to use layers but there is nothing I can find about it.
Answer by dmg0600 · Oct 11, 2014 at 06:28 PM
Assign different layers to the fruits and the bombs and use the collision matrix to avoid them to collide between each other.
Answer by MrSoad · Oct 11, 2014 at 07:01 PM
I'm not quite sure what it is you want to do but if you want to use a layer mask do the following :
public var lmPlayer : LayerMask;
function subRaycast() {
var hit : RaycastHit;
var fRaycast_Distance : float = 5.0;
//Raycast from current position of your object in forward direction.
if (Physics.Raycast (transform.position, transform.forward, hit, fRaycast_Distance, lmPlayer)) {
//If object is found...
if (hit.collider != null) {
//Do your code...
}
}
}
Use the public layer mask as you will be able to pick what to exclude easily from the drop down menu in the inspector window. Apologies for any code formatting issues, I'm having a nightmare with this, nothing works...
I understand what you mean now that dmg0600 has replied. In fruit ninja none of the objects collide with each other. Use triggers ins$$anonymous$$d of colliders. They will all pass through each other. You will have to change your code but this is the easiest way to do what you want. Raycast should work fine on the triggers.
Your answer
Follow this Question
Related Questions
Is Object At Location? 3 Answers
Extend the Collider When Picking Up Objects 0 Answers
Mouse clicking on an object (Raycast.hit) for another object to connect to it? 0 Answers
Raycast and Hit Problem 0 Answers