Reputation: 31
I have a prefab and its moving on Z-axis, want to destroy it when it reaches collider that is a trigger (yes I checked the Is Trigger box on the collider).
I have written this code directly off of Unity documentation found at: https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
function OnTriggerEnter (other : Collider){
Debug.Log ("Hello");
Destroy(other.gameObject);
}
and my prefab has the following script:
#pragma strict
var speedFactor : float = 3.0;
var rayDistance : float = 1.0;
var rayOriginOffset : float = 0.63;
function Start () {}
function Update () {
var rotationRaycast = transform.TransformDirection (Vector3.forward);
if(Physics.Raycast(transform.position + (transform.right * rayOriginOffset)
,rotationRaycast,rayDistance) || Physics.Raycast(transform.position +
(transform.right * -rayOriginOffset) ,rotationRaycast,rayDistance) ||
Physics.Raycast(transform.position ,rotationRaycast,rayDistance)){
}
else {
transform.Translate((Vector3.forward)* Time.deltaTime * speedFactor );
}
}
and my collider ignores raycasts (collision or trigger happens visually in the scene view). the prefab bypasses my collider/trigger and continues to infinity.
Upvotes: 0
Views: 1363
Reputation: 1825
I really hate it when someone is Using the Docs from Unity3d and it doesn't work. It is not your fault Murad. Unity3D developers never fixed this issue about "OnFly" or "Default Value"
var speedFactor : float = 3.0;
var rayDistance : float = 1.0;
var rayOriginOffset : float = 0.63;
This doesn't really work on JavaScript of MonoDevelop. I already inform Unity about this and they say, they would fix it. But still not. This will work for C# but not JS.
So you need to do this.
var speedFactor : float;
var rayDistance : float;
var rayOriginOffset : float;
function Start(){
speedFactor = 3.0;
rayDistance = 1.0;
rayOriginOffset = 0.63;
}
Make sure you have Rigid Body Component and a Collider in both game Objects.
Upvotes: 1