In the space shooter I’m designing, a dead ship spawns a bunch of ship fragments, which are supposed to drift apart and then disappear after a short time. To do that for my Slicer fighter, I have two scripts - SlicerFragElement, which is assigned to each of the child fragments, has velocity and angular velocity fields, and simply handles translating the frag it is assigned to. To set this up, I have the SlicerFragElement script, which is supposed to assign velocities to all of the individual frags. To do that, I have the following code (print statements are for debugging):
var startTime : float;
var duration : float = 5.0;
function Start () {
startTime = Time.time;
//*
for (var child : Transform in this.transform) {
var distanceVec : Vector3 = child.position - transform.position;
//print(distanceVec);
//print(child.name);
child.GetComponent("SlicerFragElement").velocity =
Random.Range(5.0, 14.0) * (1 / Mathf.Pow(distanceVec.magnitude + .2, 2)) * distanceVec.normalized;
//print(child.GetComponent("FalconFragElement").angularVel);
}
}
function Update () {
if (Time.time > startTime + duration) {
Destroy(this.gameObject);
}
}
The problem is, this code cannot seem to modify the SlicerFragElement scripts for any of the children. Their velocity stays at 0, the print statements show (0,0,0,0) and (0,0,0) for the angularVel and velocity, even when the angularVel (assigned in SlicerFragElement ) is nonzero. However, the names do show up correctly.
Any idea what the problem is? Also, would just assigning rigidbodies to each of the frag elements be more processor-efficient (there may be hundreds of these onscreen at once, so that’s a concern)?