Hello everybody, i have a question and couldn’t find when i searched. I’m using detenator prefab for some of my practice to improve myself. I have cubes which goes to the checkpoint in the scene,i want them to stop going there when the explosion happens near them. But i couldn’t find something useful. Even if i try to make my own explosion, always the same when i say something like that to the cubes :
var explosion : Transform;
if(Vector3.Distance(transform.position,explosion.transform.position)<10){
StopFollowing();
}
it doesn’t work, it stops following when i throw the bomb not when it explodes.
I also tried instantiating a invisible sphere and telling to cubes to stop when they’re close to it but it doesn’t work properly.
So i thought something like that: when it explodes the cubes are being affected by the force of the explosion. Is there anyway to check if a rigidbody is affacted by any force?
Thank you :).
1 Answer
1
You can use [Physics.OverlapSphere][1] : it will return all colliders inside a imaginary sphere. You can sweep the colliders returned and call your StopFollowing function in each object which contains it:
var radius = 10.0; // sphere radius
function Start(){
var colliders: Collider[] = Physics.OverlapSphere(transform.position, radius);
for (var hit: Collider in colliders){ // check all objects inside sphere
var script: CubeScript = hit.GetComponent(CubeScript);
if (script){ // if the object has a script named CubeScript...
script.StopFollowing(); // call its function StopFollowing
}
}
}
You must add this script to the explosion prefab. When the explosion is instantiated, the script does a SphereCast and returns all colliders touched by the sphere. The for sweeps the array and calls the function StopFollowing in any object which has the script CubeScript attached (CubeScript is the type of a script named “CubeScript.js”; replace it with the actual cube script type, which is always its name without quotes or extension).
[1]: http://unity3d.com/support/documentation/ScriptReference/Physics.OverlapSphere.html?from=Rigidbody
thank you so much, i just couldn't find any way out and this is a clever way, i got it thank you :).
– Inan-EvinYou could set your speed variable to 0, then in an else set it back to whatever you currently have it set to.
– Radstronomical