What we need to do in this third part:
. RedBullet collides with GreenCircle.
. GreenCircle spawn GreenBullets.
. repeat the process each collision between "bullets" objects and GreenCircles.
Basically we need add physics components to GameObjects, then add colliders.
1. In the prefab folder, select RedBulletPrefab. In the Inspector panel, at bottom, click the button Add Component > Physics > Rigidbody. Then uncheck "use gravity", check "Is kinematic" and I checked too the z position and x, y, z rotation freeze options.
2. With RedBulletPrefab selected, add a new physics component, collider. For bullets I choose Box Collider, for the Green Circle I added an Sphere Collider. Just check "Is trigger" since we will use C# script to control collisions.
3. Do the steps 1 and 2 above to GreenCirclePrefab.
4. Let's create a new prefab, GreenBulletPrefab. It's based in the RedBulletPrefab, so drag and drop RedBulletPrefab into Hierarchy panel. Rename to GreenBulletPrefab. Go to Texture folder and assuming that you have already imported the greenbullet.png (Texture type: advanced, uncheck generate mip maps, filter mode: point), drag and drop into the new GreenBulletPrefab. Then drag and drop GreenBulletPrefab to the Prefab objects. Now you can delete GreenBulletPrefab from Hierarchy panel, since we will instantiate this object with C# script.
5. Open the CircleChain script in the Script folder. Here we need check for collisions and spawn the green bullets, then destroy the green circle object. Basically we will add this:
// prefab to be instantieated
public GameObject greenBulletPrefab;
public float greenBulletXSpeed;
public float greenBulletYSpeed;
public float bulletSpeed = 3;
...
// check for collisions
void OnTriggerEnter(Collider obj) {
// for debug
//print(obj.gameObject.name);
// if this object collides with the red/green Bullet
if (obj.gameObject.name == "RedBulletPrefab(Clone)" || obj.gameObject.name == "GreenBulletPrefab(Clone)") {
// calc the new bullets positions
for (int i = 0; i < 4; i++) {
// calc the direction of the bullet
greenBulletXSpeed = bulletSpeed * Mathf.Cos(i * Mathf.PI / 2); //redBullet.xSpeed=bulletSpeed*Math.cos(i*Math.PI/2);
greenBulletYSpeed = bulletSpeed * Mathf.Sin(i * Mathf.PI / 2); //redBullet.ySpeed=bulletSpeed*Math.sin(i*Math.PI/2);
// create a instance of bullet (green)
GameObject clone = Instantiate(greenBulletPrefab, transform.position, transform.rotation) as GameObject;
clone.SendMessage("setBulletXSpeed", greenBulletXSpeed);
clone.SendMessage("setBulletYSpeed", greenBulletYSpeed);
}
// after all, destroy the green circle
Destroy(this.gameObject);
}
}
We used this for our RedCircle script.
Thats it!
Download here the new complete project files: http://goo.gl/TgBehj
Just open the "scene03" in the scene folder.
Comentários